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

The following examples show how to use org.nd4j.linalg.factory.Nd4j#linspace() . 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: CompressionMagicTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDupSkipDecompression3() {
    INDArray array = Nd4j.linspace(1, 100, 2500, DataType.FLOAT);

    INDArray compressed = Nd4j.getCompressor().compress(array, "GZIP");

    INDArray newArray = compressed.dup('f');
    assertFalse(newArray.isCompressed());

    Nd4j.getCompressor().decompressi(compressed);
    //        Nd4j.getCompressor().decompressi(newArray);

    assertEquals(array, compressed);
    assertEquals(array, newArray);
    assertEquals('f', newArray.ordering());
    assertEquals('c', compressed.ordering());
}
 
Example 2
Source File: CudaBroadcastTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void execBroadcastOp() throws Exception {
    INDArray array = Nd4j.ones(1024, 1024);
    INDArray arrayRow = Nd4j.linspace(1, 1024, 1024);

    float sum = (float) array.sumNumber().doubleValue();

    array.addiRowVector(arrayRow);

    long time1 = System.nanoTime();
    for (int x = 0; x < 1000; x++) {
        array.addiRowVector(arrayRow);
    }
    long time2 = System.nanoTime();

    System.out.println("Execution time: " + ((time2 - time1) / 1000));

    assertEquals(1002, array.getFloat(0), 0.1f);
    assertEquals(2003, array.getFloat(1), 0.1f);
}
 
Example 3
Source File: CompressionMagicTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMagicDecompression1() {
    INDArray array = Nd4j.linspace(1, 100, 2500, DataType.FLOAT);

    INDArray compressed = Nd4j.getCompressor().compress(array, "GZIP");

    assertTrue(compressed.isCompressed());
    compressed.muli(1.0);

    assertFalse(compressed.isCompressed());
    assertEquals(array, compressed);
}
 
Example 4
Source File: OpExecutionerTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testIMax() {
    INDArray arr = Nd4j.linspace(1, 10, 10, DataType.DOUBLE);
    ArgMax imax = new ArgMax(arr);
    assertEquals(9, Nd4j.getExecutioner().exec(imax)[0].getInt(0));

    arr.muli(-1);
    imax = new ArgMax(arr);
    int maxIdx = Nd4j.getExecutioner().exec(imax)[0].getInt(0);
    assertEquals(0, maxIdx);
}
 
Example 5
Source File: AveragingTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleDeviceAveraging2() throws Exception {
    INDArray exp = Nd4j.linspace(1, LENGTH, LENGTH);
    List<INDArray> arrays = new ArrayList<>();
    for (int i = 0; i < THREADS; i++)
        arrays.add(exp.dup());

    INDArray mean = Nd4j.averageAndPropagate(arrays);

    assertEquals(exp, mean);

    for (int i = 0; i < THREADS; i++)
        assertEquals(exp, arrays.get(i));
}
 
Example 6
Source File: OpExecutionerTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropoutInverted() {
    INDArray array = Nd4j.linspace(1, 100, 100);
    INDArray result = Nd4j.create(100);

    DropOutInverted dropOut = new DropOutInverted(array, result, 0.65);
    Nd4j.getExecutioner().exec(dropOut);

    System.out.println("Src array: " + array);
    System.out.println("Res array: " + result);

    assertNotEquals(array, result);
}
 
Example 7
Source File: OpExecutionerTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPow() {
    INDArray oneThroughSix = Nd4j.linspace(1, 6, 6);
    Pow pow = new Pow(oneThroughSix, 2);
    Nd4j.getExecutioner().exec(pow);
    INDArray answer = Nd4j.create(new float[] {1, 4, 9, 16, 25, 36});
    assertEquals(getFailureMessage(), answer, pow.z());
}
 
Example 8
Source File: IndexingTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testVectorIndexing() {
    INDArray arr = Nd4j.linspace(1, 10, 10);
    INDArray assertion = Nd4j.create(new double[] {2, 3, 4, 5});
    INDArray viewTest = arr.get(point(0), interval(1, 5));
    assertEquals(assertion, viewTest);
}
 
Example 9
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadWrite() throws Exception {
    INDArray write = Nd4j.linspace(1, 4, 4);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    Nd4j.write(write, dos);

    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    DataInputStream dis = new DataInputStream(bis);
    INDArray read = Nd4j.read(dis);
    assertEquals(write, read);

}
 
Example 10
Source File: ElementWiseStrideTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testVstackWithMatrices(){
    INDArray[] arr = new INDArray[3];
    arr[0] = Nd4j.linspace(0,49,50).reshape('c',5,10);
    arr[1] = Nd4j.linspace(50,59,10);
    arr[2] = Nd4j.linspace(60,99,40).reshape('c',4,10);

    INDArray expected = Nd4j.linspace(0,99,100).reshape('c',10,10);
    INDArray actual = Nd4j.vstack(arr);
    System.out.println(expected);
    System.out.println();
    System.out.println(actual);
    assertEquals(expected, actual);
}
 
Example 11
Source File: OpExecutionerTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testProd() {
    INDArray linspace = Nd4j.linspace(1, 6, 6);
    Prod prod = new Prod(linspace);
    double prod2 = Nd4j.getExecutioner().execAndReturn(prod).getFinalResult().doubleValue();
    assertEquals(720, prod2, 1e-1);
}
 
Example 12
Source File: NativeOpExecutionerTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testConditionalUpdate() {
    INDArray arr = Nd4j.linspace(-2, 2, 5);
    INDArray ones = Nd4j.ones(5);

    System.out.println("arr: " + arr);
    System.out.println("ones: " + ones);

    Nd4j.getExecutioner().exec(new CompareAndSet(ones, arr, ones, Conditions.equals(0.0)));

    System.out.println("After:");
    System.out.println("arr: " + arr);
    System.out.println("ones: " + ones);
}
 
Example 13
Source File: SpecialWorkspaceTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testViewDetach_1() throws Exception {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(10000000).overallocationLimit(3.0)
            .policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.REALLOCATE)
            .policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.BLOCK_LEFT).build();

    Nd4jWorkspace workspace =
            (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "WS109");

    INDArray row = Nd4j.linspace(1, 10, 10);
    INDArray exp = Nd4j.create(1, 10).assign(2.0);
    INDArray result = null;
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS109")) {
        INDArray matrix = Nd4j.create(10, 10);
        for (int e = 0; e < matrix.rows(); e++)
            matrix.getRow(e).assign(row);


        INDArray column = matrix.getColumn(1);
        assertTrue(column.isView());
        assertTrue(column.isAttached());
        result = column.detach();
    }

    assertFalse(result.isView());
    assertFalse(result.isAttached());
    assertEquals(exp, result);
}
 
Example 14
Source File: OpExecutionerTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxMin() {
    OpExecutioner opExecutioner = Nd4j.getExecutioner();
    INDArray x = Nd4j.linspace(1, 5, 5, DataType.DOUBLE);
    Max max = new Max(x);
    opExecutioner.exec(max);
    assertEquals(5, max.getFinalResult().doubleValue(), 1e-1);
    Min min = new Min(x);
    opExecutioner.exec(min);
    assertEquals(1, min.getFinalResult().doubleValue(), 1e-1);
}
 
Example 15
Source File: NativeOpExecutionerTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPinnedCosineSimilarity2() throws Exception {
    // simple way to stop test if we're not on CUDA backend here
    INDArray array1 = Nd4j.linspace(1, 1000, 1000);
    INDArray array2 = Nd4j.linspace(100, 200, 1000);

    double result = Nd4j.getExecutioner().execAndReturn(new CosineSimilarity(array1, array2)).getFinalResult().doubleValue();

    assertEquals(0.945f, result, 0.001f);
}
 
Example 16
Source File: OpExecutionerTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowSoftmax() {
    OpExecutioner opExecutioner = Nd4j.getExecutioner();
    INDArray arr = Nd4j.linspace(1, 6, 6);
    OldSoftMax softMax = new OldSoftMax(arr);
    opExecutioner.exec(softMax);
    assertEquals(getFailureMessage(), 1.0, softMax.z().sumNumber().doubleValue(), 1e-1);
}
 
Example 17
Source File: SpecialWorkspaceTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testViewDetach_1() {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(10000000).overallocationLimit(3.0)
            .policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.REALLOCATE)
            .policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.BLOCK_LEFT).build();

    Nd4jWorkspace workspace =
            (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "WS109");

    INDArray row = Nd4j.linspace(1, 10, 10);
    INDArray exp = Nd4j.create(10).assign(2.0);
    INDArray result = null;
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS109")) {
        INDArray matrix = Nd4j.create(10, 10);
        for (int e = 0; e < matrix.rows(); e++)
            matrix.getRow(e).assign(row);


        INDArray column = matrix.getColumn(1);
        assertTrue(column.isView());
        assertTrue(column.isAttached());
        result = column.detach();
    }

    assertFalse(result.isView());
    assertFalse(result.isAttached());
    assertEquals(exp, result);
}
 
Example 18
Source File: Nd4jBase64Test.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testBase64Several() throws IOException {
    INDArray[] arrs = new INDArray[2];
    arrs[0] = Nd4j.linspace(1, 4, 4);
    arrs[1] = arrs[0].dup();
    assertArrayEquals(arrs, Nd4jBase64.arraysFromBase64(Nd4jBase64.arraysToBase64(arrs)));
}
 
Example 19
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testVectorSum() {
    INDArray lin = Nd4j.linspace(1, 4, 4, DataType.DOUBLE);
    assertEquals(10.0, lin.sumNumber().doubleValue(), 1e-1);

}
 
Example 20
Source File: CompressionMagicTests.java    From nd4j with Apache License 2.0 3 votes vote down vote up
@Test
public void testMagicDecompression3() throws Exception {
    INDArray array = Nd4j.linspace(1, 2500, 2500);

    INDArray compressed = Nd4j.getCompressor().compress(array, "INT16");

    compressed.muli(1.0);

    assertEquals(array, compressed);
}