Java Code Examples for org.nd4j.autodiff.samediff.SDVariable#getArr()
The following examples show how to use
org.nd4j.autodiff.samediff.SDVariable#getArr() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseOp.java From nd4j with Apache License 2.0 | 6 votes |
@Override public void setX(INDArray x) { if (x == null) { if (args() != null && args().length >= 1) { DifferentialFunction firstArg = args()[0]; if (firstArg instanceof SDVariable) { SDVariable sdVariable = (SDVariable) firstArg; if (sdVariable.getArr() != null) this.x = sdVariable.getArr(); } } else throw new ND4JIllegalStateException("Unable to set null array for x. Also unable to infer from differential function arguments"); } else this.x = x; numProcessed = 0; }
Example 2
Source File: BaseOp.java From nd4j with Apache License 2.0 | 6 votes |
@Override public void setZ(INDArray z) { if (z == null) { SDVariable getResult = sameDiff.getVariable(zVertexId); if (getResult != null) { if (getResult.getArr() != null) this.z = getResult.getArr(); else if(sameDiff.getShapeForVarName(getResult.getVarName()) != null) { val shape = sameDiff.getShapeForVarName(getResult.getVarName()); sameDiff.putArrayForVarName(getResult.getVarName(),getResult.getWeightInitScheme().create(shape)); } else throw new ND4JIllegalStateException("Unable to set null array for z. Also unable to infer from differential function arguments"); } else throw new ND4JIllegalStateException("Unable to set null array for z. Also unable to infer from differential function arguments"); } else this.z = z; numProcessed = 0; }
Example 3
Source File: BaseOp.java From nd4j with Apache License 2.0 | 6 votes |
@Override public void setY(INDArray y) { if (y == null) { if (args() != null && args().length > 1) { DifferentialFunction firstArg = args()[1]; if (firstArg instanceof SDVariable) { SDVariable sdVariable = (SDVariable) firstArg; if (sdVariable.getArr() != null) this.y = sdVariable.getArr(); } } else throw new ND4JIllegalStateException("Unable to set null array for y. Also unable to infer from differential function arguments"); } else this.y = y; numProcessed = 0; }
Example 4
Source File: IntegrationTestRunner.java From deeplearning4j with Apache License 2.0 | 6 votes |
public static void assertSameDiffEquals(SameDiff sd1, SameDiff sd2){ assertEquals(sd1.variableMap().keySet(), sd2.variableMap().keySet()); assertEquals(sd1.getOps().keySet(), sd2.getOps().keySet()); assertEquals(sd1.inputs(), sd2.inputs()); //Check constant and variable arrays: for(SDVariable v : sd1.variables()){ String n = v.name(); assertEquals(n, v.getVariableType(), sd2.getVariable(n).getVariableType()); if(v.isConstant() || v.getVariableType() == VariableType.VARIABLE){ INDArray a1 = v.getArr(); INDArray a2 = sd2.getVariable(n).getArr(); assertEquals(n, a1, a2); } } //Check ops: for(SameDiffOp o : sd1.getOps().values()){ SameDiffOp o2 = sd2.getOps().get(o.getName()); assertEquals(o.getOp().getClass(), o2.getOp().getClass()); } }
Example 5
Source File: GradCheckTransforms.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testCross() { INDArray a = Nd4j.create(new float[]{4, 2, 1}, new int[]{1, 3}); INDArray b = Nd4j.create(new float[]{1, 3, 4}, new int[]{1, 3}); INDArray expOut = Nd4j.create(1, 3); DynamicCustomOp op = DynamicCustomOp.builder("cross").addInputs(a, b).addOutputs(expOut).build(); Nd4j.getExecutioner().exec(op); SameDiff sd = SameDiff.create(); SDVariable sdA = sd.var("a", expOut.shape()); SDVariable sdB = sd.var("b", expOut.shape()); sd.associateArrayWithVariable(a, sdA); sd.associateArrayWithVariable(b, sdB); SDVariable t = sd.cross(sdA, sdB); SDVariable loss = sd.mean("loss", t); sd.exec(); INDArray out = t.getArr(); if (!expOut.equals(out)) { log.info("batch to space failed on forward"); } try { GradCheckUtil.checkGradients(sd); } catch (Exception e) { e.printStackTrace(); } }
Example 6
Source File: GradCheckTransforms.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testSpaceToDepth() { Nd4j.getRandom().setSeed(1337); int miniBatch = 128; int blockSize = 4; String dataFormat = "NHWC"; int isNHWC = dataFormat.equals("NHWC") ? 1 : 0; int[] inputShape = new int[]{miniBatch, 2 * blockSize, 2 * blockSize, 1}; INDArray input = Nd4j.randn(inputShape); SameDiff sd = SameDiff.create(); SDVariable sdInput = sd.var("in", inputShape); INDArray expOut = Nd4j.create(miniBatch, 2, 2, blockSize * blockSize); DynamicCustomOp op = DynamicCustomOp.builder("space_to_depth") .addInputs(input) .addIntegerArguments(blockSize, isNHWC) .addOutputs(expOut).build(); Nd4j.getExecutioner().exec(op); sd.associateArrayWithVariable(input, sdInput); SDVariable t = sd.spaceToDepth(sdInput, blockSize, dataFormat); SDVariable loss = sd.mean("loss", t); sd.exec(); INDArray out = t.getArr(); if (!expOut.equals(out)) { log.info("depth to space failed on forward"); } try { GradCheckUtil.checkGradients(sd); } catch (Exception e) { e.printStackTrace(); } }
Example 7
Source File: GradCheckTransforms.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testDepthToSpace() { Nd4j.getRandom().setSeed(1337); int miniBatch = 128; int blockSize = 4; String dataFormat = "NHWC"; int isNHWC = dataFormat.equals("NHWC") ? 1 : 0; int[] inputShape = new int[]{miniBatch, 2, 2, blockSize * blockSize}; INDArray input = Nd4j.randn(inputShape); SameDiff sd = SameDiff.create(); SDVariable sdInput = sd.var("in", inputShape); INDArray expOut = Nd4j.create(miniBatch, 2 * blockSize, 2 * blockSize, 1); DynamicCustomOp op = DynamicCustomOp.builder("depth_to_space") .addInputs(input) .addIntegerArguments(blockSize, isNHWC) .addOutputs(expOut).build(); Nd4j.getExecutioner().exec(op); sd.associateArrayWithVariable(input, sdInput); SDVariable t = sd.depthToSpace(sdInput, blockSize, dataFormat); SDVariable loss = sd.mean("loss", t); sd.exec(); INDArray out = t.getArr(); if (!expOut.equals(out)) { log.info("depth to space failed on forward"); } try { GradCheckUtil.checkGradients(sd); } catch (Exception e) { e.printStackTrace(); } }
Example 8
Source File: GradCheckTransforms.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testDynamicStitch() { SameDiff sd = SameDiff.create(); INDArray ia = Nd4j.create(new float[]{5, 1, 3}, new int[]{1, 3}); INDArray ib = Nd4j.create(new float[]{7, 2, 4}, new int[]{1, 3}); INDArray indexA = Nd4j.create(new float[]{0, 1, 4}, new int[]{1, 3}); INDArray indexB = Nd4j.create(new float[]{2, 3, 5}, new int[]{1, 3}); INDArray expOut = Nd4j.create(new int[]{1, 6}); DynamicCustomOp dynamicStitch = DynamicCustomOp.builder("dynamic_stitch") .addInputs(indexA, indexB, ia, ib) .addOutputs(expOut).build(); Nd4j.getExecutioner().exec(dynamicStitch); SDVariable in1 = sd.var("in1", new int[]{1, 3}); SDVariable in2 = sd.var("in2", new int[]{1, 3}); SDVariable index1 = sd.var("index1", new int[]{1, 3}); SDVariable index2 = sd.var("index2", new int[]{1, 3}); sd.associateArrayWithVariable(ia, in1); sd.associateArrayWithVariable(ib, in2); sd.associateArrayWithVariable(indexA, index1); sd.associateArrayWithVariable(indexB, index2); SDVariable t = sd.dynamicStitch(new SDVariable[]{index1, index2}, new SDVariable[]{in1, in2}); SDVariable loss = sd.mean("loss", t); sd.exec(); INDArray out = t.getArr(); if (!expOut.equals(out)) { log.error("forward failed"); } }
Example 9
Source File: GradCheckTransforms.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testDiag() { SameDiff sd = SameDiff.create(); INDArray ia = Nd4j.create(new float[]{4, 2}); SDVariable in = sd.var("in", new int[]{1, 2}); INDArray expOut = Nd4j.create(new int[]{2, 2}); DynamicCustomOp diag = DynamicCustomOp.builder("diag").addInputs(ia).addOutputs(expOut).build(); Nd4j.getExecutioner().exec(diag); SDVariable t = sd.diag(in); SDVariable loss = sd.max("loss", t, 0, 1); sd.associateArrayWithVariable(ia, in); sd.exec(); INDArray out = t.getArr(); if (!expOut.equals(out)) { log.info("forward failed"); } try { GradCheckUtil.checkGradients(sd); } catch (Exception e) { e.printStackTrace(); } }
Example 10
Source File: DifferentialFunction.java From nd4j with Apache License 2.0 | 5 votes |
@JsonIgnore private INDArray getZ() { if(isInPlace()) return getX(); SDVariable opId = outputVariables()[0]; INDArray ret = opId.getArr(); return ret; }
Example 11
Source File: DynamicCustomOp.java From nd4j with Apache License 2.0 | 5 votes |
private INDArray attemptToGetOrCreateArrForVar(SDVariable var, long[] currShape) { INDArray arr = null; if (Shape.isPlaceholderShape(var.getShape())) { if (var.getShape() == null) { val shape = calculateOutputShape(); if (!shape.isEmpty()) { if (currShape != null && !Shape.isPlaceholderShape(currShape)) { sameDiff.putShapeForVarName(var.getVarName(), currShape); arr = var.storeAndAllocateNewArray(); } } else arr = null; } } else if (sameDiff.getArrForVarName(var.getVarName()) == null) { if (var.getShape() != null) arr = var.storeAndAllocateNewArray(); } else { arr = var.getArr(); } if (arr != null) { sameDiff.associateArrayWithVariable(arr, var); addOutputArgument(arr); } return arr; }
Example 12
Source File: GradCheckMisc.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testSqueezeGradient() { val origShape = new long[]{3, 4, 5}; for (int i = 0; i < 3; i++) { val shape = origShape.clone(); shape[i] = 1; for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, shape)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable squeeze = sd.f().squeeze(in, i); //Using stdev here: mean/sum would backprop the same gradient for each input... SDVariable stdev = sd.standardDeviation("out", squeeze, true); long[] expShapePostSqueeze; switch (i) { case 0: expShapePostSqueeze = new long[]{4, 5}; break; case 1: expShapePostSqueeze = new long[]{3, 5}; break; case 2: expShapePostSqueeze = new long[]{3, 4}; break; default: throw new RuntimeException(); } sd.execAndEndResult(); INDArray squeezed = squeeze.getArr(); assertArrayEquals(expShapePostSqueeze, squeezed.shape()); INDArray out = sd.execAndEndResult(); INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE); assertEquals(expOut, out); String msg = "squeezeDim=" + i + ", source=" + p.getSecond(); boolean ok = GradCheckUtil.checkGradients(sd); assertTrue(msg, ok); } } }
Example 13
Source File: GradCheckTransforms.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testBatchToSpace() { Nd4j.getRandom().setSeed(1337); int miniBatch = 4; int[] inputShape = new int[]{miniBatch, 1, 1, 1}; int M = 2; int[] blockShape = new int[]{M, 1}; int[] cropShape = new int[]{M, 2}; INDArray input = Nd4j.randn(inputShape); INDArray blocks = Nd4j.create(new float[]{2, 2}, blockShape); INDArray crops = Nd4j.create(new float[]{0, 0, 0, 0}, cropShape); SameDiff sd = SameDiff.create(); SDVariable sdInput = sd.var("in", inputShape); INDArray expOut = Nd4j.create(1, 2, 2, 1); DynamicCustomOp op = DynamicCustomOp.builder("batch_to_space") .addInputs(input, blocks, crops) .addOutputs(expOut).build(); Nd4j.getExecutioner().exec(op); sd.associateArrayWithVariable(input, sdInput); SDVariable t = sd.batchToSpace(sdInput, new int[]{2, 2}, new int[][]{{0, 0}, {0, 0}}); SDVariable loss = sd.mean("loss", t); sd.exec(); INDArray out = t.getArr(); if (!expOut.equals(out)) { log.info("batch to space failed on forward"); } try { GradCheckUtil.checkGradients(sd); } catch (Exception e) { e.printStackTrace(); } }
Example 14
Source File: GradCheckTransforms.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testSpaceToBatch() { Nd4j.getRandom().setSeed(7331); int miniBatch = 4; int[] inputShape = new int[]{1, 2, 2, 1}; int M = 2; int[] blockShape = new int[]{M, 1}; int[] paddingShape = new int[]{M, 2}; INDArray input = Nd4j.randn(inputShape); INDArray blocks = Nd4j.create(new float[]{2, 2}, blockShape); INDArray padding = Nd4j.create(new float[]{0, 0, 0, 0}, paddingShape); SameDiff sd = SameDiff.create(); SDVariable sdInput = sd.var("in", inputShape); INDArray expOut = Nd4j.create(miniBatch, 1, 1, 1); DynamicCustomOp op = DynamicCustomOp.builder("space_to_batch") .addInputs(input, blocks, padding) .addOutputs(expOut).build(); Nd4j.getExecutioner().exec(op); sd.associateArrayWithVariable(input, sdInput); SDVariable t = sd.spaceToBatch(sdInput, new int[]{2, 2}, new int[][]{{0, 0}, {0, 0}}); SDVariable loss = sd.mean("loss", t); sd.exec(); INDArray out = t.getArr(); if (!expOut.equals(out)) { log.info("space to batch failed on forward"); } try { GradCheckUtil.checkGradients(sd); } catch (Exception e) { e.printStackTrace(); } }
Example 15
Source File: Fill.java From nd4j with Apache License 2.0 | 4 votes |
public Fill(SameDiff sameDiff, SDVariable shape, double value) { super(null,sameDiff, new SDVariable[] {shape}, false); this.value = value; val shp = shape.getArr(); addArgs(); }
Example 16
Source File: MiscOpValidation.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testMulGradient() { INDArray arr1 = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2); INDArray arr2 = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2); INDArray gradAssertion = Nd4j.ones(arr1.shape()); INDArray scalar = Nd4j.scalar(1.0); INDArray aGradAssertion = Nd4j.create(new double[][]{ {1, 4}, {9, 16} }); INDArray cGradAssertion = Nd4j.create(new double[][]{ {1, 2}, {3, 4} }); INDArray wGradAssertion = Nd4j.create(new double[][]{ {2, 8}, {18, 32} }); INDArray dGradAssertion = Nd4j.ones(2, 2); SameDiff sameDiff = SameDiff.create(); SDVariable sdVariable = sameDiff.var("a", arr1); SDVariable sdVariable1 = sameDiff.var("w", arr2); SDVariable varMulPre = sdVariable.mul("c", sdVariable1); SDVariable varMul = varMulPre.mul("d", sdVariable1); SDVariable sum = sameDiff.sum("ret", varMul, Integer.MAX_VALUE); Map<String,INDArray> m = sameDiff.outputAll(null); Map<String,INDArray> gm = sameDiff.calculateGradients(null, m.keySet()); SDVariable finalResult = sameDiff.grad(sum.name()); SDVariable cGrad = sameDiff.grad(varMulPre.name()); SDVariable mulGradResult = sameDiff.grad(varMul.name()); SDVariable aGrad = sameDiff.grad(sdVariable.name()); SDVariable wGrad = sameDiff.grad(sdVariable1.name()); SDVariable dGrad = sameDiff.grad(varMul.name()); INDArray scalarGradTest = gm.get(sum.name()); assertEquals(scalar, scalarGradTest); INDArray gradTest = mulGradResult.getArr(); assertEquals(gradAssertion, gradTest); INDArray aGradTest = aGrad.getArr(); assertEquals(aGradAssertion, aGradTest); INDArray cGradTest = cGrad.getArr(); assertEquals(cGradAssertion, cGradTest); INDArray wGradTest = wGrad.getArr(); assertEquals(wGradAssertion, wGradTest); INDArray dGradTest = dGrad.getArr(); assertEquals(dGradAssertion, dGradTest); }