Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#muliRowVector()
The following examples show how to use
org.nd4j.linalg.api.ndarray.INDArray#muliRowVector() .
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: CudaBroadcastTests.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testPinnedMulRowVector() throws Exception { // simple way to stop test if we're not on CUDA backend here assertEquals("JcublasLevel1", Nd4j.getBlasWrapper().level1().getClass().getSimpleName()); INDArray array1 = Nd4j.zeros(15,15); array1.putRow(0, Nd4j.create(new float[]{2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f})); array1.putRow(1, Nd4j.create(new float[]{2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f})); INDArray array2 = Nd4j.create(new float[]{2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f}); array1.muliRowVector(array2); System.out.println("Array1: " + array1); System.out.println("Array2: " + array2); assertEquals(4.0f, array1.getRow(0).getFloat(0), 0.01); }
Example 2
Source File: LossL2.java From deeplearning4j with Apache License 2.0 | 6 votes |
protected INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if(!labels.equalShapes(preOutput)){ Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape()); } labels = labels.castTo(preOutput.dataType()); //No-op if already correct dtype INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray scoreArr = output.rsubi(labels); scoreArr = scoreArr.muli(scoreArr); //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } scoreArr.muliRowVector(weights.castTo(scoreArr.dataType())); } //Loss function with masking if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
Example 3
Source File: LossL2.java From nd4j with Apache License 2.0 | 6 votes |
protected INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray scoreArr = output.rsubi(labels); scoreArr = scoreArr.muli(scoreArr); //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } scoreArr.muliRowVector(weights); } //Loss function with masking if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
Example 4
Source File: MiscOpValidation.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testClipByNorm0(){ //Expected: if array.norm2(0) is less than 1.0, not modified //Otherwise: array.tad(x,1) = array.tad(x,1) * 1.0 / array.tad(x,1).norm2() Nd4j.getRandom().setSeed(12345); INDArray arr = Nd4j.rand(5,4); INDArray norm2_0 = arr.norm2(0); arr.diviRowVector(norm2_0); INDArray initNorm2 = Nd4j.create(new double[]{2.2, 2.1, 2.0, 1.9}, new int[]{4}); //Initial norm2s along dimension 0 arr.muliRowVector(initNorm2); norm2_0 = arr.norm2(0); assertEquals(initNorm2, norm2_0); INDArray out = Nd4j.create(arr.shape()); INDArray norm2_0b = out.norm2(0); INDArray expNorm = Nd4j.create(new double[]{2.0, 2.0, 2.0, 1.9}, new int[]{1, 4}); //Post clip norm2s along dimension 0 INDArray exp = arr.divRowVector(norm2_0b).muliRowVector(expNorm); OpTestCase op = new OpTestCase(//Clip to norm2 of 2.0, along dimension 0 new ClipByNorm(arr, out, 2.0, 0)) .expectedOutput(0, exp); assertNull(OpValidation.validate(op)); }
Example 5
Source File: Nd4jMatrix.java From jstarcraft-ai with Apache License 2.0 | 5 votes |
@Override public MathMatrix multiplyRowVector(MathVector vector) { if (vector instanceof Nd4jVector) { Nd4jEnvironmentThread thread = EnvironmentThread.getThread(Nd4jEnvironmentThread.class); try (MemoryWorkspace workspace = thread.getSpace()) { INDArray thisArray = this.getArray(); INDArray thatArray = Nd4jVector.class.cast(vector).getArray(); thisArray.muliRowVector(thatArray); return this; } } else { return MathMatrix.super.multiplyRowVector(vector); } }
Example 6
Source File: LossMSLE.java From nd4j with Apache License 2.0 | 5 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } //INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup())); INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray p1 = output.add(1.0); INDArray dlda = p1.rdiv(2.0 / labels.size(1)); INDArray logRatio = Transforms.log(p1.divi(labels.add(1.0)), false); dlda.muli(logRatio); if (weights != null) { dlda.muliRowVector(weights); } if (mask != null && LossUtil.isPerOutputMasking(dlda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - though buy us a tiny bit of performance LossUtil.applyMask(dlda, mask); } //dL/dz INDArray gradients = activationFn.backprop(preOutput, dlda).getFirst(); //TODO activation functions with weights if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
Example 7
Source File: LossMAPE.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if(!labels.equalShapes(preOutput)){ Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape()); } labels = labels.castTo(preOutput.dataType()); //No-op if already correct dtype INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray actSubPredicted = labels.sub(output); INDArray dLda = Nd4j.getExecutioner().exec(new Sign(actSubPredicted)); INDArray absLabels = Nd4j.getExecutioner().exec(new Abs(labels.dup())); dLda.divi(absLabels).muli(-100.0 / labels.size(1)); //Weighted loss function if (weights != null) { dLda.muliRowVector(weights.castTo(dLda.dataType())); } if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray gradient = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation functions with params if (mask != null) { LossUtil.applyMask(gradient, mask); } return gradient; }
Example 8
Source File: LossMAPE.java From nd4j with Apache License 2.0 | 5 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray actSubPredicted = labels.sub(output); INDArray dLda = Nd4j.getExecutioner().execAndReturn(new Sign(actSubPredicted)); INDArray absLabels = Nd4j.getExecutioner().execAndReturn(new Abs(labels.dup())); dLda.divi(absLabels).muli(-100.0 / labels.size(1)); //Weighted loss function if (weights != null) { dLda.muliRowVector(weights); } if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray gradient = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation functions with params if (mask != null) { LossUtil.applyMask(gradient, mask); } return gradient; }
Example 9
Source File: PCA.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * Generates a set of <i>count</i> random samples with the same variance and mean and eigenvector/values * as the data set used to initialize the PCA object, with same number of features <i>N</i>. * @param count The number of samples to generate * @return A matrix of size <i>count</i> rows by <i>N</i> columns */ public INDArray generateGaussianSamples(long count) { INDArray samples = Nd4j.randn(new long[] {count, eigenvalues.columns()}); INDArray factors = Transforms.pow(eigenvalues, -0.5, true); samples.muliRowVector(factors); return Nd4j.tensorMmul(eigenvectors, samples, new int[][] {{1}, {1}}).transposei().addiRowVector(mean); }
Example 10
Source File: StandardizeStrategy.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * Denormalize a data array * * @param array the data to denormalize * @param stats statistics of the data population */ @Override public void revert(INDArray array, INDArray maskArray, DistributionStats stats) { if (array.rank() <= 2) { array.muliRowVector(filteredStd(stats)); array.addiRowVector(stats.getMean()); } else { Nd4j.getExecutioner().execAndReturn(new BroadcastMulOp(array, filteredStd(stats).castTo(array.dataType()), array, 1)); Nd4j.getExecutioner().execAndReturn(new BroadcastAddOp(array, stats.getMean().castTo(array.dataType()), array, 1)); } if (maskArray != null) { DataSetUtil.setMaskedValuesToZero(array, maskArray); } }
Example 11
Source File: LossMSLE.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if(!labels.equalShapes(preOutput)){ Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape()); } labels = labels.castTo(preOutput.dataType()); //No-op if already correct dtype INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray p1 = output.add(1.0); INDArray dlda = p1.rdiv(2.0 / labels.size(1)); INDArray logRatio = Transforms.log(p1.divi(labels.add(1.0)), false); dlda.muli(logRatio); if (weights != null) { dlda.muliRowVector(weights.castTo(dlda.dataType())); } if (mask != null && LossUtil.isPerOutputMasking(dlda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - though buy us a tiny bit of performance LossUtil.applyMask(dlda, mask); } //dL/dz INDArray gradients = activationFn.backprop(preOutput, dlda).getFirst(); //TODO activation functions with weights if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
Example 12
Source File: LossMCXENT.java From deeplearning4j with Apache License 2.0 | 5 votes |
protected INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if(!labels.equalShapes(preOutput)){ Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape()); } labels = labels.castTo(preOutput.dataType()); //No-op if already correct dtype INDArray output = activationFn.getActivation(preOutput.dup(), true); if(activationFn instanceof ActivationSoftmax && softmaxClipEps > 0.0){ BooleanIndexing.replaceWhere(output, softmaxClipEps, Conditions.lessThan(softmaxClipEps)); BooleanIndexing.replaceWhere(output, 1.0-softmaxClipEps, Conditions.greaterThan(1.0-softmaxClipEps)); } INDArray scoreArr = Transforms.log(output, false).muli(labels); //Weighted loss function if (weights != null) { if (weights.length() != scoreArr.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + preOutput.size(1)); } scoreArr.muliRowVector(weights.castTo(scoreArr.dataType())); } if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
Example 13
Source File: LossMCXENT.java From nd4j with Apache License 2.0 | 5 votes |
private INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray output = activationFn.getActivation(preOutput.dup(), true); if(activationFn instanceof ActivationSoftmax && softmaxClipEps > 0.0){ BooleanIndexing.replaceWhere(output, softmaxClipEps, Conditions.lessThan(softmaxClipEps)); BooleanIndexing.replaceWhere(output, 1.0-softmaxClipEps, Conditions.greaterThan(1.0-softmaxClipEps)); } INDArray scoreArr = Transforms.log(output, false).muli(labels); //Weighted loss function if (weights != null) { if (weights.length() != scoreArr.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + preOutput.size(1)); } scoreArr.muliRowVector(weights); } if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
Example 14
Source File: PCA.java From nd4j with Apache License 2.0 | 5 votes |
/** * Generates a set of <i>count</i> random samples with the same variance and mean and eigenvector/values * as the data set used to initialize the PCA object, with same number of features <i>N</i>. * @param count The number of samples to generate * @return A matrix of size <i>count</i> rows by <i>N</i> columns */ public INDArray generateGaussianSamples(long count) { INDArray samples = Nd4j.randn(new long[] {count, eigenvalues.columns()}); INDArray factors = Transforms.pow(eigenvalues, -0.5, true); samples.muliRowVector(factors); return Nd4j.tensorMmul(eigenvectors, samples, new int[][] {{1}, {1}}).transposei().addiRowVector(mean); }
Example 15
Source File: StandardizeStrategy.java From nd4j with Apache License 2.0 | 5 votes |
/** * Denormalize a data array * * @param array the data to denormalize * @param stats statistics of the data population */ @Override public void revert(INDArray array, INDArray maskArray, DistributionStats stats) { if (array.rank() <= 2) { array.muliRowVector(filteredStd(stats)); array.addiRowVector(stats.getMean()); } else { Nd4j.getExecutioner().execAndReturn(new BroadcastMulOp(array, filteredStd(stats), array, 1)); Nd4j.getExecutioner().execAndReturn(new BroadcastAddOp(array, stats.getMean(), array, 1)); } if (maskArray != null) { DataSetUtil.setMaskedValuesToZero(array, maskArray); } }
Example 16
Source File: LossL1.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if(!labels.equalShapes(preOutput)){ Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape()); } labels = labels.castTo(preOutput.dataType()); //No-op if already correct dtype INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray outSubLabels = output.sub(labels); INDArray dLda = Nd4j.getExecutioner().exec(new Sign(outSubLabels)); if (weights != null) { dLda.muliRowVector(weights.castTo(dLda.dataType())); } if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } //dL/dz INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation function param gradients if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
Example 17
Source File: LossBinaryXENT.java From nd4j with Apache License 2.0 | 4 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray output = activationFn.getActivation(preOutput.dup(), true); if (clipEps > 0.0) { CustomOp op = DynamicCustomOp.builder("clipbyvalue") .addInputs(output) .callInplace(true) .addFloatingPointArguments(clipEps, 1.0-clipEps) .build(); Nd4j.getExecutioner().exec(op); } INDArray numerator = output.sub(labels); INDArray denominator = Nd4j.getExecutioner().execAndReturn(new TimesOneMinus(output)); // output * (1-output) INDArray dLda = numerator.divi(denominator); if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray grad = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation functions with weights //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } grad.muliRowVector(weights); } if (mask != null) { LossUtil.applyMask(grad, mask); } return grad; }
Example 18
Source File: LossBinaryXENT.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if(!labels.equalShapes(preOutput)){ Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape()); } labels = labels.castTo(preOutput.dataType()); //No-op if already correct dtype INDArray output = activationFn.getActivation(preOutput.dup(), true); if (clipEps > 0.0) { CustomOp op = DynamicCustomOp.builder("clipbyvalue") .addInputs(output) .callInplace(true) .addFloatingPointArguments(clipEps, 1.0-clipEps) .build(); Nd4j.getExecutioner().execAndReturn(op); } INDArray numerator = output.sub(labels); INDArray denominator = Nd4j.getExecutioner().exec(new TimesOneMinus(output)); // output * (1-output) INDArray dLda = numerator.divi(denominator); if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray grad = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation functions with weights //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } grad.muliRowVector(weights.castTo(grad.dataType())); } if (mask != null) { LossUtil.applyMask(grad, mask); } return grad; }
Example 19
Source File: LossMCXENT.java From nd4j with Apache License 2.0 | 4 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray grad; //INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup())); INDArray output = activationFn.getActivation(preOutput.dup(), true); if (activationFn instanceof ActivationSoftmax) { if (mask != null && LossUtil.isPerOutputMasking(output, mask)) { throw new UnsupportedOperationException("Per output masking for MCXENT + softmax: not supported"); } //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } INDArray temp = labels.mulRowVector(weights); INDArray col = temp.sum(1); grad = output.mulColumnVector(col).sub(temp); } else { grad = output.subi(labels); } } else { INDArray dLda = output.rdivi(labels).negi(); grad = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation function with weights //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } grad.muliRowVector(weights); } } //Loss function with masking if (mask != null) { LossUtil.applyMask(grad, mask); } return grad; }
Example 20
Source File: ReductionBpOpValidation.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testProdAlongDimensionBP() { //dL/dIn_i = dL/dOut * dOut/dIn_i // = dL/dOut * d(prod(in))/dIn_i // = dL/dOut * (prod(in) / in_i) for (boolean keepDims : new boolean[]{false, true}) { long[] reducedShape_0 = (keepDims ? new long[]{1, 4} : new long[]{4}); INDArray preReduceInput = Nd4j.linspace(1, 12, 12).reshape(3, 4); INDArray prod_0 = preReduceInput.prod(0); INDArray dLdOut_0 = Nd4j.create(new double[]{1, 2, 3, 4}, reducedShape_0); INDArray dLdInExpected_0 = Nd4j.create(3, 4); for (int i = 0; i < 3; i++) { dLdInExpected_0.putRow(i, prod_0); } dLdInExpected_0.divi(preReduceInput); //Currently: prod(in)/in_i (along dim 0) dLdInExpected_0.muliRowVector(dLdOut_0); //System.out.println(dLdInExpected_0); /* [[ 45.0000, 120.0000, 231.0000, 384.0000], [ 9.0000, 40.0000, 99.0000, 192.0000], [ 5.0000, 24.0000, 63.0000, 128.0000]] */ INDArray dLdIn = Nd4j.createUninitialized(3, 4); String err = OpValidation.validate(new OpTestCase(new ProdBp(preReduceInput, dLdOut_0, dLdIn, keepDims, 0)) .expectedOutput(0, dLdInExpected_0)); assertNull(err); long[] reducedShape_1 = (keepDims ? new long[]{3, 1} : new long[]{3}); INDArray dLdOut_1 = Nd4j.create(new double[]{1, 2, 3}, reducedShape_1); INDArray prod_1 = preReduceInput.prod(1); INDArray dLdInExpected_1 = Nd4j.create(3, 4); for (int i = 0; i < 4; i++) { dLdInExpected_1.putColumn(i, prod_1); } dLdInExpected_1.divi(preReduceInput); dLdInExpected_1.muliColumnVector(dLdOut_1.reshape(3, 1)); //Reshape is a hack around https://github.com/deeplearning4j/deeplearning4j/issues/5530 //System.out.println(dLdInExpected_1); /* [[ 24.0000, 12.0000, 8.0000, 6.0000], [ 672.0000, 560.0000, 480.0000, 420.0000], [ 3960.0000, 3564.0000, 3240.0000, 2970.0000]] */ dLdIn = Nd4j.createUninitialized(3, 4); err = OpValidation.validate(new OpTestCase(new ProdBp(preReduceInput, dLdOut_1, dLdIn, keepDims, 1)) .expectedOutput(0, dLdInExpected_1)); assertNull(err, err); } }