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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#tensorAlongDimension() . 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: OpExecutionerUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/** Tensor1DStats, used to efficiently iterate through tensors on a matrix (2d NDArray) for element-wise ops
 * For example, the offset of each 1d tensor can be calculated using only a single tensorAlongDimension method call,
 * hence is potentially faster than approaches requiring multiple tensorAlongDimension calls.<br>
 * Note that this can only (generally) be used for 2d NDArrays. For certain 3+d NDArrays, the tensor starts may not
 * be in increasing order
 */
public static Tensor1DStats get1DTensorStats(INDArray array, int... dimension) {
    long tensorLength = array.size(dimension[0]);

    //As per tensorssAlongDimension:
    long numTensors = array.tensorssAlongDimension(dimension);

    //First tensor always starts with the first element in the NDArray, regardless of dimension
    long firstTensorOffset = array.offset();

    //Next: Need to work out the separation between the start (first element) of each 1d tensor
    long tensorStartSeparation;
    int elementWiseStride; //Separation in buffer between elements in the tensor
    if (numTensors == 1) {
        tensorStartSeparation = -1; //Not applicable
        elementWiseStride = array.elementWiseStride();
    } else {
        INDArray secondTensor = array.tensorAlongDimension(1, dimension);
        tensorStartSeparation = secondTensor.offset() - firstTensorOffset;
        elementWiseStride = secondTensor.elementWiseStride();
    }

    return new Tensor1DStats(firstTensorOffset, tensorStartSeparation, numTensors, tensorLength, elementWiseStride);
}
 
Example 2
Source File: ShapeTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreeTwoTwoTwo() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12, DataType.DOUBLE).reshape(3, 2, 2);
    INDArray[] assertions = new INDArray[] {Nd4j.create(new double[] {1, 7}), Nd4j.create(new double[] {4, 10}),
                    Nd4j.create(new double[] {2, 8}), Nd4j.create(new double[] {5, 11}),
                    Nd4j.create(new double[] {3, 9}), Nd4j.create(new double[] {6, 12}),

    };

    assertEquals(assertions.length, threeTwoTwo.tensorsAlongDimension(2));
    for (int i = 0; i < assertions.length; i++) {
        INDArray test = threeTwoTwo.tensorAlongDimension(i, 2);
        assertEquals(assertions[i], test);
    }

}
 
Example 3
Source File: ShapeTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreeTwoTwoTwo() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12).reshape(3, 2, 2);
    INDArray[] assertions = new INDArray[] {Nd4j.create(new double[] {1, 7}), Nd4j.create(new double[] {4, 10}),
                    Nd4j.create(new double[] {2, 8}), Nd4j.create(new double[] {5, 11}),
                    Nd4j.create(new double[] {3, 9}), Nd4j.create(new double[] {6, 12}),

    };

    assertEquals(assertions.length, threeTwoTwo.tensorssAlongDimension(2));
    for (int i = 0; i < assertions.length; i++) {
        INDArray test = threeTwoTwo.tensorAlongDimension(i, 2);
        assertEquals(assertions[i], test);
    }

}
 
Example 4
Source File: ShapeTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreeTwoTwo() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12).reshape(3, 2, 2);
    INDArray[] assertions = new INDArray[] {Nd4j.create(new double[] {1, 4}), Nd4j.create(new double[] {7, 10}),
                    Nd4j.create(new double[] {2, 5}), Nd4j.create(new double[] {8, 11}),
                    Nd4j.create(new double[] {3, 6}), Nd4j.create(new double[] {9, 12}),

    };

    assertEquals(assertions.length, threeTwoTwo.tensorssAlongDimension(1));
    for (int i = 0; i < assertions.length; i++) {
        INDArray test = threeTwoTwo.tensorAlongDimension(i, 1);
        assertEquals(assertions[i], test);
    }

}
 
Example 5
Source File: TestTensorAlongDimension.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaVsNative() {
    long totalJavaTime = 0;
    long totalCTime = 0;
    long n = 10;
    INDArray row = Nd4j.create(1, 100);

    for (int i = 0; i < n; i++) {
        StopWatch javaTiming = new StopWatch();
        javaTiming.start();
        row.javaTensorAlongDimension(0, 0);
        javaTiming.stop();
        StopWatch cTiming = new StopWatch();
        cTiming.start();
        row.tensorAlongDimension(0, 0);
        cTiming.stop();
        totalJavaTime += javaTiming.getNanoTime();
        totalCTime += cTiming.getNanoTime();
    }

    System.out.println("Java timing " + (totalJavaTime / n) + " C time " + (totalCTime / n));

}
 
Example 6
Source File: CnnToRnnPreProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray backprop(INDArray output, int miniBatchSize, LayerWorkspaceMgr workspaceMgr) {
    if (output.ordering() == 'c' || !Shape.hasDefaultStridesForShape(output))
        output = output.dup('f');
    if (rnnDataFormat == RNNFormat.NWC){
        output = output.permute(0, 2, 1);
    }
    val shape = output.shape();
    INDArray output2d;
    if (shape[0] == 1) {
        //Edge case: miniBatchSize = 1
        output2d = output.tensorAlongDimension(0, 1, 2).permutei(1, 0);
    } else if (shape[2] == 1) {
        //Edge case: timeSeriesLength = 1
        output2d = output.tensorAlongDimension(0, 1, 0);
    } else {
        //As per FeedForwardToRnnPreprocessor
        INDArray permuted3d = output.permute(0, 2, 1);
        output2d = permuted3d.reshape('f', shape[0] * shape[2], shape[1]);
    }

    if (shape[1] != product)
        throw new IllegalArgumentException("Invalid input: expected output size(1)=" + shape[1]
                        + " must be equal to " + inputHeight + " x columns " + inputWidth + " x channels "
                        + numChannels + " = " + product + ", received: " + shape[1]);
    INDArray ret = workspaceMgr.dup(ArrayType.ACTIVATION_GRAD, output2d, 'c');
    return ret.reshape('c', output2d.size(0), numChannels, inputHeight, inputWidth);
}
 
Example 7
Source File: DataSetUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static INDArray tailor4d2d(@NonNull INDArray data) {
    long instances = data.size(0);
    long channels = data.size(1);
    long height = data.size(2);
    long width = data.size(3);

    INDArray in2d = Nd4j.create(channels, height * width * instances);

    long tads = data.tensorssAlongDimension(3, 2, 0);
    for (int i = 0; i < tads; i++) {
        INDArray thisTAD = data.tensorAlongDimension(i, 3, 2, 0);
        in2d.putRow(i, Nd4j.toFlattened(thisTAD));
    }
    return in2d.transposei();
}
 
Example 8
Source File: ShapeTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEight() {
    INDArray baseArr = Nd4j.linspace(1, 8, 8).reshape(2, 2, 2);
    assertEquals(2, baseArr.tensorssAlongDimension(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 9
Source File: BlasTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGemm2() {
    final INDArray a = Nd4j.rand(4, 3);
    final INDArray b = Nd4j.rand(4, 5);

    final INDArray target = Nd4j.zeros(new int[]{2, 3, 5}, 'f');
    final INDArray view = target.tensorAlongDimension(0, 1, 2);

    a.transpose().mmuli(b, view);

    final INDArray result = a.transpose().mmul(b);

    assertEquals(result, view);
}
 
Example 10
Source File: OpExecutionerTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testMeanSumSimple() {
//        System.out.println("3d");
        INDArray arr = Nd4j.ones(1, 4, 4);
        assertEquals(Nd4j.ones(1), arr.mean(1, 2));
        assertEquals(Nd4j.ones(1).muli(16), arr.sum(1, 2));

//        System.out.println("4d");
        INDArray arr4 = Nd4j.ones(1, 1, 4, 4);
        INDArray arr4m = arr4.mean(2, 3);
        INDArray arr4s = arr4.sum(2, 3);
        for (int i = 0; i < arr4m.length(); i++)
            assertEquals(arr4m.getDouble(i), 1, 1e-1);
        for (int i = 0; i < arr4s.length(); i++)
            assertEquals(arr4s.getDouble(i), 16, 1e-1);

//        System.out.println("5d");
        INDArray arr5 = Nd4j.ones(1, 1, 4, 4, 4);
        INDArray arr5m = arr5.mean(2, 3);
        INDArray arr5s = arr5.sum(2, 3);
        for (int i = 0; i < arr5m.length(); i++)
            assertEquals(arr5m.getDouble(i), 1, 1e-1);
        for (int i = 0; i < arr5s.length(); i++)
            assertEquals(arr5s.getDouble(i), 16, 1e-1);
//        System.out.println("6d");
        INDArray arr6 = Nd4j.ones(1, 1, 4, 4, 4, 4);
        INDArray arr6Tad = arr6.tensorAlongDimension(0, 2, 3);
        INDArray arr6s = arr6.sum(2, 3);
        for (int i = 0; i < arr6s.length(); i++)
            assertEquals(arr6s.getDouble(i), 16, 1e-1);

        INDArray arr6m = arr6.mean(2, 3);
        for (int i = 0; i < arr6m.length(); i++)
            assertEquals(arr6m.getDouble(i), 1, 1e-1);

    }
 
Example 11
Source File: TimeSeriesUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static INDArray reshape3dTo2d(INDArray in) {
    if (in.rank() != 3)
        throw new IllegalArgumentException("Invalid input: expect NDArray with rank 3");
    val shape = in.shape();
    if (shape[0] == 1)
        return in.tensorAlongDimension(0, 1, 2).permutei(1, 0); //Edge case: miniBatchSize==1
    if (shape[2] == 1)
        return in.tensorAlongDimension(0, 1, 0); //Edge case: timeSeriesLength=1
    INDArray permuted = in.permute(0, 2, 1); //Permute, so we get correct order after reshaping
    return permuted.reshape('f', shape[0] * shape[2], shape[1]);
}
 
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: LoneTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcat3D_Vstack_C() throws Exception {
    int[] shape = new int[]{1, 1000, 150};
    //INDArray cOrder =  Nd4j.rand(shape,123);


    List<INDArray> cArrays = new ArrayList<>();
    List<INDArray> fArrays = new ArrayList<>();

    for (int e = 0; e < 32; e++) {
        cArrays.add(Nd4j.create(shape, 'c').assign(e));
        //            fArrays.add(cOrder.dup('f'));
    }

    Nd4j.getExecutioner().commit();

    long time1 = System.currentTimeMillis();
    INDArray res = Nd4j.vstack(cArrays);
    long time2 = System.currentTimeMillis();

    log.info("Time spent: {} ms", time2 - time1);

    for (int e = 0; e < 32; e++) {
        INDArray tad = res.tensorAlongDimension(e, 1, 2);
        assertEquals((double) e, tad.meanNumber().doubleValue(), 1e-5);
    }
}
 
Example 14
Source File: TestTensorAlongDimension.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testStalled() {
    int shape[] = new int[] {3, 3, 4, 5};
    INDArray orig2 = Nd4j.create(shape, 'c');
    System.out.println("Shape: " + Arrays.toString(orig2.shapeInfoDataBuffer().asInt()));
    INDArray tad2 = orig2.tensorAlongDimension(1, 1, 2, 3);

    log.info("You'll never see this message");
}
 
Example 15
Source File: TimeSeriesUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static INDArray reshape3dTo2d(INDArray in, LayerWorkspaceMgr workspaceMgr, ArrayType arrayType) {
    if (in.rank() != 3)
        throw new IllegalArgumentException("Invalid input: expect NDArray with rank 3");
    val shape = in.shape();
    INDArray ret;
    if (shape[0] == 1) {
        ret = in.tensorAlongDimension(0, 1, 2).permutei(1, 0); //Edge case: miniBatchSize==1
    } else if (shape[2] == 1) {
        ret = in.tensorAlongDimension(0, 1, 0); //Edge case: timeSeriesLength=1
    } else {
        INDArray permuted = in.permute(0, 2, 1); //Permute, so we get correct order after reshaping
        ret = permuted.reshape('f', shape[0] * shape[2], shape[1]);
    }
    return workspaceMgr.leverageTo(arrayType, ret);
}
 
Example 16
Source File: ComputationGraphTestRNN.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testRnnTimeStep2dInput() {
    Nd4j.getRandom().setSeed(12345);
    int timeSeriesLength = 6;

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in")
                    .addLayer("0", new org.deeplearning4j.nn.conf.layers.GravesLSTM.Builder().nIn(5).nOut(7)
                                    .activation(Activation.TANH)
                                    .dist(new NormalDistribution(0, 0.5)).build(), "in")
                    .addLayer("1", new org.deeplearning4j.nn.conf.layers.GravesLSTM.Builder().nIn(7).nOut(8)
                                    .activation(Activation.TANH)
                                    .dist(new NormalDistribution(0,
                                                    0.5))
                                    .build(), "0")
                    .addLayer("2", new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                                    .nIn(8).nOut(4)
                                    .activation(Activation.SOFTMAX)
                                    .dist(new NormalDistribution(0, 0.5)).build(), "1")
                    .setOutputs("2").build();
    ComputationGraph graph = new ComputationGraph(conf);
    graph.init();

    INDArray input3d = Nd4j.rand(new int[] {3, 5, timeSeriesLength});
    INDArray out3d = graph.rnnTimeStep(input3d)[0];
    assertArrayEquals(out3d.shape(), new long[] {3, 4, timeSeriesLength});

    graph.rnnClearPreviousState();
    for (int i = 0; i < timeSeriesLength; i++) {
        INDArray input2d = input3d.tensorAlongDimension(i, 1, 0);
        INDArray out2d = graph.rnnTimeStep(input2d)[0];

        assertArrayEquals(out2d.shape(), new long[] {3, 4});

        INDArray expOut2d = out3d.tensorAlongDimension(i, 1, 0);
        assertEquals(out2d, expOut2d);
    }

    //Check same but for input of size [3,5,1]. Expect [3,4,1] out
    graph.rnnClearPreviousState();
    for (int i = 0; i < timeSeriesLength; i++) {
        INDArray temp = Nd4j.create(new int[] {3, 5, 1});
        temp.tensorAlongDimension(0, 1, 0).assign(input3d.tensorAlongDimension(i, 1, 0));
        INDArray out3dSlice = graph.rnnTimeStep(temp)[0];
        assertArrayEquals(out3dSlice.shape(), new long[] {3, 4, 1});

        assertTrue(out3dSlice.tensorAlongDimension(0, 1, 0).equals(out3d.tensorAlongDimension(i, 1, 0)));
    }
}
 
Example 17
Source File: TADTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testStall() {
    //[4, 3, 3, 4, 5, 60, 20, 5, 1, 0, 1, 99], dimensions: [1, 2, 3]
    INDArray arr = Nd4j.create(3, 3, 4, 5);
    arr.tensorAlongDimension(0, 1, 2, 3);
}
 
Example 18
Source File: ConcatTestsC.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testConcat3d() {
        INDArray first = Nd4j.linspace(1, 24, 24, Nd4j.dataType()).reshape('c', 2, 3, 4);
        INDArray second = Nd4j.linspace(24, 36, 12, Nd4j.dataType()).reshape('c', 1, 3, 4);
        INDArray third = Nd4j.linspace(36, 48, 12, Nd4j.dataType()).reshape('c', 1, 3, 4);

        //ConcatV2, dim 0
        INDArray exp = Nd4j.create(2 + 1 + 1, 3, 4);
        exp.put(new INDArrayIndex[] {NDArrayIndex.interval(0, 2), NDArrayIndex.all(), NDArrayIndex.all()}, first);
        exp.put(new INDArrayIndex[] {NDArrayIndex.point(2), NDArrayIndex.all(), NDArrayIndex.all()}, second);
        exp.put(new INDArrayIndex[] {NDArrayIndex.point(3), NDArrayIndex.all(), NDArrayIndex.all()}, third);

        INDArray concat0 = Nd4j.concat(0, first, second, third);

        assertEquals(exp, concat0);

        //ConcatV2, dim 1
        second = Nd4j.linspace(24, 32, 8, Nd4j.dataType()).reshape('c', 2, 1, 4);
        for (int i = 0; i < second.tensorsAlongDimension(1); i++) {
            INDArray secondTad = second.tensorAlongDimension(i, 1);
//            System.out.println(second.tensorAlongDimension(i, 1));
        }

        third = Nd4j.linspace(32, 48, 16).reshape('c', 2, 2, 4);
        exp = Nd4j.create(2, 3 + 1 + 2, 4);
        exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.interval(0, 3), NDArrayIndex.all()}, first);
        exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(3), NDArrayIndex.all()}, second);
        exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.interval(4, 6), NDArrayIndex.all()}, third);

        INDArray concat1 = Nd4j.concat(1, first, second, third);

        assertEquals(exp, concat1);

        //ConcatV2, dim 2
        second = Nd4j.linspace(24, 36, 12).reshape('c', 2, 3, 2);
        third = Nd4j.linspace(36, 42, 6).reshape('c', 2, 3, 1);
        exp = Nd4j.create(2, 3, 4 + 2 + 1);

        exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 4)}, first);
        exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(4, 6)}, second);
        exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(6)}, third);

        INDArray concat2 = Nd4j.concat(2, first, second, third);

        assertEquals(exp, concat2);
    }
 
Example 19
Source File: ConvolutionalIterationListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void onForwardPass(Model model, List<INDArray> activations) {
    int iteration = (model instanceof MultiLayerNetwork ? ((MultiLayerNetwork)model).getIterationCount() : ((ComputationGraph)model).getIterationCount());
    if (iteration % freq == 0) {

        List<INDArray> tensors = new ArrayList<>();
        int cnt = 0;
        Random rnd = new Random();
        BufferedImage sourceImage = null;
        if (model instanceof MultiLayerNetwork) {
            MultiLayerNetwork l = (MultiLayerNetwork) model;
            Layer[] layers = l.getLayers();
            if(layers.length != activations.size())
                throw new RuntimeException();
            for( int i=0; i<layers.length; i++ ){
                if(layers[i].type() == Layer.Type.CONVOLUTIONAL){
                    INDArray output = activations.get(i+1); //Offset by 1 - activations list includes input

                    if (output.shape()[0] - 1 > Integer.MAX_VALUE)
                        throw new ND4JArraySizeException();
                    int sampleDim = output.shape()[0] == 1 ? 0 : rnd.nextInt((int) output.shape()[0] - 1) + 1;
                    if (cnt == 0) {
                        INDArray inputs = layers[i].input();

                        try {
                            sourceImage = restoreRGBImage(
                                    inputs.tensorAlongDimension(sampleDim, new int[] {3, 2, 1}));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    INDArray tad = output.tensorAlongDimension(sampleDim, 3, 2, 1);

                    tensors.add(tad);

                    cnt++;
                }
            }
        } else {
            //Compgraph: no op (other forward pass method should be called instead)
            return;
        }
        BufferedImage render = rasterizeConvoLayers(tensors, sourceImage);
        Persistable p = new ConvolutionListenerPersistable(sessionID, workerID, System.currentTimeMillis(), render);
        ssr.putStaticInfo(p);

        minibatchNum++;
    }
}
 
Example 20
Source File: TADTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testStall() {
    //[4, 3, 3, 4, 5, 60, 20, 5, 1, 0, 1, 99], dimensions: [1, 2, 3]
    INDArray arr = Nd4j.create(3, 3, 4, 5);
    arr.tensorAlongDimension(0, 1, 2, 3);
}