[WIP] SVD (#16)

* - new SVD constructor
- OrthogonalDistribution now uses SVD custom op

Signed-off-by: raver119 <raver119@gmail.com>

* shapes fixed

Signed-off-by: raver119 <raver119@gmail.com>
master
raver119 2019-10-28 12:31:01 +03:00 committed by GitHub
parent 029a69a835
commit 5a4d2e8b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 49 deletions

View File

@ -47,6 +47,20 @@ public class Svd extends DynamicCustomOp {
public Svd(){ }
public Svd(INDArray input, boolean full_matrices, INDArray s, INDArray u, INDArray v) {
inputArguments.add(input);
fullUV = full_matrices;
computeUv = true;
switchNum = DEFAULT_SWITCHNUM;
outputArguments.add(s);
outputArguments.add(u);
outputArguments.add(v);
addIArgument(ArrayUtil.fromBoolean(fullUV), ArrayUtil.fromBoolean(computeUv), switchNum);
}
public Svd(SameDiff sd, SDVariable input, boolean fullUV, boolean computeUv){
this(sd, input, fullUV, computeUv, DEFAULT_SWITCHNUM);
}

View File

@ -21,6 +21,7 @@ import lombok.val;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.impl.transforms.custom.Svd;
import org.nd4j.linalg.api.ops.random.impl.GaussianDistribution;
import org.nd4j.linalg.api.rng.distribution.BaseDistribution;
import org.nd4j.linalg.factory.Nd4j;
@ -231,21 +232,20 @@ public class OrthogonalDistribution extends BaseDistribution {
val flatShape = new long[]{numRows, numCols};
val flatRng = Nd4j.getExecutioner().exec(new GaussianDistribution(Nd4j.createUninitialized(dtype, flatShape, Nd4j.order()), 0.0, 1.0), random);
long m = flatRng.rows();
long n = flatRng.columns();
val m = flatRng.rows();
val n = flatRng.columns();
val s = Nd4j.create(dtype, m < n ? m : n);
val u = m < n ? Nd4j.create(dtype, m, n) : Nd4j.create(dtype, m, m);
val u = Nd4j.create(dtype, m, m);
val v = Nd4j.create(dtype, new long[] {n, n}, 'f');
Nd4j.getBlasWrapper().lapack().gesvd(flatRng, s, u, v);
Nd4j.exec(new Svd(flatRng, true, s, u, v));
// FIXME: int cast
if (gains == null) {
if (u.rows() == numRows && u.columns() == numCols) {
return v.get(NDArrayIndex.interval(0, numRows), NDArrayIndex.interval(0, numCols)).mul(gain).reshape(shape);
} else {
if (u.rows() >= numRows && u.columns() >= numCols) {
return u.get(NDArrayIndex.interval(0, numRows), NDArrayIndex.interval(0, numCols)).mul(gain).reshape(shape);
} else {
return v.get(NDArrayIndex.interval(0, numRows), NDArrayIndex.interval(0, numCols)).mul(gain).reshape(shape);
}
} else {
throw new UnsupportedOperationException();

View File

@ -598,6 +598,10 @@ public class Nd4jCpu extends org.nd4j.nativeblas.Nd4jCpuHelper {
public native @Cast("bool") boolean isCPU();
public native int blasMajorVersion();
public native int blasMinorVersion();
public native int blasPatchVersion();
public native @StdVector Pair capabilities();
}
@ -12281,6 +12285,7 @@ public static final int TAD_THRESHOLD = TAD_THRESHOLD();
// #include <ops/declarable/headers/datatypes.h>
// #include <ops/declarable/headers/third_party.h>
// #include <ops/declarable/headers/tests.h>
// #include <ops/declarable/headers/kernels.h>
// #include <ops/declarable/headers/BarnesHutTsne.h>
// #include <dll.h>
// #include <helpers/shape.h>
@ -21502,23 +21507,6 @@ public static final int TAD_THRESHOLD = TAD_THRESHOLD();
public native ShapeList calculateOutputShape(ShapeList inputShape, @ByRef Context block);
}
// #endif
// #if NOT_EXCLUDED(OP_batchnorm_new)
@Namespace("nd4j::ops") public static class batchnorm_new extends DeclarableCustomOp {
static { Loader.load(); }
/** Pointer cast constructor. Invokes {@link Pointer#Pointer(Pointer)}. */
public batchnorm_new(Pointer p) { super(p); }
/** Native array allocator. Access with {@link Pointer#position(long)}. */
public batchnorm_new(long size) { super((Pointer)null); allocateArray(size); }
private native void allocateArray(long size);
@Override public batchnorm_new position(long position) {
return (batchnorm_new)super.position(position);
}
public batchnorm_new() { super((Pointer)null); allocate(); }
private native void allocate();
public native ShapeList calculateOutputShape(ShapeList inputShape, @ByRef Context block);
}
// #endif
/**
* back prop in batch normalization
@ -21542,8 +21530,8 @@ public static final int TAD_THRESHOLD = TAD_THRESHOLD();
* dL/dInput
* dL/dMean
* dL/dVariance
* dL/dGamma
* dL/dBeta
* dL/dGamma, optional
* dL/dBeta, optional
*/
// #if NOT_EXCLUDED(OP_batchnorm)
@Namespace("nd4j::ops") public static class batchnorm_bp extends DeclarableCustomOp {

View File

@ -1375,6 +1375,24 @@ public class RandomTests extends BaseNd4jTest {
log.info("Array: {}", array);
}
@Test
public void testOrthogonalDistribution2() {
val dist = new OrthogonalDistribution(1.0);
val array = dist.sample(new int[] {9, 6});
log.info("Array: {}", array);
}
@Test
public void testOrthogonalDistribution3() {
val dist = new OrthogonalDistribution(1.0);
val array = dist.sample(new int[] {9, 9});
log.info("Array: {}", array);
}
@Test
public void reproducabilityTest(){