Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#toString()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#toString() . 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: AsynchronousFlowControllerTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDependencies3() throws Exception {
    INDArray arrayWrite = Nd4j.create(new float[]{1f, 2f, 3f});
    INDArray array = Nd4j.create(new float[]{1f, 2f, 3f});

    // we use synchronization to make sure it completes activeWrite caused by array creation
    String arrayContents = array.toString();

    AllocationPoint point = allocator.getAllocationPoint(array);
    AllocationPoint pointWrite = allocator.getAllocationPoint(arrayWrite);

    assertPointHasNoDependencies(point);

    CudaContext context = controller.prepareAction(arrayWrite, array);
    controller.registerAction(context, arrayWrite, array);

    assertTrue(controller.hasActiveReads(point));
    assertFalse(controller.hasActiveReads(pointWrite));
    assertNotEquals(-1, controller.hasActiveWrite(pointWrite));

    controller.synchronizeReadLanes(point);

    assertPointHasNoDependencies(point);

    assertEquals(-1, controller.hasActiveWrite(pointWrite));
}
 
Example 2
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPermute() {
    INDArray n = Nd4j.create(Nd4j.linspace(1, 20, 20).data(), new long[] {5, 4});
    INDArray transpose = n.transpose();
    INDArray permute = n.permute(1, 0);
    assertEquals(permute, transpose);
    assertEquals(transpose.length(), permute.length(), 1e-1);


    INDArray toPermute = Nd4j.create(Nd4j.linspace(0, 7, 8).data(), new long[] {2, 2, 2});
    INDArray permuted = toPermute.permute(2, 1, 0);
    assertNotEquals(toPermute, permuted);

    INDArray permuteOther = toPermute.permute(1, 2, 0);
    for (int i = 0; i < permuteOther.slices(); i++) {
        INDArray toPermutesliceI = toPermute.slice(i);
        INDArray permuteOtherSliceI = permuteOther.slice(i);
        permuteOtherSliceI.toString();
        assertNotEquals(toPermutesliceI, permuteOtherSliceI);
    }
    assertArrayEquals(permuteOther.shape(), toPermute.shape());
    assertNotEquals(toPermute, permuteOther);


}
 
Example 3
Source File: BaseUnderSamplingPreProcessor.java    From nd4j with Apache License 2.0 6 votes vote down vote up
private void validateData(INDArray label, INDArray labelMask) {
    if (label.rank() != 3) {
        throw new IllegalArgumentException(
                        "UnderSamplingByMaskingPreProcessor can only be applied to a time series dataset");
    }
    if (label.size(1) > 2) {
        throw new IllegalArgumentException(
                        "UnderSamplingByMaskingPreProcessor can only be applied to labels that represent binary classes. Label size was found to be "
                                        + label.size(1) + ".Expecting size=1 or size=2.");
    }
    if (label.size(1) == 2) {
        //check if label is of size one hot
        if (!label.sum(1).mul(labelMask).equals(labelMask)) {
            throw new IllegalArgumentException("Labels of size minibatchx2xtimesteps are expected to be one hot."
                            + label.toString() + "\n is not one-hot");
        }
    }
}
 
Example 4
Source File: ToStringTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testToStringScalars(){
    DataType[] dataTypes = new DataType[]{DataType.FLOAT, DataType.DOUBLE, DataType.BOOL, DataType.INT, DataType.UINT32};
    String[] strs = new String[]{"1.0000", "1.0000", "true", "1", "1"};

    for(int dt=0; dt<5; dt++ ) {
        for (int i = 0; i < 5; i++) {
            long[] shape = ArrayUtil.nTimes(i, 1L);
            INDArray scalar = Nd4j.scalar(1.0f).castTo(dataTypes[dt]).reshape(shape);
            String str = scalar.toString();
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < i; j++) {
                sb.append("[");
            }
            sb.append(strs[dt]);
            for (int j = 0; j < i; j++) {
                sb.append("]");
            }
            String exp = sb.toString();
            assertEquals("Rank: " + i + ", DT: " + dataTypes[dt], exp, str);
        }
    }
}
 
Example 5
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPermute() {
    INDArray n = Nd4j.create(Nd4j.linspace(1, 20, 20, DataType.DOUBLE).data(), new long[] {5, 4});
    INDArray transpose = n.transpose();
    INDArray permute = n.permute(1, 0);
    assertEquals(permute, transpose);
    assertEquals(transpose.length(), permute.length(), 1e-1);


    INDArray toPermute = Nd4j.create(Nd4j.linspace(0, 7, 8, DataType.DOUBLE).data(), new long[] {2, 2, 2});
    INDArray permuted = toPermute.dup().permute(2, 1, 0);
    boolean eq = toPermute.equals(permuted);
    assertNotEquals(toPermute, permuted);

    INDArray permuteOther = toPermute.permute(1, 2, 0);
    for (int i = 0; i < permuteOther.slices(); i++) {
        INDArray toPermutesliceI = toPermute.slice(i);
        INDArray permuteOtherSliceI = permuteOther.slice(i);
        permuteOtherSliceI.toString();
        assertNotEquals(toPermutesliceI, permuteOtherSliceI);
    }
    assertArrayEquals(permuteOther.shape(), toPermute.shape());
    assertNotEquals(toPermute, permuteOther);


}
 
Example 6
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testPutRowGetRowOrdering() {
        INDArray row1 = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
        INDArray put = Nd4j.create(new double[] {5, 6});
        row1.putRow(1, put);

//        System.out.println(row1);
        row1.toString();

        INDArray row1Fortran = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
        INDArray putFortran = Nd4j.create(new double[] {5, 6});
        row1Fortran.putRow(1, putFortran);
        assertEquals(row1, row1Fortran);
        INDArray row1CTest = row1.getRow(1);
        INDArray row1FortranTest = row1Fortran.getRow(1);
        assertEquals(row1CTest, row1FortranTest);



    }
 
Example 7
Source File: BaseUnderSamplingPreProcessor.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private void validateData(INDArray label, INDArray labelMask) {
    if (label.rank() != 3) {
        throw new IllegalArgumentException(
                        "UnderSamplingByMaskingPreProcessor can only be applied to a time series dataset");
    }
    if (label.size(1) > 2) {
        throw new IllegalArgumentException(
                        "UnderSamplingByMaskingPreProcessor can only be applied to labels that represent binary classes. Label size was found to be "
                                        + label.size(1) + ".Expecting size=1 or size=2.");
    }
    if (label.size(1) == 2) {
        //check if label is of size one hot
        INDArray sum1 = label.sum(1).mul(labelMask);
        INDArray floatMask = labelMask.castTo(label.dataType());
        if (!sum1.equals(floatMask)) {
            throw new IllegalArgumentException("Labels of size minibatchx2xtimesteps are expected to be one hot."
                            + label.toString() + "\n is not one-hot");
        }
    }
}
 
Example 8
Source File: MnistTestFXApp.java    From java-ml-projects with Apache License 2.0 5 votes vote down vote up
private void predictImage(BufferedImage img ) throws IOException {
	ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1);
	INDArray image = loader.asRowVector(img);
	imagePreProcessingScaler.transform(image);
	INDArray output = model.output(image);
	String putStr = output.toString();
	lblResult.setText("Prediction: " + model.predict(image)[0] + "\n " + putStr);
}
 
Example 9
Source File: LeadingAndTrailingOnes.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateLeadingAndTrailingOnes() {
    INDArray arr = Nd4j.create(1, 10, 1, 1);
    arr.assign(1);
    arr.toString();
    System.out.println(arr);
}
 
Example 10
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRngSanityCheck(){
    Nd4j.getRandom().setSeed(12345);
    for(DataType dt : new DataType[]{DataType.FLOAT, DataType.DOUBLE,DataType.BFLOAT16}) {
        if (!dt.isNumerical())
            continue;
        SameDiff sameDiff = SameDiff.create();
        INDArray indaShape = Nd4j.createFromArray(3, 10);
        SDVariable sdShape = sameDiff.constant(indaShape);
        SDVariable random = sameDiff.random().uniform("data", 0.0, 10.0, dt, 3, 10);
        INDArray out = random.eval();
        String s = out.toString();
    }
}
 
Example 11
Source File: LoneTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void checkSliceofSlice() {
        /*
            Issue 1: Slice of slice with c order and f order views are not equal

            Comment out assert and run then -> Issue 2: Index out of bound exception with certain shapes when accessing elements with getDouble() in f order
            (looks like problem is when rank-1==1) eg. 1,2,1 and 2,2,1
         */
        int[] ranksToCheck = new int[]{2, 3, 4, 5};
        for (int rank = 0; rank < ranksToCheck.length; rank++) {
//            log.info("\nRunning through rank " + ranksToCheck[rank]);
            List<Pair<INDArray, String>> allF = NDArrayCreationUtil.getTestMatricesWithVaryingShapes(ranksToCheck[rank], 'f', DataType.FLOAT);
            Iterator<Pair<INDArray, String>> iter = allF.iterator();
            while (iter.hasNext()) {
                Pair<INDArray, String> currentPair = iter.next();
                INDArray origArrayF = currentPair.getFirst();
                INDArray sameArrayC = origArrayF.dup('c');
//                log.info("\nLooping through slices for shape " + currentPair.getSecond());
//                log.info("\nOriginal array:\n" + origArrayF);
                origArrayF.toString();
                INDArray viewF = origArrayF.slice(0);
                INDArray viewC = sameArrayC.slice(0);
//                log.info("\nSlice 0, C order:\n" + viewC.toString());
//                log.info("\nSlice 0, F order:\n" + viewF.toString());
                viewC.toString();
                viewF.toString();
                for (int i = 0; i < viewF.slices(); i++) {
                    //assertEquals(viewF.slice(i),viewC.slice(i));
                    for (int j = 0; j < viewF.slice(i).length(); j++) {
                        //if (j>0) break;
//                        log.info("\nC order slice " + i + ", element 0 :" + viewC.slice(i).getDouble(j)); //C order is fine
//                        log.info("\nF order slice " + i + ", element 0 :" + viewF.slice(i).getDouble(j)); //throws index out of bound err on F order
                        viewC.slice(i).getDouble(j);
                        viewF.slice(i).getDouble(j);
                    }
                }
            }
        }
    }
 
Example 12
Source File: LeadingAndTrailingOnes.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testCreateLeadingAndTrailingOnes() {
        INDArray arr = Nd4j.create(1, 10, 1, 1);
        arr.assign(1);
        arr.toString();
//        System.out.println(arr);
    }
 
Example 13
Source File: NDArrayPreconditionsFormat.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public String format(String tag, Object arg) {
    if(arg == null)
        return "null";
    INDArray arr = (INDArray)arg;
    switch (tag){
        case "%ndRank":
            return String.valueOf(arr.rank());
        case "%ndShape":
            return Arrays.toString(arr.shape());
        case "%ndStride":
            return Arrays.toString(arr.stride());
        case "%ndLength":
            return String.valueOf(arr.length());
        case "%ndSInfo":
            return arr.shapeInfoToString().replaceAll("\n","");
        case "%nd10":
            if(arr.isScalar() || arr.isEmpty()){
                return arr.toString();
            }
            INDArray sub = arr.reshape(arr.length()).get(NDArrayIndex.interval(0, Math.min(arr.length(), 10)));
            return sub.toString();
        default:
            //Should never happen
            throw new IllegalStateException("Unknown format tag: " + tag);
    }
}
 
Example 14
Source File: AsynchronousFlowControllerTest.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDependencies2() throws Exception {


    INDArray arrayWrite = Nd4j.create(new float[]{1f, 2f, 3f});
    INDArray array = Nd4j.create(new float[]{1f, 2f, 3f});

    // we use synchronization to make sure it completes activeWrite caused by array creation
    String arrayContents = array.toString();

    AllocationPoint point = allocator.getAllocationPoint(array);

    assertPointHasNoDependencies(point);

    CudaContext context = controller.prepareAction(arrayWrite, array);
    controller.registerAction(context, arrayWrite, array);

    assertTrue(controller.hasActiveReads(point));
    assertEquals(-1, controller.hasActiveWrite(point));
}
 
Example 15
Source File: AsynchronousFlowControllerTest.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDependencies4() throws Exception {
    INDArray arrayWrite = Nd4j.create(new float[]{1f, 2f, 3f});
    INDArray array = Nd4j.create(new float[]{1f, 2f, 3f});

    // we use synchronization to make sure it completes activeWrite caused by array creation
    String arrayContents = array.toString();

    AllocationPoint point = allocator.getAllocationPoint(array);
    AllocationPoint pointWrite = allocator.getAllocationPoint(arrayWrite);

    assertPointHasNoDependencies(point);
    controller.cutTail();

    CudaContext context = controller.prepareAction(arrayWrite, array);
    controller.registerAction(context, arrayWrite, array);

    assertTrue(controller.hasActiveReads(point));
    assertFalse(controller.hasActiveReads(pointWrite));
    assertNotEquals(-1, controller.hasActiveWrite(pointWrite));

    Configuration configuration = CudaEnvironment.getInstance().getConfiguration();

    controller.sweepTail();

    assertTrue(controller.hasActiveReads(point));
    assertFalse(controller.hasActiveReads(pointWrite));
    assertNotEquals(-1, controller.hasActiveWrite(pointWrite));

    controller.sweepTail();

    assertTrue(controller.hasActiveReads(point));
    assertFalse(controller.hasActiveReads(pointWrite));
    assertNotEquals(-1, controller.hasActiveWrite(pointWrite));

    for (int i = 0; i < configuration.getCommandQueueLength(); i++)
        controller.sweepTail();

    assertPointHasNoDependencies(point);
    assertPointHasNoDependencies(pointWrite);
}
 
Example 16
Source File: AsynchronousFlowControllerTest.java    From nd4j with Apache License 2.0 3 votes vote down vote up
@Test
public void testDependencies1() throws Exception {


    INDArray array = Nd4j.create(new float[]{1f, 2f, 3f});

    // we use synchronization to make sure it completes activeWrite caused by array creation
    String arrayContents = array.toString();

    AllocationPoint point = allocator.getAllocationPoint(array);

    assertPointHasNoDependencies(point);
}