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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#ravel() . 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: ImageLoader.java    From DataVec with Apache License 2.0 5 votes vote down vote up
/**
 * Changes the input stream in to an
 * bgr based raveled(flattened) vector
 * @param file the input stream to convert
 * @return  the raveled bgr values for this input stream
 */
public INDArray toRaveledTensor(File file) {
    try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        INDArray ret = toRaveledTensor(bis);
        bis.close();
        return ret.ravel();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: ShapeTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRavel() {
    INDArray linspace = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArray asseriton = Nd4j.linspace(1, 4, 4);
    INDArray raveled = linspace.ravel();
    assertEquals(asseriton, raveled);

    INDArray tensorLinSpace = Nd4j.linspace(1, 16, 16).reshape(2, 2, 2, 2);
    INDArray linspaced = Nd4j.linspace(1, 16, 16);
    INDArray tensorLinspaceRaveled = tensorLinSpace.ravel();
    assertEquals(linspaced, tensorLinspaceRaveled);

}
 
Example 4
Source File: ImageLoader.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Changes the input stream in to an
 * bgr based raveled(flattened) vector
 *
 * @param file the input stream to convert
 * @return the raveled bgr values for this input stream
 */
public INDArray toRaveledTensor(File file) {
    try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        INDArray ret = toRaveledTensor(bis);
        bis.close();
        return ret.ravel();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: Nd4jTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpandDims(){
    final List<Pair<INDArray, String>> testMatricesC = NDArrayCreationUtil.getAllTestMatricesWithShape('c', 3, 5, 0xDEAD, DataType.DOUBLE);
    final List<Pair<INDArray, String>> testMatricesF = NDArrayCreationUtil.getAllTestMatricesWithShape('f', 7, 11, 0xBEEF, DataType.DOUBLE);

    final ArrayList<Pair<INDArray, String>> testMatrices = new ArrayList<>(testMatricesC);
    testMatrices.addAll(testMatricesF);

    for (Pair<INDArray, String> testMatrixPair : testMatrices) {
        final String recreation = testMatrixPair.getSecond();
        final INDArray testMatrix = testMatrixPair.getFirst();
        final char ordering = testMatrix.ordering();
        val shape = testMatrix.shape();
        final int rank = testMatrix.rank();
        for (int i = -rank; i <= rank; i++) {
            final INDArray expanded = Nd4j.expandDims(testMatrix, i);

            final String message = "Expanding in Dimension " + i + "; Shape before expanding: " + Arrays.toString(shape) + " "+ordering+" Order; Shape after expanding: " + Arrays.toString(expanded.shape()) +  " "+expanded.ordering()+"; Input Created via: " + recreation;

            val tmR = testMatrix.ravel();
            val expR = expanded.ravel();
            assertEquals(message, 1, expanded.shape()[i < 0 ? i + rank : i]);
            assertEquals(message, tmR, expR);
            assertEquals(message, ordering,  expanded.ordering());

            testMatrix.assign(Nd4j.rand(DataType.DOUBLE, shape));
            assertEquals(message, testMatrix.ravel(), expanded.ravel());
        }
    }
}
 
Example 6
Source File: ShapeTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRavel() {
    INDArray linspace = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArray asseriton = Nd4j.linspace(1, 4, 4);
    INDArray raveled = linspace.ravel();
    assertEquals(asseriton, raveled);

    INDArray tensorLinSpace = Nd4j.linspace(1, 16, 16).reshape(2, 2, 2, 2);
    INDArray linspaced = Nd4j.linspace(1, 16, 16);
    INDArray tensorLinspaceRaveled = tensorLinSpace.ravel();
    assertEquals(linspaced, tensorLinspaceRaveled);

}
 
Example 7
Source File: MovingWindowMatrix.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Moving window, capture a row x column moving window of
 * a given matrix
 * @param flattened whether the arrays should be flattened or not
 * @return the list of moving windows
 */
public List<INDArray> windows(boolean flattened) {
    List<INDArray> ret = new ArrayList<>();
    int window = 0;

    for (int i = 0; i < toSlice.length(); i++) {
        if (window >= toSlice.length())
            break;
        double[] w = new double[this.windowRowSize * this.windowColumnSize];
        for (int count = 0; count < this.windowRowSize * this.windowColumnSize; count++) {
            w[count] = toSlice.getDouble(count + window);
        }
        INDArray add = Nd4j.create(w);
        if (flattened)
            add = add.ravel();
        else
            add = add.reshape(windowRowSize, windowColumnSize);
        if (addRotate) {
            INDArray currRotation = add.dup();
            //3 different orientations besides the original
            for (int rotation = 0; rotation < 3; rotation++) {
                Nd4j.rot90(currRotation);
                ret.add(currRotation.dup());
            }

        }

        window += this.windowRowSize * this.windowColumnSize;
        ret.add(add);
    }


    return ret;
}
 
Example 8
Source File: BaseLabels.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public List<List<ClassPrediction>> decodePredictions(INDArray predictions, int n) {
    if(predictions.rank() == 1){
        //Reshape 1d edge case to [1, nClasses] 2d
        predictions = predictions.reshape(1, predictions.length());
    }
    Preconditions.checkState(predictions.size(1) == labels.size(), "Invalid input array:" +
            " expected array with size(1) equal to numLabels (%s), got array with shape %s", labels.size(), predictions.shape());

    long rows = predictions.size(0);
    long cols = predictions.size(1);
    if (predictions.isColumnVectorOrScalar()) {
        predictions = predictions.ravel();
        rows = (int) predictions.size(0);
        cols = (int) predictions.size(1);
    }
    List<List<ClassPrediction>> descriptions = new ArrayList<>();
    for (int batch = 0; batch < rows; batch++) {
        INDArray result = predictions.getRow(batch, true);
        result = Nd4j.vstack(Nd4j.linspace(result.dataType(), 0, cols, 1).reshape(1,cols), result);
        result = Nd4j.sortColumns(result, 1, false);
        List<ClassPrediction> current = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int label = result.getInt(0, i);
            double prob = result.getDouble(1, i);
            current.add(new ClassPrediction(label, getLabel(label), prob));
        }
        descriptions.add(current);
    }
    return descriptions;
}