use of org.nd4j.linalg.api.iter.NdIndexIterator in project nd4j by deeplearning4j.
the class Nd4jTestsC method testIsMax2of4d.
@Test
public void testIsMax2of4d() {
Nd4j.getRandom().setSeed(12345);
int[] s = new int[] { 2, 3, 4, 5 };
INDArray arr = Nd4j.rand(s);
// Test 0,1
INDArray exp = Nd4j.create(s);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
INDArray subset = arr.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(i), NDArrayIndex.point(j));
INDArray subsetExp = exp.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(i), NDArrayIndex.point(j));
assertArrayEquals(new int[] { 2, 3 }, subset.shape());
NdIndexIterator iter = new NdIndexIterator(2, 3);
int[] maxIdx = { 0, 0 };
double max = -Double.MAX_VALUE;
while (iter.hasNext()) {
int[] next = iter.next();
double d = subset.getDouble(next);
if (d > max) {
max = d;
maxIdx[0] = next[0];
maxIdx[1] = next[1];
}
}
subsetExp.putScalar(maxIdx, 1.0);
}
}
INDArray actC = Nd4j.getExecutioner().execAndReturn(new IsMax(arr.dup('c'), 0, 1));
INDArray actF = Nd4j.getExecutioner().execAndReturn(new IsMax(arr.dup('f'), 0, 1));
assertEquals(exp, actC);
assertEquals(exp, actF);
// Test 2,3
exp = Nd4j.create(s);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
INDArray subset = arr.get(NDArrayIndex.point(i), NDArrayIndex.point(j), NDArrayIndex.all(), NDArrayIndex.all());
INDArray subsetExp = exp.get(NDArrayIndex.point(i), NDArrayIndex.point(j), NDArrayIndex.all(), NDArrayIndex.all());
assertArrayEquals(new int[] { 4, 5 }, subset.shape());
NdIndexIterator iter = new NdIndexIterator(4, 5);
int[] maxIdx = { 0, 0 };
double max = -Double.MAX_VALUE;
while (iter.hasNext()) {
int[] next = iter.next();
double d = subset.getDouble(next);
if (d > max) {
max = d;
maxIdx[0] = next[0];
maxIdx[1] = next[1];
}
}
subsetExp.putScalar(maxIdx, 1.0);
}
}
actC = Nd4j.getExecutioner().execAndReturn(new IsMax(arr.dup('c'), 2, 3));
actF = Nd4j.getExecutioner().execAndReturn(new IsMax(arr.dup('f'), 2, 3));
assertEquals(exp, actC);
assertEquals(exp, actF);
}
use of org.nd4j.linalg.api.iter.NdIndexIterator in project nd4j by deeplearning4j.
the class Nd4jTestsC method toFlattenedViaIterator.
private static INDArray toFlattenedViaIterator(char order, INDArray... toFlatten) {
int length = 0;
for (INDArray i : toFlatten) length += i.length();
INDArray out = Nd4j.create(1, length);
int i = 0;
for (INDArray arr : toFlatten) {
NdIndexIterator iter = new NdIndexIterator(order, arr.shape());
while (iter.hasNext()) {
double next = arr.getDouble(iter.next());
out.putScalar(i++, next);
}
}
return out;
}
use of org.nd4j.linalg.api.iter.NdIndexIterator in project nd4j by deeplearning4j.
the class GradCheckUtil method checkGradients.
public static boolean checkGradients(SameDiff sd, double eps, double maxRelError, double minAbsError, boolean print, boolean exitOnFirstFailure) {
// Check data type:
if (Nd4j.dataType() != DataBuffer.Type.DOUBLE) {
throw new IllegalStateException("Data type must be set to double");
}
Set<String> fnOutputs = new HashSet<>();
for (DifferentialFunction f : sd.functions()) {
for (SDVariable s : f.outputVariables()) {
fnOutputs.add(s.getVarName());
}
}
// Check that all *input* SDVariables have arrays associated with them
for (SDVariable s : sd.variables()) {
if (fnOutputs.contains(s.getVarName())) {
// This is not an input to the graph
continue;
}
if (s.getArr() == null) {
throw new IllegalStateException("Variable \"" + s.getVarName() + "\" does not have array associated with it");
}
}
// Do forward pass, check that output is a scalar:
INDArray out = sd.execAndEndResult();
if (out.length() != 1) {
throw new IllegalStateException("Output variable is not a scalar - has shape " + Arrays.toString(out.shape()));
}
// TODO also check that all inputs are non-zero (otherwise: consider out = sum(x * y) with all x and y being 0
// in this case, gradients of x and y are all 0 too
sd.execBackwards();
Map<String, INDArray> grad = new HashMap<>();
for (SDVariable v : sd.variables()) {
if (fnOutputs.contains(v.getVarName())) {
// This is not an input to the graph
continue;
}
SDVariable g = sd.grad(v.getVarName());
if (g == null) {
throw new IllegalStateException("Null gradient variable for \"" + v.getVarName() + "\"");
}
INDArray ga = g.getArr();
if (ga == null) {
throw new IllegalStateException("Null gradient array encountered for variable: " + v.getVarName());
}
if (!Arrays.equals(v.getArr().shape(), g.getArr().shape())) {
throw new IllegalStateException("Gradient shape does not match variable shape for variable \"" + v.getVarName() + "\": shape " + Arrays.toString(v.getArr().shape()) + " vs. gradient shape " + Arrays.toString(ga.shape()));
}
grad.put(v.getVarName(), ga.dup());
}
// Validate gradients for each variable:
int totalNFailures = 0;
int totalCount = 0;
double maxError = 0.0;
for (SDVariable s : sd.variables()) {
if (fnOutputs.contains(s.getVarName())) {
// This is not an input to the graph
continue;
}
String name = s.getVarName();
INDArray a = s.getArr();
int n = a.length();
if (print) {
log.info("Starting test for variable \"{}\" with {} values", s.getVarName(), n);
}
NdIndexIterator iter = new NdIndexIterator('c', a.shape());
int i = 0;
while (iter.hasNext()) {
int[] idx = iter.next();
String strIdx = null;
if (print) {
strIdx = Arrays.toString(idx).replaceAll(" ", "");
}
totalCount++;
double orig = a.getDouble(idx);
a.putScalar(idx, orig + eps);
double scorePlus = sd.execAndEndResult().getDouble(0);
a.putScalar(idx, orig - eps);
double scoreMinus = sd.execAndEndResult().getDouble(0);
a.putScalar(idx, orig);
double numericalGrad = (scorePlus - scoreMinus) / (2 * eps);
double analyticGrad = grad.get(s.getVarName()).getDouble(idx);
if (Double.isInfinite(numericalGrad) || Double.isNaN(numericalGrad)) {
throw new IllegalStateException("Numerical gradient was " + numericalGrad + " for variable \"" + name + "\", parameter " + i + " of " + n + " (position: " + strIdx + ")");
}
if (Double.isInfinite(analyticGrad) || Double.isNaN(analyticGrad)) {
throw new IllegalStateException("Analytic (SameDiff) gradient was " + analyticGrad + " for variable \"" + name + "\", parameter " + i + " of " + n + " (position: " + strIdx + ")");
}
double relError;
if (numericalGrad == 0.0 && analyticGrad == 0.0) {
relError = 0.0;
} else {
relError = Math.abs(analyticGrad - numericalGrad) / (Math.abs(Math.abs(analyticGrad) + Math.abs(numericalGrad)));
}
if (relError > maxError)
maxError = relError;
if (relError > maxRelError || Double.isNaN(relError)) {
double absError = Math.abs(analyticGrad - numericalGrad);
if (absError < minAbsError) {
if (print) {
log.info("Param " + i + " (" + name + strIdx + ") passed: grad= " + analyticGrad + ", numericalGrad= " + numericalGrad + ", relError= " + relError + "; absolute error = " + absError + " < minAbsoluteError = " + minAbsError);
}
} else {
if (print)
log.info("Param " + i + " (" + name + strIdx + ") FAILED: grad= " + analyticGrad + ", numericalGrad= " + numericalGrad + ", relError= " + relError + ", absError=" + absError + ", scorePlus=" + scorePlus + ", scoreMinus= " + scoreMinus);
if (exitOnFirstFailure)
return false;
totalNFailures++;
}
} else if (print) {
log.info("Param " + i + " (" + name + strIdx + ") passed: grad= " + analyticGrad + ", numericalGrad= " + numericalGrad + ", relError= " + relError);
}
i++;
}
}
if (print) {
int nPass = totalCount - totalNFailures;
log.info("GradCheckUtil.checkGradients(): " + totalCount + " params checked, " + nPass + " passed, " + totalNFailures + " failed. Largest relative error = " + maxError);
}
return totalNFailures == 0;
}
use of org.nd4j.linalg.api.iter.NdIndexIterator in project nd4j by deeplearning4j.
the class LossFunctionGradientChecks method testLossFunctionGradients.
@Test
public void testLossFunctionGradients() {
INDArray[] labels = new INDArray[] { Nd4j.create(new double[] { 0, 1, 0 }), Nd4j.create(new double[] { 0, 1, 1 }), /*Nd4j.create(new double[][]{{1,0,0},{0,1,0},{0,0,1}}),
Nd4j.create(new double[]{1,2,1}),
Nd4j.create(new double[][]{{1,2,1},{0.1,1,0.5},{20,3,1}}),
Nd4j.create(new double[]{1,0,0}),
Nd4j.create(new double[][]{{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}),
Nd4j.create(new double[]{1,2,1}),
Nd4j.create(new double[][]{{101,21,110},{10.1,1,0.5},{200,30,0.001}}),
*/
Nd4j.create(new double[] { 1, 2, 1 }), Nd4j.create(new double[][] { { 101, 21, 110 }, { 10.1, 1, 0.5 }, { 200, 30, 0.001 } }), Nd4j.create(new double[] { 1, 2, 1 }), Nd4j.create(new double[][] { { 101, 21, 110 }, { 10.1, 1, 0.5 }, { 200, 30, 0.001 } }), Nd4j.create(new double[] { 1, 2, 1 }), Nd4j.create(new double[][] { { 101, 21, 110 }, { 10.1, 1, 0.5 }, { 200, 30, 0.001 } }), Nd4j.create(new double[] { 1, 2, 1 }), Nd4j.create(new double[][] { { 101, 21, 110 }, { 10.1, 1, 0.5 }, { 200, 30, 0.001 } }), // Nd4j.create(new double[][] {{-1,-1,1},{-1,1,1},{-1,1,1}}),
Nd4j.create(new double[][] { { -1, 1, -1 }, { 1, 1, -1 }, { -1, 1, 1 } }), Nd4j.create(new double[][] { { -1, 1, -1 }, { 1, 1, -1 }, { -1, 1, 1 } }), Nd4j.create(new double[][] { { 1, 0, 0 }, { 0, 1, 0 }, { 1, 0, 1 } }) // Nd4j.create(new double[][] {{10,1,3},{1,10,1},{1,2,5}}),
// Nd4j.create(new double[][] {{10,-1,3},{1,10,1},{1,2,-5}}),
};
INDArray[] preOut = new INDArray[] { Nd4j.rand(1, 3), Nd4j.rand(1, 3), /*
Nd4j.rand(3,3),
Nd4j.rand(1,3).add(5),
Nd4j.rand(3,3),
Nd4j.rand(1,3).add(5),
Nd4j.rand(4,4),*/
Nd4j.randn(1, 3), Nd4j.randn(3, 3).add(10), Nd4j.rand(1, 3), Nd4j.randn(3, 3).add(10), Nd4j.randn(1, 3), Nd4j.randn(3, 3).add(10), Nd4j.rand(1, 3), Nd4j.randn(3, 3).add(10), // adding a neg num makes some +ve/ some -ve
Nd4j.rand(3, 3).addi(-0.5), // adding a neg num makes some +ve/ some -ve
Nd4j.rand(3, 3).addi(-0.5), Nd4j.rand(3, 3).addi(-0.5) // Nd4j.rand(3,3),
// Nd4j.randn(3,3)
};
ILossFunction[] lossFn = new ILossFunction[] { new LossBinaryXENT(), new LossBinaryXENT(), /*new LossMCXENT(), new LossMCXENT(),
new LossMCXENT(),new LossMSE(), new LossMSE(), new LossKLD(), new LossKLD(), new LossMAE(), new LossMAE(),*/
new LossMAE(), new LossMAE(), new LossMSE(), new LossMSE(), new LossL1(), new LossL1(), new LossL2(), new LossL2(), new LossSquaredHinge(), new LossHinge(), new LossMultiLabel() // new LossPoisson(),
// new LossCosineProximity()
};
String[] activationFns = new String[] { "identity", "tanh", /*"softmax","tanh","identity","tanh",
"tanh","identity","identity","identity","identity",*/
"identity", "identity", "identity", "identity", "sigmoid", "relu", "sigmoid", "relu", "identity", "identity", "identity" // "relu",
// "identity"
};
for (int i = 0; i < labels.length; i++) {
// if (i != labels.length-2) continue;
int totalNFailures = 0;
ILossFunction lf = lossFn[i];
INDArray l = labels[i];
INDArray p = preOut[i];
String afn = activationFns[i];
System.out.printf("Starting test: %s, %s, input shape = %s\n", lf, afn, Arrays.toString(p.shape()));
INDArray grad = lf.computeGradient(l, p, activationInstance(afn), null);
NdIndexIterator iter = new NdIndexIterator(l.shape());
while (iter.hasNext()) {
int[] next = iter.next();
double before = p.getDouble(next);
p.putScalar(next, before + epsilon);
double scorePlus = lf.computeScore(l, p, activationInstance(afn), null, true);
p.putScalar(next, before - epsilon);
double scoreMinus = lf.computeScore(l, p, activationInstance(afn), null, true);
p.putScalar(next, before);
double scoreDelta = scorePlus - scoreMinus;
double numericalGradient = scoreDelta / (2 * epsilon);
// Analytic gradient method is before dividing by minibatch
double analyticGradient = grad.getDouble(next) / l.size(0);
double relError = Math.abs(analyticGradient - numericalGradient) * 100 / (Math.abs(numericalGradient));
if (analyticGradient == 0.0 && numericalGradient == 0.0)
// Edge case: i.e., RNNs with time series length of 1.0
relError = 0.0;
if (relError > maxRelError || Double.isNaN(relError)) {
System.out.println("Param " + i + " FAILED: grad= " + analyticGradient + ", numericalGrad= " + numericalGradient + ", relErrorPerc= " + relError + ", scorePlus=" + scorePlus + ", scoreMinus= " + scoreMinus);
totalNFailures++;
} else {
System.out.println("Param " + i + " passed: grad= " + analyticGradient + ", numericalGrad= " + numericalGradient + ", relError= " + relError + ", scorePlus=" + scorePlus + ", scoreMinus= " + scoreMinus);
}
// System.out.println("Param " + i + " passed: grad= " + analyticGradient + ", numericalGrad= " + numericalGradient
// + ", relError= " + relError + ", scorePlus="+scorePlus+", scoreMinus= " + scoreMinus );
}
if (totalNFailures > 0)
System.out.println("Gradient check failed for loss function " + lf + "; total num failures = " + totalNFailures);
System.out.println("DONE");
}
}
use of org.nd4j.linalg.api.iter.NdIndexIterator in project nd4j by deeplearning4j.
the class ShapeTestsC method testPutScalar.
@Test
public void testPutScalar() {
// Check that the various putScalar methods have the same result...
int[][] shapes = new int[][] { { 3, 4 }, { 1, 4 }, { 3, 1 }, { 3, 4, 5 }, { 1, 4, 5 }, { 3, 1, 5 }, { 3, 4, 1 }, { 1, 1, 5 }, { 3, 4, 5, 6 }, { 1, 4, 5, 6 }, { 3, 1, 5, 6 }, { 3, 4, 1, 6 }, { 3, 4, 5, 1 }, { 1, 1, 5, 6 }, { 3, 1, 1, 6 }, { 3, 1, 1, 1 } };
for (int[] shape : shapes) {
int rank = shape.length;
NdIndexIterator iter = new NdIndexIterator(shape);
INDArray firstC = Nd4j.create(shape, 'c');
INDArray firstF = Nd4j.create(shape, 'f');
INDArray secondC = Nd4j.create(shape, 'c');
INDArray secondF = Nd4j.create(shape, 'f');
int i = 0;
while (iter.hasNext()) {
int[] currIdx = iter.next();
firstC.putScalar(currIdx, i);
firstF.putScalar(currIdx, i);
switch(rank) {
case 2:
secondC.putScalar(currIdx[0], currIdx[1], i);
secondF.putScalar(currIdx[0], currIdx[1], i);
break;
case 3:
secondC.putScalar(currIdx[0], currIdx[1], currIdx[2], i);
secondF.putScalar(currIdx[0], currIdx[1], currIdx[2], i);
break;
case 4:
secondC.putScalar(currIdx[0], currIdx[1], currIdx[2], currIdx[3], i);
secondF.putScalar(currIdx[0], currIdx[1], currIdx[2], currIdx[3], i);
break;
default:
throw new RuntimeException();
}
i++;
}
assertEquals(firstC, firstF);
assertEquals(firstC, secondC);
assertEquals(firstC, secondF);
}
}
Aggregations