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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#putSlice() . 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: TwoPointApproximation.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param func
 * @param x0
 * @param f0
 * @param h
 * @param oneSided
 * @return
 */
public static INDArray denseDifference(Function<INDArray,INDArray> func,
                                       INDArray x0,INDArray f0,
                                       INDArray h,INDArray oneSided) {
    INDArray hVecs = Nd4j.diag(h.reshape(1,h.length()));
    INDArray dx,df,x;
    INDArray jTransposed = Nd4j.create(x0.length(),f0.length());
    for(int i = 0; i < h.length(); i++) {
        INDArray hVecI = hVecs.slice(i);
        x = (x0.add(hVecI));
        dx = x.slice(i).sub(x0.slice(i));
        df = func.apply(x).sub(f0);
        INDArray div = df.div(dx);
        jTransposed.putSlice(i,div);
    }

    if(f0.length() == 1)
        jTransposed = jTransposed.ravel();

        return jTransposed;

}
 
Example 2
Source File: TimeSeriesGenerator.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public Pair<INDArray, INDArray> next(int index) {
    INDArray rows;
    if (shuffle) {
        rows = Nd4j.getRandom().nextInt(endIndex, new int[] {batchSize});
        rows.addi(startIndex);
    } else {
        int i = startIndex + batchSize + stride * index;
        // TODO: add stride arg to arange
        rows = Nd4j.arange(i, Math.min(i + batchSize * stride, endIndex + 1));
    }
    INDArray samples = Nd4j.create(rows.length(), length / samplingRate, data.columns());
    INDArray targets = Nd4j.create(rows.length(), this.targets.columns());

    for (int j = 0; j < rows.rows(); j++) {
        long idx = (long) rows.getDouble(j);
        INDArrayIndex indices = NDArrayIndex.interval(idx - this.length, this.samplingRate, idx);
        samples.putSlice(j, this.data.get(indices));
        INDArrayIndex point = NDArrayIndex.point((long) rows.getDouble(j));
        targets.putSlice(j, this.targets.get(point));
    }
    if (reverse)
        samples = Nd4j.reverse(samples);

    return new Pair<>(samples, targets);
}
 
Example 3
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutSlice() {
    INDArray n = Nd4j.linspace(1, 27, 27).reshape(3, 3, 3);
    INDArray newSlice = Nd4j.zeros(3, 3);
    n.putSlice(0, newSlice);
    assertEquals(getFailureMessage(), newSlice, n.slice(0));

}
 
Example 4
Source File: BaseNDArrayFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Reverses the passed in matrix such that m[0] becomes m[m.length - 1] etc
 *
 * @param reverse the matrix to reverse
 * @return the reversed matrix
 */
@Override
public INDArray rot(INDArray reverse) {
    INDArray ret = Nd4j.create(reverse.shape());
    if (reverse.isVector())
        return reverse(reverse);
    else {
        for (int i = 0; i < reverse.slices(); i++) {
            ret.putSlice(i, reverse(reverse.slice(i)));
        }
    }
    return ret.reshape(reverse.shape());
}
 
Example 5
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutSlice() {
    INDArray n = Nd4j.linspace(1, 27, 27, DataType.DOUBLE).reshape(3, 3, 3);
    INDArray newSlice = Nd4j.create(DataType.DOUBLE, 3, 3);
    Nd4j.exec(new PrintVariable(newSlice));
    log.info("Slice: {}", newSlice);
    n.putSlice(0, newSlice);
    assertEquals(getFailureMessage(), newSlice, n.slice(0));

}
 
Example 6
Source File: RGBtoGrayscaleDataSetPreProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void preProcess(DataSet dataSet) {
    Preconditions.checkNotNull(dataSet, "Encountered null dataSet");

    if(dataSet.isEmpty()) {
        return;
    }

    INDArray originalFeatures = dataSet.getFeatures();
    long[] originalShape = originalFeatures.shape();

    // result shape is NHW
    INDArray result = Nd4j.create(originalShape[0], originalShape[2], originalShape[3]);

    for(long n = 0, numExamples = originalShape[0]; n < numExamples; ++n) {
        // Extract channels
        INDArray itemFeatures = originalFeatures.slice(n, 0); // shape is CHW
        INDArray R = itemFeatures.slice(0, 0);  // shape is HW
        INDArray G = itemFeatures.slice(1, 0);
        INDArray B = itemFeatures.slice(2, 0);

        // Convert
        R.muli(RED_RATIO);
        G.muli(GREEN_RATIO);
        B.muli(BLUE_RATIO);
        R.addi(G).addi(B);

        result.putSlice((int)n, R);
    }

    dataSet.setFeatures(result);
}
 
Example 7
Source File: BaseNDArrayFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Reverses the passed in matrix such that m[0] becomes m[m.length - 1] etc
 *
 * @param reverse the matrix to reverse
 * @return the reversed matrix
 */
@Override
public INDArray rot(INDArray reverse) {
    INDArray ret = Nd4j.create(reverse.shape());
    if (reverse.isVector())
        return reverse(reverse);
    else {
        for (int i = 0; i < reverse.slices(); i++) {
            ret.putSlice(i, reverse(reverse.slice(i)));
        }
    }
    return ret.reshape(reverse.shape());
}
 
Example 8
Source File: VPTree.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create an ndarray
 * from the datapoints
 * @param data
 * @return
 */
public static INDArray buildFromData(List<DataPoint> data) {
    INDArray ret = Nd4j.create(data.size(), data.get(0).getD());
    for (int i = 0; i < ret.slices(); i++)
        ret.putSlice(i, data.get(i).getPoint());
    return ret;
}