Java Code Examples for org.nd4j.linalg.factory.Nd4j#create()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#create() . 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: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBroadcastDynamicShape1(){

    //Test case: [2,1] and [4]: expect [2,4]
    INDArray out = Nd4j.create(DataType.INT, 2);
    DynamicCustomOp op = DynamicCustomOp.builder("broadcast_dynamic_shape")
            .addInputs(Nd4j.createFromArray(new int[]{2,1}), Nd4j.createFromArray(new int[]{4}))
            .addOutputs(out)
            .build();
    Nd4j.getExecutioner().exec(op);
    assertEquals(Nd4j.createFromArray(new int[]{2,4}), out);

    //Same thing, reversed input order (expect same output)
    op = DynamicCustomOp.builder("broadcast_dynamic_shape")
            .addInputs(Nd4j.createFromArray(new int[]{4}), Nd4j.createFromArray(new int[]{2,1}))
            .addOutputs(out)
            .build();
    Nd4j.getExecutioner().exec(op);
    assertEquals(Nd4j.createFromArray(new int[]{2,4}), out);
}
 
Example 2
Source File: MultiNormalizerHybridTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testMasking() {
    MultiDataSet timeSeries = new MultiDataSet(
                    new INDArray[] {Nd4j.create(new float[] {1, 2, 3, 4, 5, 0, 7, 0}).reshape(2, 2, 2),},
                    new INDArray[] {Nd4j.create(new float[] {0, 20, 0, 40, 50, 60, 70, 80}).reshape(2, 2, 2)},
                    new INDArray[] {Nd4j.create(new float[][] {{1, 1}, {1, 0}})},
                    new INDArray[] {Nd4j.create(new float[][] {{0, 1}, {1, 1}})});
    MultiDataSet timeSeriesCopy = timeSeries.copy();

    SUT.minMaxScaleAllInputs(-10, 10).minMaxScaleAllOutputs(-10, 10).fit(timeSeries);
    SUT.preProcess(timeSeries);

    MultiDataSet expected = new MultiDataSet(
                    new INDArray[] {Nd4j.create(new float[] {-10, -5, -10, -5, 10, 0, 10, 0}).reshape(2, 2, 2),},
                    new INDArray[] {Nd4j.create(new float[] {0, -10, 0, -10, 5, 10, 5, 10}).reshape(2, 2, 2),},
                    new INDArray[] {Nd4j.create(new float[][] {{1, 1}, {1, 0}})},
                    new INDArray[] {Nd4j.create(new float[][] {{0, 1}, {1, 1}})});

    assertEquals(expected, timeSeries);

    SUT.revert(timeSeries);

    assertEquals(timeSeriesCopy, timeSeries);
}
 
Example 3
Source File: TupleStreamDataSetIterator.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private DataSet convertDataSetsToDataSet(List<DataSet> rawDataSets) throws IOException {

        final int numFound = rawDataSets.size();

        final INDArray inputs = Nd4j.create(numFound, inputColumns());
        final INDArray labels = Nd4j.create(numFound, totalOutcomes());
        for (int ii = 0; ii < numFound; ++ii) {
            final DataSet dataSet = rawDataSets.get(ii);
            if (preProcessor != null) {
                preProcessor.preProcess(dataSet);
            }
            inputs.putRow(ii, dataSet.getFeatures());
            labels.putRow(ii, dataSet.getLabels());
        }

        return new DataSet(inputs, labels);
    }
 
Example 4
Source File: BaseNDArrayList.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean add(X aX) {
    if(container == null) {
        container = Nd4j.create(10);
    }
    else if(size == container.length()) {
        growCapacity(size * 2);
    }
    if(DataTypeUtil.getDtypeFromContext() == DataBuffer.Type.DOUBLE)
        container.putScalar(size,aX.doubleValue());
    else {
        container.putScalar(size,aX.floatValue());

    }

    size++;
    return true;
}
 
Example 5
Source File: BasicWorkspaceTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testLeverage3() throws Exception {
    try (Nd4jWorkspace wsOne =
                    (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "EXT")) {
        INDArray array = null;
        try (Nd4jWorkspace wsTwo =
                        (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "INT")) {
            INDArray matrix = Nd4j.create(32, 1, 40);

            INDArray view = matrix.tensorAlongDimension(0, 1, 2);
            view.assign(1.0f);
            assertEquals(40.0f, matrix.sumNumber().floatValue(), 0.01f);
            assertEquals(40.0f, view.sumNumber().floatValue(), 0.01f);
            array = view.leverageTo("EXT");
        }

        assertEquals(40.0f, array.sumNumber().floatValue(), 0.01f);
    }
}
 
Example 6
Source File: TestInvertMatrices.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Example from: <a href="https://www.wolframalpha.com/input/?i=invert+matrix+((1,2),(3,4),(5,6))^T">here</a>
 */
@Test
public void testRightPseudoInvert() {
    INDArray X = Nd4j.create(new double[][]{{1, 2}, {3, 4}, {5, 6}}).transpose();
    INDArray expectedRightInverse = Nd4j.create(new double[][]{{-16, 13}, {-4, 4}, {8, -5}}).mul(1 / 12d);
    INDArray rightInverse = InvertMatrix.pRightInvert(X, false);
    assertEquals(expectedRightInverse, rightInverse);

    final INDArray identity3x3 = Nd4j.create(new double[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}});
    final INDArray identity2x2 = Nd4j.create(new double[][]{{1, 0}, {0, 1}});
    final double precision = 1e-5;

    // left inverse
    final INDArray leftInverseCheck = rightInverse.mmul(X);
    // left inverse must not hold since X columns are not linear independent (x_3 + x_1 = 2*x_2)
    assertFalse(leftInverseCheck.equalsWithEps(identity3x3, precision));

    // left inverse must hold since X rows are linear independent
    final INDArray rightInverseCheck = X.mmul(rightInverse);
    assertTrue(rightInverseCheck.equalsWithEps(identity2x2, precision));

    // general condition X = X * X^-1 * X
    final INDArray generalCond = X.mmul(rightInverse).mmul(X);
    assertTrue(X.equalsWithEps(generalCond, precision));
    checkMoorePenroseConditions(X, rightInverse, precision);
}
 
Example 7
Source File: CudaFloatDataBufferTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReshapeDup1() throws Exception {
    INDArray arrayC = Nd4j.create(10, 100);
    INDArray arrayF = arrayC.dup('f');

    System.out.println(arrayC.shapeInfoDataBuffer());
    System.out.println(arrayF.shapeInfoDataBuffer());

    assertEquals(102, arrayF.shapeInfoDataBuffer().getInt(7));
    assertEquals(1, arrayF.shapeInfoDataBuffer().getInt(6));
}
 
Example 8
Source File: MixedDataTypesTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testUint32_1() {
    val x = Nd4j.create(DataType.UINT32, 5);
    val y = Nd4j.createFromArray(new int[]{2, 2, 2, 2, 2}).castTo(DataType.UINT32);

    x.addi(y);
    assertEquals(x, y);
}
 
Example 9
Source File: CudaFloatDataBufferTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testINDArrayOffsets3() throws Exception {
        INDArray array = Nd4j.linspace(0, 24, 25).reshape(5, 5);
        INDArray array2 = Nd4j.create(5, 5);

        assertEquals(6.0f, array.getFloat(6), 0.01f);

        INDArray slice0 = array.slice(0);
        assertEquals(0f, slice0.getFloat(0), 0.01f);

        array2.putRow(0, slice0);
        assertEquals(slice0, array2.slice(0));

        INDArray slice1 = array.slice(1);
        assertEquals(5f, slice1.getFloat(0), 0.01f);

        System.out.println("---------------------------------------------------------------------");
        array2.putRow(1, slice1);
//        assertFalse(true);
        assertEquals(slice1, array2.slice(1));
        System.out.println("---------------------------------------------------------------------");


        INDArray slice2 = array.slice(2);
        assertEquals(10f, slice2.getFloat(0), 0.01f);

        INDArray slice3 = array.slice(3);
        assertEquals(15f, slice3.getFloat(0), 0.01f);

        assertEquals(array.data().getTrackingPoint(), slice0.data().getTrackingPoint());
        assertEquals(array.data().getTrackingPoint(), slice1.data().getTrackingPoint());
        assertEquals(array.data().getTrackingPoint(), slice2.data().getTrackingPoint());
        assertEquals(array.data().getTrackingPoint(), slice3.data().getTrackingPoint());
    }
 
Example 10
Source File: ReshapeTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testColumnVectorReshape() {
    double delta = 1e-1;
    INDArray arr = Nd4j.create(1, 3);
    INDArray reshaped = arr.reshape('f', 3, 1);
    assertArrayEquals(new long[] {3, 1}, reshaped.shape());
    assertEquals(0.0, reshaped.getDouble(1), delta);
    assertEquals(0.0, reshaped.getDouble(2), delta);
    log.info("Reshaped: {}", reshaped.shapeInfoDataBuffer().asInt());
    assumeNotNull(reshaped.toString());
}
 
Example 11
Source File: BasicWorkspaceTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCold() {
    INDArray array = Nd4j.create(10);

    array.addi(1.0);

    assertEquals(10f, array.sumNumber().floatValue(), 0.01f);
}
 
Example 12
Source File: ShapeTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEight() {
    INDArray baseArr = Nd4j.linspace(1, 8, 8, DataType.DOUBLE).reshape(2, 2, 2);
    assertEquals(2, baseArr.tensorsAlongDimension(0, 1));
    INDArray columnVectorFirst = Nd4j.create(new double[][] {{1, 3}, {5, 7}});
    INDArray columnVectorSecond = Nd4j.create(new double[][] {{2, 4}, {6, 8}});
    INDArray test1 = baseArr.tensorAlongDimension(0, 0, 1);
    assertEquals(columnVectorFirst, test1);
    INDArray test2 = baseArr.tensorAlongDimension(1, 0, 1);
    assertEquals(columnVectorSecond, test2);

}
 
Example 13
Source File: CustomOpsTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test(expected = ND4JIllegalStateException.class)
public void testScatterUpdate3() throws Exception {
    val matrix = Nd4j.create(5, 5);
    val updates = Nd4j.create(2, 5).assign(1.0);
    int[] dims = new int[]{1};
    int[] indices = new int[]{0, 6};

    val exp0 = Nd4j.create(1, 5).assign(0);
    val exp1 = Nd4j.create(1, 5).assign(1);

    ScatterUpdate op = new ScatterUpdate(matrix, updates, indices, dims, ScatterUpdate.UpdateOp.ADD);
}
 
Example 14
Source File: BagOfWordsVectorizer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray transform(List<String> tokens) {
    INDArray input = Nd4j.create(1, vocabCache.numWords());
    for (String token : tokens) {
        int idx = vocabCache.indexOf(token);
        if (vocabCache.indexOf(token) >= 0)
            input.putScalar(idx, vocabCache.wordFrequency(token));
    }
    return input;
}
 
Example 15
Source File: ReductionBpOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testVarianceAlongDimensionBP() {
    //If out = variance(in) then:
    //dL/dIn = dL/dOut * dOut/dIn
    //dOut/dIn_i = 2*(in_i-mean)/(n-1)
    //OR: n instead of n-1, if not bias corrected

    for (boolean biasCorrected : new boolean[]{false, true}) {
        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);
            long divisor = biasCorrected ? 2 : 3;
            INDArray mean_0 = preReduceInput.mean(0);
            INDArray dLdOut_0 = Nd4j.create(new double[]{1, 2, 3, 4}, reducedShape_0);

            INDArray dLdInExpected_0 = preReduceInput.dup();
            dLdInExpected_0.subiRowVector(mean_0).muli(2.0 / divisor)
                    .muliRowVector(dLdOut_0);

            INDArray dLdIn = Nd4j.createUninitialized(3, 4);
            String err = OpValidation.validate(new OpTestCase(new VarianceBp(preReduceInput, dLdOut_0, dLdIn, biasCorrected, keepDims, 0))
                    .expectedOutput(0, dLdInExpected_0));
            assertNull(err);

            divisor = biasCorrected ? 3 : 4;
            long[] reducedShape_1 = (keepDims ? new long[]{3, 1} : new long[]{3});
            INDArray dLdOut_1 = Nd4j.create(new double[]{1, 2, 3}, reducedShape_1);
            INDArray mean_1 = preReduceInput.mean(1);
            INDArray dLdInExpected_1 = preReduceInput.dup();
            dLdInExpected_1.subiColumnVector(mean_1).muli(2.0 / divisor)
                    .muliColumnVector(dLdOut_1.reshape(3, 1));


            dLdIn = Nd4j.createUninitialized(3, 4);
            err = OpValidation.validate(new OpTestCase(new VarianceBp(preReduceInput, dLdOut_1, dLdIn, biasCorrected, keepDims, 1))
                    .expectedOutput(0, dLdInExpected_1));
            assertNull(err);
        }
    }
}
 
Example 16
Source File: TwoPointApproximationTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    Nd4j.create(1);
    dtype = Nd4j.dataType();

    Nd4j.setDataType(DataBuffer.Type.DOUBLE);
    Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.ANY_PANIC);
}
 
Example 17
Source File: OpExecutionerTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSoftmax() {
    INDArray vec = Nd4j.linspace(1, 6, 6, DataType.DOUBLE);
    INDArray matrix = vec.dup().reshape('f', 2, 3);
    Nd4j.getExecutioner().exec((CustomOp) new SoftMax(matrix));
    INDArray matrixAssertion = Nd4j.create(
            new double[] {0.015876241, 0.015876241, 0.11731043, 0.11731043, 0.86681336, 0.86681336},
            new int[] {2, 3}, 'f');
    assertEquals(matrixAssertion, matrix);
}
 
Example 18
Source File: CudaFloatDataBufferTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testIndexer2() throws Exception {
        INDArray array1 = Nd4j.create(15);

        System.out.println("-------------------------------------");
//        assertEquals(0.0, array1.getFloat(0), 0.0001);
        System.out.println(array1);
    }
 
Example 19
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransposeOp(){

    INDArray arr = Nd4j.linspace(1,15, 15).reshape(5,3);
    INDArray out = Nd4j.create(Nd4j.defaultFloatingPointType(), new long[]{3,5}, 'c');

    OpTestCase op = new OpTestCase(new Transpose(arr, out));
    INDArray exp = arr.transpose();
    op.expectedOutput(0, exp.dup('f'));
    String err = OpValidation.validate(op);
    assertNull(err);
}
 
Example 20
Source File: BooleanIndexingTest.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testOr1() throws Exception {
    INDArray array = Nd4j.create(new float[] {1.0f, 2.0f, 3.0f, 4.0f, 5.0f});

    assertTrue(BooleanIndexing.or(array, Conditions.greaterThan(3.0f)));
}