Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#vectorsAlongDimension()
The following examples show how to use
org.nd4j.linalg.api.ndarray.INDArray#vectorsAlongDimension() .
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: ShapeTest.java From nd4j with Apache License 2.0 | 5 votes |
private void testSumHelper(int... shape) { INDArray array = Nd4j.ones(shape); for (int i = 0; i < shape.length; i++) { for (int j = 0; j < array.vectorsAlongDimension(i); j++) { INDArray vec = array.vectorAlongDimension(j, i); } array.sum(i); } }
Example 2
Source File: ShapeTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testVectorAlongDimension1() { INDArray arr = Nd4j.create(1, 5, 5); assertEquals(arr.vectorsAlongDimension(0), 5); assertEquals(arr.vectorsAlongDimension(1), 5); for (int i = 0; i < arr.vectorsAlongDimension(0); i++) { if (i < arr.vectorsAlongDimension(0) - 1 && i > 0) assertEquals(25, arr.vectorAlongDimension(i, 0).length()); } }
Example 3
Source File: ShapeTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
private void testSumHelper(int... shape) { INDArray array = Nd4j.ones(shape); for (int i = 0; i < shape.length; i++) { for (int j = 0; j < array.vectorsAlongDimension(i); j++) { INDArray vec = array.vectorAlongDimension(j, i); } array.sum(i); } }
Example 4
Source File: ShapeTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testVectorAlongDimension1() { INDArray arr = Nd4j.create(1, 5, 5); assertEquals(arr.vectorsAlongDimension(0), 5); assertEquals(arr.vectorsAlongDimension(1), 5); for (int i = 0; i < arr.vectorsAlongDimension(0); i++) { if (i < arr.vectorsAlongDimension(0) - 1 && i > 0) assertEquals(25, arr.vectorAlongDimension(i, 0).length()); } }
Example 5
Source File: ShapeTestsC.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testVectorAlongDimension() { INDArray arr = Nd4j.linspace(1, 24, 24).reshape(4, 3, 2); INDArray assertion = Nd4j.create(new float[] {3, 4}, new long[] {1, 2}); INDArray vectorDimensionTest = arr.vectorAlongDimension(1, 2); assertEquals(assertion, vectorDimensionTest); val vectorsAlongDimension1 = arr.vectorsAlongDimension(1); assertEquals(8, vectorsAlongDimension1); INDArray zeroOne = arr.vectorAlongDimension(0, 1); assertEquals(zeroOne, Nd4j.create(new float[] {1, 3, 5})); INDArray testColumn2Assertion = Nd4j.create(new float[] {2, 4, 6}); INDArray testColumn2 = arr.vectorAlongDimension(1, 1); assertEquals(testColumn2Assertion, testColumn2); INDArray testColumn3Assertion = Nd4j.create(new float[] {7, 9, 11}); INDArray testColumn3 = arr.vectorAlongDimension(2, 1); assertEquals(testColumn3Assertion, testColumn3); INDArray v1 = Nd4j.linspace(1, 4, 4).reshape(new long[] {2, 2}); INDArray testColumnV1 = v1.vectorAlongDimension(0, 0); INDArray testColumnV1Assertion = Nd4j.create(new float[] {1, 3}); assertEquals(testColumnV1Assertion, testColumnV1); INDArray testRowV1 = v1.vectorAlongDimension(1, 0); INDArray testRowV1Assertion = Nd4j.create(new float[] {2, 4}); assertEquals(testRowV1Assertion, testRowV1); INDArray n = Nd4j.create(Nd4j.linspace(1, 8, 8).data(), new long[] {2, 2, 2}); INDArray vectorOne = n.vectorAlongDimension(1, 2); INDArray assertionVectorOne = Nd4j.create(new double[] {3, 4}); assertEquals(assertionVectorOne, vectorOne); INDArray oneThroughSixteen = Nd4j.linspace(1, 16, 16).reshape(2, 2, 2, 2); assertEquals(8, oneThroughSixteen.vectorsAlongDimension(1)); assertEquals(Nd4j.create(new double[] {1, 5}), oneThroughSixteen.vectorAlongDimension(0, 1)); assertEquals(Nd4j.create(new double[] {2, 6}), oneThroughSixteen.vectorAlongDimension(1, 1)); assertEquals(Nd4j.create(new double[] {3, 7}), oneThroughSixteen.vectorAlongDimension(2, 1)); assertEquals(Nd4j.create(new double[] {4, 8}), oneThroughSixteen.vectorAlongDimension(3, 1)); assertEquals(Nd4j.create(new double[] {9, 13}), oneThroughSixteen.vectorAlongDimension(4, 1)); assertEquals(Nd4j.create(new double[] {10, 14}), oneThroughSixteen.vectorAlongDimension(5, 1)); assertEquals(Nd4j.create(new double[] {11, 15}), oneThroughSixteen.vectorAlongDimension(6, 1)); assertEquals(Nd4j.create(new double[] {12, 16}), oneThroughSixteen.vectorAlongDimension(7, 1)); INDArray fourdTest = Nd4j.linspace(1, 16, 16).reshape(2, 2, 2, 2); double[][] assertionsArr = new double[][] {{1, 3}, {2, 4}, {5, 7}, {6, 8}, {9, 11}, {10, 12}, {13, 15}, {14, 16}, }; assertEquals(assertionsArr.length, fourdTest.vectorsAlongDimension(2)); for (int i = 0; i < assertionsArr.length; i++) { INDArray test = fourdTest.vectorAlongDimension(i, 2); INDArray assertionEntry = Nd4j.create(assertionsArr[i]); assertEquals(assertionEntry, test); } }
Example 6
Source File: ShapeTestsC.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testVectorAlongDimension() { INDArray arr = Nd4j.linspace(1, 24, 24, DataType.DOUBLE).reshape(4, 3, 2); INDArray assertion = Nd4j.create(new double[] {3, 4}); INDArray vectorDimensionTest = arr.vectorAlongDimension(1, 2); assertEquals(assertion, vectorDimensionTest); val vectorsAlongDimension1 = arr.vectorsAlongDimension(1); assertEquals(8, vectorsAlongDimension1); INDArray zeroOne = arr.vectorAlongDimension(0, 1); assertEquals(zeroOne, Nd4j.create(new double[] {1, 3, 5})); INDArray testColumn2Assertion = Nd4j.create(new double[] {2, 4, 6}); INDArray testColumn2 = arr.vectorAlongDimension(1, 1); assertEquals(testColumn2Assertion, testColumn2); INDArray testColumn3Assertion = Nd4j.create(new double[] {7, 9, 11}); INDArray testColumn3 = arr.vectorAlongDimension(2, 1); assertEquals(testColumn3Assertion, testColumn3); INDArray v1 = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(new long[] {2, 2}); INDArray testColumnV1 = v1.vectorAlongDimension(0, 0); INDArray testColumnV1Assertion = Nd4j.create(new double[] {1, 3}); assertEquals(testColumnV1Assertion, testColumnV1); INDArray testRowV1 = v1.vectorAlongDimension(1, 0); INDArray testRowV1Assertion = Nd4j.create(new double[] {2, 4}); assertEquals(testRowV1Assertion, testRowV1); INDArray n = Nd4j.create(Nd4j.linspace(1, 8, 8, DataType.DOUBLE).data(), new long[] {2, 2, 2}); INDArray vectorOne = n.vectorAlongDimension(1, 2); INDArray assertionVectorOne = Nd4j.create(new double[] {3, 4}); assertEquals(assertionVectorOne, vectorOne); INDArray oneThroughSixteen = Nd4j.linspace(1, 16, 16, DataType.DOUBLE).reshape(2, 2, 2, 2); assertEquals(8, oneThroughSixteen.vectorsAlongDimension(1)); assertEquals(Nd4j.create(new double[] {1, 5}), oneThroughSixteen.vectorAlongDimension(0, 1)); assertEquals(Nd4j.create(new double[] {2, 6}), oneThroughSixteen.vectorAlongDimension(1, 1)); assertEquals(Nd4j.create(new double[] {3, 7}), oneThroughSixteen.vectorAlongDimension(2, 1)); assertEquals(Nd4j.create(new double[] {4, 8}), oneThroughSixteen.vectorAlongDimension(3, 1)); assertEquals(Nd4j.create(new double[] {9, 13}), oneThroughSixteen.vectorAlongDimension(4, 1)); assertEquals(Nd4j.create(new double[] {10, 14}), oneThroughSixteen.vectorAlongDimension(5, 1)); assertEquals(Nd4j.create(new double[] {11, 15}), oneThroughSixteen.vectorAlongDimension(6, 1)); assertEquals(Nd4j.create(new double[] {12, 16}), oneThroughSixteen.vectorAlongDimension(7, 1)); INDArray fourdTest = Nd4j.linspace(1, 16, 16, DataType.DOUBLE).reshape(2, 2, 2, 2); double[][] assertionsArr = new double[][] {{1, 3}, {2, 4}, {5, 7}, {6, 8}, {9, 11}, {10, 12}, {13, 15}, {14, 16}, }; assertEquals(assertionsArr.length, fourdTest.vectorsAlongDimension(2)); for (int i = 0; i < assertionsArr.length; i++) { INDArray test = fourdTest.vectorAlongDimension(i, 2); INDArray assertionEntry = Nd4j.create(assertionsArr[i]); assertEquals(assertionEntry, test); } }
Example 7
Source File: Indices.java From nd4j with Apache License 2.0 | 3 votes |
/** * Compute the linear offset * for an index in an ndarray. * * For c ordering this is just the index itself. * For fortran ordering, the following algorithm is used. * * Assuming an ndarray is a list of vectors. * The index of the vector relative to the given index is calculated. * * vectorAlongDimension is then used along the last dimension * using the computed index. * * The offset + the computed column wrt the index: (index % the size of the last dimension) * will render the given index in fortran ordering * @param index the index * @param arr the array * @return the linear offset */ public static int rowNumber(int index, INDArray arr) { double otherTest = ((double) index) / arr.size(-1); int test = (int) Math.floor(otherTest); // FIXME: int cast int vectors = (int) arr.vectorsAlongDimension(-1); if (test >= vectors) return vectors - 1; return test; }
Example 8
Source File: Indices.java From deeplearning4j with Apache License 2.0 | 3 votes |
/** * Compute the linear offset * for an index in an ndarray. * * For c ordering this is just the index itself. * For fortran ordering, the following algorithm is used. * * Assuming an ndarray is a list of vectors. * The index of the vector relative to the given index is calculated. * * vectorAlongDimension is then used along the last dimension * using the computed index. * * The offset + the computed column wrt the index: (index % the size of the last dimension) * will render the given index in fortran ordering * @param index the index * @param arr the array * @return the linear offset */ public static int rowNumber(int index, INDArray arr) { double otherTest = ((double) index) / arr.size(-1); int test = (int) Math.floor(otherTest); if (arr.vectorsAlongDimension(-1) > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int vectors = (int) arr.vectorsAlongDimension(-1); if (test >= vectors) return vectors - 1; return test; }