org.nd4j.linalg.indexing.NDArrayIndex Java Examples
The following examples show how to use
org.nd4j.linalg.indexing.NDArrayIndex.
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: UnstackVertex.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public Pair<Gradient, INDArray[]> doBackward(boolean tbptt, LayerWorkspaceMgr workspaceMgr) { if (!canDoBackward()) throw new IllegalStateException("Cannot do backward pass: error not set"); INDArray out = workspaceMgr.create(ArrayType.ACTIVATION_GRAD, inputs[0].dataType(), forwardShape); long start = from * step; long end = (from + 1) * step; switch (forwardShape.length) { case 2: out.put(new INDArrayIndex[] {NDArrayIndex.interval(start, end), NDArrayIndex.all()}, epsilon); break; case 3: out.put(new INDArrayIndex[] {NDArrayIndex.interval(start, end), NDArrayIndex.all(), NDArrayIndex.all()}, epsilon); break; case 4: out.put(new INDArrayIndex[] {NDArrayIndex.interval(start, end), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all()}, epsilon); break; default: throw new RuntimeException("Invalid activation rank"); //Should never happen } return new Pair<>(null, new INDArray[] {out}); }
Example #2
Source File: Nd4jApacheAdapterUtilsUnitTest.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testINDArrayToApacheVector() { final INDArray rowArrCOrder = Nd4j.randn('c', new int[] {1, 5}); final INDArray rowArrFOrder = Nd4j.randn('f', new int[] {1, 5}); final INDArray colArrCOrder = Nd4j.randn('c', new int[] {5, 1}); final INDArray colArrFOrder = Nd4j.randn('f', new int[] {5, 1}); assertINDArrayToApacheVectorCorrectness(rowArrCOrder); assertINDArrayToApacheVectorCorrectness(rowArrFOrder); assertINDArrayToApacheVectorCorrectness(colArrCOrder); assertINDArrayToApacheVectorCorrectness(colArrFOrder); /* test on INDArray views */ assertINDArrayToApacheVectorCorrectness(rowArrCOrder.get(NDArrayIndex.all(), NDArrayIndex.interval(2, 4))); assertINDArrayToApacheVectorCorrectness(rowArrFOrder.get(NDArrayIndex.all(), NDArrayIndex.interval(2, 4))); assertINDArrayToApacheVectorCorrectness(colArrCOrder.get(NDArrayIndex.interval(2, 4), NDArrayIndex.all())); assertINDArrayToApacheVectorCorrectness(colArrFOrder.get(NDArrayIndex.interval(2, 4), NDArrayIndex.all())); }
Example #3
Source File: PLNetLoss.java From AILibs with GNU Affero General Public License v3.0 | 6 votes |
/** * Computes the gradient of the NLL for PL networks w.r.t. the k-th dyad according to equation (28) in [1]. * @param plNetOutputs The outputs for M_n dyads generated by a PLNet's output layer in order of their ranking (from best to worst). * @param k The ranking position with respect to which the gradient should be computed. Assumes zero-based indices, unlike the paper. * @return The gradient of the NLL loss w.r.t. the k-th dyad in the ranking. */ public static INDArray computeLossGradient(INDArray plNetOutputs, int k) { if (!(plNetOutputs.isRowVector()) || plNetOutputs.size(1) < 2 || k < 0 || k >= plNetOutputs.size(1)) { throw new IllegalArgumentException("Input has to be a row vector of 2 or more elements. And k has to be a valid index of plNetOutputs."); } long dyadRankingLength = plNetOutputs.size(1); double errorGradient = 0; for (int m = 0; m <= k; m++) { INDArray innerSumSlice = plNetOutputs.get(NDArrayIndex.interval(m, dyadRankingLength)); innerSumSlice = Transforms.exp(innerSumSlice); double innerSum = innerSumSlice.sum(1).getDouble(0); errorGradient += Math.exp(plNetOutputs.getDouble(k)) / innerSum; } errorGradient -= 1; return Nd4j.create(new double[] {errorGradient}); }
Example #4
Source File: Nd4jApacheAdapterUtilsUnitTest.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testINDArrayToApacheMatrix() { final INDArray rowArrCOrder = Nd4j.randn('c', new int[] {1, 4}); final INDArray rowArrFOrder = Nd4j.randn('f', new int[] {1, 4}); final INDArray colArrCOrder = Nd4j.randn('c', new int[] {4, 1}); final INDArray colArrFOrder = Nd4j.randn('f', new int[] {4, 1}); final INDArray generalCOrder = Nd4j.randn('c', new int[] {4, 5}); final INDArray generalFOrder = Nd4j.randn('f', new int[] {4, 5}); assertINDArrayToApacheMatrixCorrectness(rowArrCOrder); assertINDArrayToApacheMatrixCorrectness(rowArrFOrder); assertINDArrayToApacheMatrixCorrectness(colArrCOrder); assertINDArrayToApacheMatrixCorrectness(colArrFOrder); assertINDArrayToApacheMatrixCorrectness(generalCOrder); assertINDArrayToApacheMatrixCorrectness(generalFOrder); /* test on INDArray views */ assertINDArrayToApacheMatrixCorrectness(rowArrCOrder.get(NDArrayIndex.all(), NDArrayIndex.interval(0, 3))); assertINDArrayToApacheMatrixCorrectness(rowArrFOrder.get(NDArrayIndex.all(), NDArrayIndex.interval(0, 3))); assertINDArrayToApacheMatrixCorrectness(colArrCOrder.get(NDArrayIndex.interval(0, 3), NDArrayIndex.all())); assertINDArrayToApacheMatrixCorrectness(colArrFOrder.get(NDArrayIndex.interval(0, 3), NDArrayIndex.all())); assertINDArrayToApacheMatrixCorrectness(generalCOrder.get(NDArrayIndex.interval(1, 4), NDArrayIndex.interval(2, 4))); assertINDArrayToApacheMatrixCorrectness(generalFOrder.get(NDArrayIndex.interval(1, 4), NDArrayIndex.interval(2, 4))); }
Example #5
Source File: IndexingTests.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testGetIndicesVectorView() { INDArray matrix = Nd4j.linspace(1, 25, 25, DataType.DOUBLE).reshape('c',5, 5); INDArray column = matrix.getColumn(0).reshape(1,5); INDArray test = Nd4j.create(new double[] {6, 11}); INDArray result = null; //column.get(NDArrayIndex.point(0), NDArrayIndex.interval(1, 3)); // assertEquals(test, result); // INDArray column3 = matrix.getColumn(2).reshape(1,5); // INDArray exp = Nd4j.create(new double[] {8, 13}); // result = column3.get(NDArrayIndex.point(0), NDArrayIndex.interval(1, 3)); // assertEquals(exp, result); INDArray exp2 = Nd4j.create(new double[] {8, 18}); result = column3.get(NDArrayIndex.point(0), NDArrayIndex.interval(1, 2, 4)); assertEquals(exp2, result); }
Example #6
Source File: NDArrayIndexResolveTests.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testResolvePointVector() { INDArray arr = Nd4j.linspace(1, 4, 4); INDArrayIndex[] getPoint = {NDArrayIndex.point(1)}; INDArrayIndex[] resolved = NDArrayIndex.resolve(arr.shape(), getPoint); if (getPoint.length == resolved.length) assertArrayEquals(getPoint, resolved); else { assertEquals(2, resolved.length); assertTrue(resolved[0] instanceof PointIndex); assertEquals(0, resolved[0].offset()); assertTrue(resolved[1] instanceof PointIndex); assertEquals(1, resolved[1].offset()); } }
Example #7
Source File: UnderSamplingPreProcessorTest.java From nd4j with Apache License 2.0 | 6 votes |
public static DataSet makeDataSetSameL(int batchSize, int timesteps, float[] minorityDist, boolean twoClass) { INDArray features = Nd4j.rand(1, batchSize * timesteps * 2).reshape(batchSize, 2, timesteps); INDArray labels; if (twoClass) { labels = Nd4j.zeros(new int[] {batchSize, 2, timesteps}); } else { labels = Nd4j.zeros(new int[] {batchSize, 1, timesteps}); } for (int i = 0; i < batchSize; i++) { INDArray l; if (twoClass) { l = labels.get(NDArrayIndex.point(i), NDArrayIndex.point(1), NDArrayIndex.all()); Nd4j.getExecutioner().exec(new BernoulliDistribution(l, minorityDist[i])); INDArray lOther = labels.get(NDArrayIndex.point(i), NDArrayIndex.point(0), NDArrayIndex.all()); lOther.assign(Transforms.not(l.dup())); } else { l = labels.get(NDArrayIndex.point(i), NDArrayIndex.point(0), NDArrayIndex.all()); Nd4j.getExecutioner().exec(new BernoulliDistribution(l, minorityDist[i])); } } return new DataSet(features, labels); }
Example #8
Source File: ShapeResolutionTestsC.java From nd4j with Apache License 2.0 | 6 votes |
@Test @Ignore public void testIndexPointInterval() { INDArray zeros = Nd4j.zeros(3, 3, 3); INDArrayIndex x = NDArrayIndex.point(1); INDArrayIndex y = NDArrayIndex.interval(1, 2, true); INDArrayIndex z = NDArrayIndex.point(1); INDArray value = Nd4j.ones(1, 2); zeros.put(new INDArrayIndex[] {x, y, z}, value); String f1 = "[[[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]\n" + " [[0,00,0,00,0,00]\n" + " [0,00,1,00,0,00]\n" + " [0,00,1,00,0,00]]\n" + " [[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]]"; String f2 = "[[[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]\n" + " [[0.00,0.00,0.00]\n" + " [0.00,1.00,0.00]\n" + " [0.00,1.00,0.00]]\n" + " [[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]]"; if (!zeros.toString().equals(f2) && !zeros.toString().equals(f1)) assertEquals(f2, zeros.toString()); }
Example #9
Source File: PreProcessor3D4DTest.java From nd4j with Apache License 2.0 | 6 votes |
public Construct4dDataSet(int nExamples, int nChannels, int height, int width) { INDArray allImages = Nd4j.rand(new int[] {nExamples, nChannels, height, width}); allImages.get(NDArrayIndex.all(), NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all()).muli(100) .addi(200); allImages.get(NDArrayIndex.all(), NDArrayIndex.point(2), NDArrayIndex.all(), NDArrayIndex.all()).muli(0.001) .subi(10); INDArray labels = Nd4j.linspace(1, nChannels, nChannels).reshape(nChannels, 1); sampleDataSet = new DataSet(allImages, labels); expectedMean = allImages.mean(0, 2, 3); expectedStd = allImages.std(0, 2, 3); expectedLabelMean = labels.mean(0); expectedLabelStd = labels.std(0); expectedMin = allImages.min(0, 2, 3); expectedMax = allImages.max(0, 2, 3); }
Example #10
Source File: AdaMaxUpdater.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) { if (!viewArray.isRowVector()) throw new IllegalArgumentException("Invalid input: expect row vector input"); if (initialize) viewArray.assign(0); long length = viewArray.length(); this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2)); this.u = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length)); //Reshape to match the expected shape of the input gradient arrays this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f'); this.u = Shape.newShapeNoCopy(this.u, gradientShape, gradientOrder == 'f'); if (m == null || u == null) throw new IllegalStateException("Could not correctly reshape gradient view arrays"); this.gradientReshapeOrder = gradientOrder; }
Example #11
Source File: ShapeResolutionTestsC.java From nd4j with Apache License 2.0 | 6 votes |
@Test @Ignore public void testIndexPointAll() { INDArray zeros = Nd4j.zeros(3, 3, 3); INDArrayIndex x = NDArrayIndex.point(1); INDArrayIndex y = NDArrayIndex.all(); INDArrayIndex z = NDArrayIndex.point(1); INDArray value = Nd4j.ones(1, 3); zeros.put(new INDArrayIndex[] {x, y, z}, value); String f1 = "[[[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]\n" + " [[0,00,1,00,0,00]\n" + " [0,00,1,00,0,00]\n" + " [0,00,1,00,0,00]]\n" + " [[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]]"; String f2 = "[[[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]\n" + " [[0.00,1.00,0.00]\n" + " [0.00,1.00,0.00]\n" + " [0.00,1.00,0.00]]\n" + " [[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]]"; if (!zeros.toString().equals(f1) && !zeros.toString().equals(f2)) assertEquals(f2, zeros.toString()); }
Example #12
Source File: Nd4jUnitTest.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * A test for implementing a multiplication like: * * X_{b c} = \sum_{a} W_{a b c} v_{a} * * using matrix products and successive reshapes. */ @Test public void testMulTensorVector() { /* generate random data */ final int A = 5; final int B = 6; final int C = 7; final INDArray W = Nd4j.rand(new int[] {A, B, C}); final INDArray v = Nd4j.rand(new int[] {A, 1}); /* result using reshapes and matrix products */ final INDArray X = W.reshape(new int[] {A, B*C}).transpose().mmul(v).reshape(new int[] {B, C}); /* check against brute force result */ for (int b = 0; b < B; b++) { for (int c = 0; c < C; c++) { double prod = 0; for (int a = 0; a < A; a++) { prod += W.get(NDArrayIndex.point(a), NDArrayIndex.point(b), NDArrayIndex.point(c)).getDouble(0) * v.getDouble(a); } Assert.assertEquals(X.getScalar(b, c).getDouble(0), prod, EPS); } } }
Example #13
Source File: IndexingTests.java From nd4j with Apache License 2.0 | 6 votes |
@Test @Ignore //added recently: For some reason this is passing. // The test .equals fails on a comparison of row vs column vector. //TODO: possibly figure out what's going on here at some point? // - Adam public void testTensorGet() { INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12).reshape(3, 2, 2); /* * [[[ 1., 7.], [ 4., 10.]], [[ 2., 8.], [ 5., 11.]], [[ 3., 9.], [ 6., 12.]]]) */ INDArray firstAssertion = Nd4j.create(new double[] {1, 7}); INDArray firstTest = threeTwoTwo.get(NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all()); assertEquals(firstAssertion, firstTest); INDArray secondAssertion = Nd4j.create(new double[] {3, 9}); INDArray secondTest = threeTwoTwo.get(NDArrayIndex.point(2), NDArrayIndex.point(0), NDArrayIndex.all()); assertEquals(secondAssertion, secondTest); }
Example #14
Source File: BarnesHutTsneTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test(timeout = 300000) public void testTsne() throws Exception { DataTypeUtil.setDTypeForContext(DataType.DOUBLE); Nd4j.getRandom().setSeed(123); BarnesHutTsne b = new BarnesHutTsne.Builder().stopLyingIteration(10).setMaxIter(10).theta(0.5).learningRate(500) .useAdaGrad(false).build(); File f = Resources.asFile("/deeplearning4j-core/mnist2500_X.txt"); INDArray data = Nd4j.readNumpy(f.getAbsolutePath(), " ").get(NDArrayIndex.interval(0, 100), NDArrayIndex.interval(0, 784)); ClassPathResource labels = new ClassPathResource("mnist2500_labels.txt"); List<String> labelsList = IOUtils.readLines(labels.getInputStream()).subList(0, 100); b.fit(data); File outDir = testDir.newFolder(); b.saveAsFile(labelsList, new File(outDir, "out.txt").getAbsolutePath()); }
Example #15
Source File: PReLUParamInitializer.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public Map<String, INDArray> init(NeuralNetConfiguration conf, INDArray paramsView, boolean initializeParams) { if (!(conf.getLayer() instanceof BaseLayer)) throw new IllegalArgumentException("unsupported layer type: " + conf.getLayer().getClass().getName()); Map<String, INDArray> params = Collections.synchronizedMap(new LinkedHashMap<String, INDArray>()); val length = numParams(conf); if (paramsView.length() != length) throw new IllegalStateException( "Expected params view of length " + length + ", got length " + paramsView.length()); INDArray weightView = paramsView.get(NDArrayIndex.interval(0,0,true), NDArrayIndex.interval(0, length)); params.put(WEIGHT_KEY, createWeightMatrix(conf, weightView, initializeParams)); conf.addVariable(WEIGHT_KEY); return params; }
Example #16
Source File: AMSGradUpdater.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) { if (!viewArray.isRowVector()) throw new IllegalArgumentException("Invalid input: expect row vector input"); if (initialize) viewArray.assign(0); val n = viewArray.length() / 3; this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, n)); this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(n, 2*n)); this.vHat = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(2*n, 3*n)); //Reshape to match the expected shape of the input gradient arrays this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f'); this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f'); this.vHat = Shape.newShapeNoCopy(this.vHat, gradientShape, gradientOrder == 'f'); if (m == null || v == null || vHat == null) throw new IllegalStateException("Could not correctly reshape gradient view arrays"); this.gradientReshapeOrder = gradientOrder; }
Example #17
Source File: IndexingTestsC.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testIndexingWithMmul() { INDArray a = Nd4j.linspace(1, 9, 9).reshape(3, 3); INDArray b = Nd4j.linspace(1, 5, 5); System.out.println(b); INDArray view = a.get(all(), NDArrayIndex.interval(0, 1)); INDArray c = view.mmul(b); INDArray assertion = a.get(all(), NDArrayIndex.interval(0, 1)).dup().mmul(b); assertEquals(assertion, c); }
Example #18
Source File: SeparableConvolutionParamInitializer.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public Map<String, INDArray> getGradientsFromFlattened(NeuralNetConfiguration conf, INDArray gradientView) { SeparableConvolution2D layerConf = (SeparableConvolution2D) conf.getLayer(); int[] kernel = layerConf.getKernelSize(); val nIn = layerConf.getNIn(); val depthMultiplier = layerConf.getDepthMultiplier(); val nOut = layerConf.getNOut(); Map<String, INDArray> out = new LinkedHashMap<>(); val depthWiseParams = numDepthWiseParams(layerConf); val biasParams = numBiasParams(layerConf); INDArray depthWiseWeightGradientView = gradientView.get( NDArrayIndex.interval(0,0,true), NDArrayIndex.interval(biasParams, biasParams + depthWiseParams)) .reshape('c', depthMultiplier, nIn, kernel[0], kernel[1]); INDArray pointWiseWeightGradientView = gradientView.get( NDArrayIndex.interval(0,0,true), NDArrayIndex.interval(biasParams + depthWiseParams, numParams(conf))) .reshape('c', nOut, nIn * depthMultiplier, 1, 1); out.put(DEPTH_WISE_WEIGHT_KEY, depthWiseWeightGradientView); out.put(POINT_WISE_WEIGHT_KEY, pointWiseWeightGradientView); if(layerConf.hasBias()){ INDArray biasGradientView = gradientView.get(NDArrayIndex.interval(0,0,true), NDArrayIndex.interval(0, nOut)); out.put(BIAS_KEY, biasGradientView); } return out; }
Example #19
Source File: OCNNParamInitializer.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public Map<String, INDArray> init(NeuralNetConfiguration conf, INDArray paramsView, boolean initializeParams) { org.deeplearning4j.nn.conf.ocnn.OCNNOutputLayer ocnnOutputLayer = ( org.deeplearning4j.nn.conf.ocnn.OCNNOutputLayer) conf.getLayer(); Map<String, INDArray> params = Collections.synchronizedMap(new LinkedHashMap<String, INDArray>()); val nIn = ocnnOutputLayer.getNIn(); int hiddenLayer = ocnnOutputLayer.getHiddenSize(); Preconditions.checkState(hiddenLayer > 0, "OCNNOutputLayer hidden layer state: must be non-zero."); val firstLayerWeightLength = hiddenLayer; val secondLayerLength = nIn * hiddenLayer; int rLength = 1; INDArray weightView = paramsView.get(point(0),interval(0, firstLayerWeightLength)) .reshape(1,hiddenLayer); INDArray weightsTwoView = paramsView.get(point(0), NDArrayIndex.interval(firstLayerWeightLength, firstLayerWeightLength + secondLayerLength)) .reshape('f',nIn,hiddenLayer); INDArray rView = paramsView.get(point(0),point(paramsView.length() - rLength)); INDArray paramViewPut = createWeightMatrix(conf, weightView, initializeParams); params.put(W_KEY, paramViewPut); conf.addVariable(W_KEY); INDArray paramIvewPutTwo = createWeightMatrix(conf,weightsTwoView,initializeParams); params.put(V_KEY,paramIvewPutTwo); conf.addVariable(V_KEY); INDArray rViewPut = createWeightMatrix(conf,rView,initializeParams); params.put(R_KEY,rViewPut); conf.addVariable(R_KEY); return params; }
Example #20
Source File: OldConvolution.java From nd4j with Apache License 2.0 | 5 votes |
/** * Rearrange matrix * columns into blocks * @param col the column * transposed image to convert * @param sy stride y * @param sx stride x * @param ph padding height * @param pw padding width * @param h height * @param w width * @return */ public static INDArray col2im(INDArray col, int sy, int sx, int ph, int pw, int h, int w) { //number of images long n = col.size(0); //number of columns long c = col.size(1); //kernel height long kh = col.size(2); //kernel width long kw = col.size(3); //out height long outH = col.size(4); //out width long outW = col.size(5); INDArray img = Nd4j.create(n, c, h + 2 * ph + sy - 1, w + 2 * pw + sx - 1); for (int i = 0; i < kh; i++) { //iterate over the kernel rows long iLim = i + sy * outH; for (int j = 0; j < kw; j++) { //iterate over the kernel columns long jLim = j + sx * outW; INDArrayIndex[] indices = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(i, sy, iLim), NDArrayIndex.interval(j, sx, jLim)}; INDArray get = img.get(indices); INDArray colAdd = col.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(i), NDArrayIndex.point(j), NDArrayIndex.all(), NDArrayIndex.all()); get.addi(colAdd); img.put(indices, get); } } //return the subset of the padded image relative to the height/width of the image and the padding width/height return img.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(ph, ph + h), NDArrayIndex.interval(pw, pw + w)); }
Example #21
Source File: IndexingTestsC.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testNewAxis() { INDArray arr = Nd4j.linspace(1, 12, 12).reshape(3, 2, 2); INDArray get = arr.get(NDArrayIndex.all(), NDArrayIndex.all(), newAxis(), newAxis()); long[] shapeAssertion = {3, 2, 1, 1, 2}; assertArrayEquals(shapeAssertion, get.shape()); }
Example #22
Source File: IndexingTestsC.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void test1dSubarray_1() { val data = Nd4j.linspace(DataType.FLOAT,0, 10, 1); val exp = Nd4j.createFromArray(new float[]{3.f, 4.f}); val dataAtIndex = data.get(NDArrayIndex.interval(3, 5)); assertEquals(exp, dataAtIndex); }
Example #23
Source File: ZeroPaddingLayer.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public INDArray activate(boolean training, LayerWorkspaceMgr workspaceMgr) { assertInputSet(false); boolean nchw = layerConf().getDataFormat() == CNN2DFormat.NCHW; int hIdx = nchw ? 2 : 1; int wIdx = nchw ? 3 : 2; int[] padding = layerConf().getPadding(); val inShape = input.shape(); val outH = inShape[hIdx] + padding[0] + padding[1]; val outW = inShape[wIdx] + padding[2] + padding[3]; val outShape = nchw ? new long[] {inShape[0], inShape[1], outH, outW} : new long[] {inShape[0], outH, outW, inShape[3]}; INDArray out = workspaceMgr.create(ArrayType.ACTIVATIONS, input.dataType(), outShape, 'c'); if(nchw) { out.put(new INDArrayIndex[]{NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(padding[0], padding[0] + inShape[hIdx]), NDArrayIndex.interval(padding[2], padding[2] + inShape[wIdx])}, input); } else { out.put(new INDArrayIndex[]{NDArrayIndex.all(), NDArrayIndex.interval(padding[0], padding[0] + inShape[hIdx]), NDArrayIndex.interval(padding[2], padding[2] + inShape[wIdx]), NDArrayIndex.all()}, input); } return out; }
Example #24
Source File: GaussianReconstructionDistribution.java From deeplearning4j with Apache License 2.0 | 5 votes |
private INDArray[] calcLogProbArrayExConstants(INDArray x, INDArray preOutDistributionParams) { INDArray output = preOutDistributionParams.dup(); activationFn.getActivation(output, false); val size = output.size(1) / 2; INDArray mean = output.get(NDArrayIndex.all(), NDArrayIndex.interval(0, size)); INDArray logStdevSquared = output.get(NDArrayIndex.all(), NDArrayIndex.interval(size, 2 * size)); INDArray sigmaSquared = Transforms.exp(logStdevSquared, true); INDArray lastTerm = x.sub(mean.castTo(x.dataType())); lastTerm.muli(lastTerm); lastTerm.divi(sigmaSquared.castTo(lastTerm.dataType())).divi(2); return new INDArray[] {logStdevSquared, lastTerm}; }
Example #25
Source File: IndexShapeTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testSinglePoint() { /* Assumes all indexes are filled out. Test simple general point case */ int[] assertion = {2, 1, 4, 5, 1}; INDArrayIndex[] indexes = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all()}; int[] testShape = Indices.shape(shape, indexes); assertArrayEquals(assertion, testShape); int[] secondAssertion = {1, 2, 1, 5, 1}; INDArrayIndex[] otherCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.point(0) }; assertArrayEquals(secondAssertion, Indices.shape(shape, otherCase)); int[] thridAssertion = {1, 2, 1, 4, 5, 1}; INDArrayIndex[] thirdCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), }; assertArrayEquals(thridAssertion, Indices.shape(shape, thirdCase)); }
Example #26
Source File: NDArrayIndexResolveTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testResolvePoint() { INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2); INDArrayIndex[] test = NDArrayIndex.resolve(arr.shape(), NDArrayIndex.point(1)); INDArrayIndex[] assertion = {NDArrayIndex.point(1), NDArrayIndex.all()}; assertArrayEquals(assertion, test); INDArrayIndex[] allAssertion = {NDArrayIndex.all(), NDArrayIndex.all()}; assertArrayEquals(allAssertion, NDArrayIndex.resolve(arr.shape(), NDArrayIndex.all())); INDArrayIndex[] allAndOne = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(1)}; assertArrayEquals(allAndOne, NDArrayIndex.resolve(arr.shape(), allAndOne)); }
Example #27
Source File: GraphFeedForwardWithKeyFunction.java From deeplearning4j with Apache License 2.0 | 5 votes |
private INDArray getSubset(long exampleStart, long exampleEnd, INDArray from) { switch (from.rank()) { case 2: return from.get(NDArrayIndex.interval(exampleStart, exampleEnd), NDArrayIndex.all()); case 3: return from.get(NDArrayIndex.interval(exampleStart, exampleEnd), NDArrayIndex.all(), NDArrayIndex.all()); case 4: return from.get(NDArrayIndex.interval(exampleStart, exampleEnd), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all()); default: throw new RuntimeException("Invalid rank: " + from.rank()); } }
Example #28
Source File: LossMixtureDensity.java From nd4j with Apache License 2.0 | 5 votes |
private INDArray labelsMinusMu(INDArray labels, INDArray mu) { // Now that we have the mixtures, let's compute the negative // log likelihodd of the label against the long nSamples = labels.size(0); long labelsPerSample = labels.size(1); // This worked, but was actually much // slower than the for loop below. // labels = samples, mixtures, labels // mu = samples, mixtures // INDArray labelMinusMu = labels // .reshape('f', nSamples, labelsPerSample, 1) // .repeat(2, mMixtures) // .permute(0, 2, 1) // .subi(mu); // The above code does the same thing as the loop below, // but it does it with index magix instead of a for loop. // It turned out to be way less efficient than the simple 'for' here. INDArray labelMinusMu = Nd4j.zeros(nSamples, mMixtures, labelsPerSample); for (int k = 0; k < mMixtures; k++) { labelMinusMu.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(k), NDArrayIndex.all()}, labels); } labelMinusMu.subi(mu); return labelMinusMu; }
Example #29
Source File: ShapeTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testNewAxis() { INDArray tensor = Nd4j.linspace(1, 12, 12).reshape(3, 2, 2); INDArray assertion = Nd4j.create(new double[][] {{1, 7}, {4, 10}}).reshape(1, 2, 2); INDArray tensorGet = tensor.get(NDArrayIndex.point(0), NDArrayIndex.newAxis()); assertEquals(assertion, tensorGet); }
Example #30
Source File: TestSerialization.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testSerializationOnViewsNd4jWriteRead() throws Exception { int length = 100; INDArray arrC = Nd4j.linspace(1, length, length).reshape('c', 10, 10); INDArray arrF = Nd4j.linspace(1, length, length).reshape('f', 10, 10); INDArray subC = arrC.get(NDArrayIndex.interval(5, 10), NDArrayIndex.interval(5, 10)); INDArray subF = arrF.get(NDArrayIndex.interval(5, 10), NDArrayIndex.interval(5, 10)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputStream dos = new DataOutputStream(baos)) { Nd4j.write(subC, dos); } byte[] bytesC = baos.toByteArray(); baos = new ByteArrayOutputStream(); try (DataOutputStream dos = new DataOutputStream(baos)) { Nd4j.write(subF, dos); } byte[] bytesF = baos.toByteArray(); INDArray arr2C; try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytesC))) { arr2C = Nd4j.read(dis); } INDArray arr2F; try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytesF))) { arr2F = Nd4j.read(dis); } assertEquals(subC, arr2C); assertEquals(subF, arr2F); }