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

The following examples show how to use org.nd4j.linalg.factory.Nd4j#pullRows() . 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: OpExecutionerTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testVPull2() {
    int indexes[] = new int[] {0, 2, 4};
    INDArray array = Nd4j.linspace(1, 25, 25).reshape(5, 5);
    INDArray assertion = Nd4j.createUninitialized(new int[] {3, 5}, 'c');
    for (int i = 0; i < 3; i++) {
        assertion.putRow(i, array.getRow(indexes[i]));
    }

    INDArray result = Nd4j.pullRows(array, 1, indexes, 'c');

    assertEquals(3, result.rows());
    assertEquals(5, result.columns());
    assertEquals(assertion, result);

    System.out.println(assertion.toString());
    System.out.println(result.toString());
}
 
Example 2
Source File: WordVectorsImpl.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns 2D array, where each row represents corresponding label
 *
 * @param labels
 * @return
 */
@Override
public INDArray getWordVectors(@NonNull Collection<String> labels) {
    int indexes[] = new int[labels.size()];
    int cnt = 0;
    boolean useIndexUnknown = useUnknown && vocab.containsWord(getUNK());

    for (String label : labels) {
        if (vocab.containsWord(label)) {
            indexes[cnt] = vocab.indexOf(label);
        } else
            indexes[cnt] = useIndexUnknown ? vocab.indexOf(getUNK()) : -1;
        cnt++;
    }

    while (ArrayUtils.contains(indexes, -1)) {
        indexes = ArrayUtils.removeElement(indexes, -1);
    }
    if (indexes.length == 0) {
            return Nd4j.empty(((InMemoryLookupTable)lookupTable).getSyn0().dataType());
    }

    INDArray result = Nd4j.pullRows(lookupTable.getWeights(), 1, indexes);
    return result;
}
 
Example 3
Source File: TimeSeriesUtils.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Reverse a (per time step) time series mask, with shape [minibatch, timeSeriesLength]
 * @param mask Mask to reverse along time dimension
 * @return Mask after reversing
 */
public static INDArray reverseTimeSeriesMask(INDArray mask){
    if(mask == null){
        return null;
    }
    if(mask.rank() == 3){
        //Should normally not be used - but handle the per-output masking case
        return reverseTimeSeries(mask);
    } else if(mask.rank() != 2){
        throw new IllegalArgumentException("Invalid mask rank: must be rank 2 or 3. Got rank " + mask.rank()
                + " with shape " + Arrays.toString(mask.shape()));
    }

    if (mask.size(1) > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();
    int[] idxs = new int[(int) mask.size(1)];
    int j=0;
    for( int i=idxs.length-1; i>=0; i--){
        idxs[j++] = i;
    }

    return Nd4j.pullRows(mask, 0, idxs);
}
 
Example 4
Source File: TimeSeriesUtils.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Reverse an input time series along the time dimension
 *
 * @param in Input activations to reverse, with shape [minibatch, size, timeSeriesLength]
 * @return Reversed activations
 */
public static INDArray reverseTimeSeries(INDArray in){
    if(in == null){
        return null;
    }

    if(in.ordering() != 'f' || in.isView() || !Shape.strideDescendingCAscendingF(in)){
        in = in.dup('f');
    }

    int[] idxs = new int[(int) in.size(2)];
    int j=0;
    for( int i=idxs.length-1; i>=0; i--){
        idxs[j++] = i;
    }

    INDArray inReshape = in.reshape('f', in.size(0)*in.size(1), in.size(2));

    INDArray outReshape = Nd4j.pullRows(inReshape, 0, idxs, 'f');
    return outReshape.reshape('f', in.size(0), in.size(1), in.size(2));
}
 
Example 5
Source File: OpExecutionerTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testVPull2() {
        int indexes[] = new int[] {0, 2, 4};
        INDArray array = Nd4j.linspace(1, 25, 25, DataType.DOUBLE).reshape(5, 5);
        INDArray assertion = Nd4j.createUninitialized(DataType.DOUBLE, new long[] {3, 5}, 'c');
        for (int i = 0; i < 3; i++) {
            assertion.putRow(i, array.getRow(indexes[i]));
        }

        INDArray result = Nd4j.pullRows(array, 1, indexes, 'c');

        assertEquals(3, result.rows());
        assertEquals(5, result.columns());
        assertEquals(assertion, result);

//        System.out.println(assertion.toString());
//        System.out.println(result.toString());
    }
 
Example 6
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGather2() {

    INDArray in = Nd4j.rand(DataType.FLOAT, 10, 10);
    INDArray indices = Nd4j.createFromArray(0, 1, 5);

    SameDiff sd = SameDiff.create();

    SDVariable var = sd.var("in", in);
    SDVariable varIndices = sd.constant("indices", indices);
    SDVariable gather = sd.gather(var, varIndices, 0);

    INDArray exp = Nd4j.pullRows(in, 1, new int[]{0, 1, 5});  //Along dimension 1 -> equiv to "indexes for axis 0"
    INDArray act = gather.eval();

    assertEquals(exp, act);
}
 
Example 7
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get whole rows from the passed indices.
 *
 * @param rindices
 */
@Override
public INDArray getRows(int[] rindices) {
    Nd4j.getCompressor().autoDecompress(this);

    if (!isMatrix() && !isVector())
        throw new IllegalArgumentException("Unable to get columns from a non matrix or vector");
    if (isVector())
        return Nd4j.pullRows(this, 1, rindices);
    else {
        INDArray ret = Nd4j.create(rindices.length, columns());
        for (int i = 0; i < rindices.length; i++)
            ret.putRow(i, getRow(rindices[i]));
        return ret;
    }
}
 
Example 8
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGatherOp() {

    INDArray in = Nd4j.rand(DataType.DOUBLE, 10, 10);
    INDArray indices = Nd4j.createFromArray(0, 1, 5);
    INDArray out = Nd4j.create(3, 10);

    DynamicCustomOp op = DynamicCustomOp.builder("gather")
            .addIntegerArguments(0) //Indexes are for dimension 0
            .addInputs(in, indices)
            .addOutputs(out)
            .build();

    Nd4j.exec(op);

    INDArray exp = Nd4j.pullRows(in, 1, new int[]{0, 1, 5});  //Along dimension 1 == indexes for dimension 0

    assertEquals(exp, out);

    //Shape function:
    val shapes = Nd4j.getExecutioner().calculateOutputShape(op);
    long[] expShape = new long[]{3, 10};

    assertEquals(1, shapes.size());

    assertArrayEquals(expShape, shapes.get(0).getShape());
}
 
Example 9
Source File: OpExecutionerTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testVPull1() {
    int indexes[] = new int[] {0, 2, 4};
    INDArray array = Nd4j.linspace(1, 25, 25, DataType.DOUBLE).reshape(5, 5);
    INDArray assertion = Nd4j.createUninitialized(DataType.DOUBLE, new long[] {3, 5}, 'f');
    for (int i = 0; i < 3; i++) {
        assertion.putRow(i, array.getRow(indexes[i]));
    }

    INDArray result = Nd4j.pullRows(array, 1, indexes, 'f');

    assertEquals(3, result.rows());
    assertEquals(5, result.columns());
    assertEquals(assertion, result);
}
 
Example 10
Source File: EvaluationUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static Pair<INDArray, INDArray> extractNonMaskedTimeSteps(INDArray labels, INDArray predicted,
                                                                 INDArray outputMask) {
    if (labels.rank() != 3 || predicted.rank() != 3) {
        throw new IllegalArgumentException("Invalid data: expect rank 3 arrays. Got arrays with shapes labels="
                        + Arrays.toString(labels.shape()) + ", predictions=" + Arrays.toString(predicted.shape()));
    }

    //Reshaping here: basically RnnToFeedForwardPreProcessor...
    //Dup to f order, to ensure consistent buffer for reshaping
    labels = labels.dup('f');
    predicted = predicted.dup('f');

    INDArray labels2d = EvaluationUtils.reshapeTimeSeriesTo2d(labels);
    INDArray predicted2d = EvaluationUtils.reshapeTimeSeriesTo2d(predicted);

    if (outputMask == null) {
        return new Pair<>(labels2d, predicted2d);
    }

    INDArray oneDMask = reshapeTimeSeriesMaskToVector(outputMask);
    float[] f = oneDMask.dup().data().asFloat();
    int[] rowsToPull = new int[f.length];
    int usedCount = 0;
    for (int i = 0; i < f.length; i++) {
        if (f[i] == 1.0f) {
            rowsToPull[usedCount++] = i;
        }
    }
    if(usedCount == 0){
        //Edge case: all time steps are masked -> nothing to extract
        return null;
    }
    rowsToPull = Arrays.copyOfRange(rowsToPull, 0, usedCount);

    labels2d = Nd4j.pullRows(labels2d, 1, rowsToPull);
    predicted2d = Nd4j.pullRows(predicted2d, 1, rowsToPull);

    return new Pair<>(labels2d, predicted2d);
}
 
Example 11
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getRows(int[] rindices) {
    Nd4j.getCompressor().autoDecompress(this);

    if (!isMatrix() && !isVector())
        throw new IllegalArgumentException("Unable to get columns from a non matrix or vector");
    if (isVector())
        return Nd4j.pullRows(this, 1, rindices);
    else {
        INDArray ret = Nd4j.createUninitialized(this.dataType(), new long[] {rindices.length, columns()});
        for (int i = 0; i < rindices.length; i++)
            ret.putRow(i, getRow(rindices[i]));
        return ret;
    }
}
 
Example 12
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getColumns(int... cindices) {
    if (!isMatrix() && !isVector())
        throw new IllegalArgumentException("Unable to get columns from a non matrix or vector");
    if (isVector()) {
        return Nd4j.pullRows(this, 0, cindices, this.ordering());
    } else {
        INDArray ret = Nd4j.createUninitialized(this.dataType(), new long[]{rows(), cindices.length});
        for (int i = 0; i < cindices.length; i++)
            ret.putColumn(i, getColumn(cindices[i]));
        return ret;
    }

}
 
Example 13
Source File: EmbeddingLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
protected INDArray preOutput(boolean training, LayerWorkspaceMgr workspaceMgr) {
    assertInputSet(false);
    if (input.columns() != 1) {
        //Assume shape is [numExamples,1], and each entry is an integer index
        throw new DL4JInvalidInputException(
                        "Cannot do forward pass for embedding layer with input more than one column. "
                                        + "Expected input shape: [numExamples,1] with each entry being an integer index "
                                        + layerId());
    }

    val nIn = layerConf().getNIn();

    if (input.length() > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();
    int[] indexes = new int[(int) input.length()];
    for (int i = 0; i < indexes.length; i++) {
        indexes[i] = input.getInt(i, 0);

        if (indexes[i] < 0 || indexes[i] >= nIn) {
            throw new DL4JInvalidInputException("Invalid index for embedding layer: got index " + indexes[i]
                    + " for entry " + i + " in minibatch; indexes must be between 0 and nIn-1 inclusive (0 to "
                    + (nIn  -1) + ")");
        }
    }

    INDArray weights = getParam(DefaultParamInitializer.WEIGHT_KEY);
    INDArray bias = getParam(DefaultParamInitializer.BIAS_KEY);

    INDArray destination = workspaceMgr.createUninitialized(ArrayType.ACTIVATIONS, weights.dataType(), input.size(0), weights.size(1));
    INDArray rows = Nd4j.pullRows(weights, destination, 1, indexes);
    if(hasBias()){
        rows.addiRowVector(bias);
    }

    return rows;
}
 
Example 14
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get whole columns
 * from the passed indices.
 *
 * @param cindices
 */
@Override
public INDArray getColumns(int... cindices) {
    if (!isMatrix() && !isVector())
        throw new IllegalArgumentException("Unable to get columns from a non matrix or vector");
    if (isVector()) {
        return Nd4j.pullRows(this, 0, cindices, this.ordering());
    } else {
        INDArray ret = Nd4j.create(rows(), cindices.length);
        for (int i = 0; i < cindices.length; i++)
            ret.putColumn(i, getColumn(cindices[i]));
        return ret;
    }

}
 
Example 15
Source File: TimeSeriesUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Reverse an input time series along the time dimension
 *
 * @param in Input activations to reverse, with shape [minibatch, size, timeSeriesLength]
 * @return Reversed activations
 */
public static INDArray reverseTimeSeries(INDArray in, LayerWorkspaceMgr workspaceMgr, ArrayType arrayType){
    if(in == null){
        return null;
    }

    if(in.ordering() != 'f' || in.isView() || !Shape.strideDescendingCAscendingF(in)){
        in = workspaceMgr.dup(arrayType, in, 'f');
    }

    if (in.size(2) > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();
    int[] idxs = new int[(int) in.size(2)];
    int j=0;
    for( int i=idxs.length-1; i>=0; i--){
        idxs[j++] = i;
    }

    INDArray inReshape = in.reshape('f', in.size(0)*in.size(1), in.size(2));

    INDArray outReshape = workspaceMgr.create(arrayType, in.dataType(), new long[]{inReshape.size(0), idxs.length}, 'f');
    Nd4j.pullRows(inReshape, outReshape, 0, idxs);
    return workspaceMgr.leverageTo(arrayType, outReshape.reshape('f', in.size(0), in.size(1), in.size(2)));

    /*
    INDArray out = Nd4j.createUninitialized(in.shape(), 'f');
    CustomOp op = DynamicCustomOp.builder("reverse")
            .addIntegerArguments(new int[]{0,1})
            .addInputs(in)
            .addOutputs(out)
            .callInplace(false)
            .build();
    Nd4j.getExecutioner().exec(op);
    return out;
    */
}
 
Example 16
Source File: TimeSeriesUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Reverse a (per time step) time series mask, with shape [minibatch, timeSeriesLength]
 * @param mask Mask to reverse along time dimension
 * @return Mask after reversing
 */
public static INDArray reverseTimeSeriesMask(INDArray mask, LayerWorkspaceMgr workspaceMgr, ArrayType arrayType){
    if(mask == null){
        return null;
    }
    if(mask.rank() == 3){
        //Should normally not be used - but handle the per-output masking case
        return reverseTimeSeries(mask, workspaceMgr, arrayType);
    } else if(mask.rank() != 2){
        throw new IllegalArgumentException("Invalid mask rank: must be rank 2 or 3. Got rank " + mask.rank()
                + " with shape " + Arrays.toString(mask.shape()));
    }

    if (mask.size(1) > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();
    int[] idxs = new int[(int) mask.size(1)];
    int j=0;
    for( int i=idxs.length-1; i>=0; i--){
        idxs[j++] = i;
    }

    INDArray ret = workspaceMgr.createUninitialized(arrayType, mask.dataType(), new long[]{mask.size(0), idxs.length}, 'f');

    return Nd4j.pullRows(mask, ret, 0, idxs);

    /*
    //Assume input mask is 2d: [minibatch, tsLength]
    INDArray out = Nd4j.createUninitialized(mask.shape(), 'f');
    CustomOp op = DynamicCustomOp.builder("reverse")
            .addIntegerArguments(new int[]{1})
            .addInputs(mask)
            .addOutputs(out)
            .callInplace(false)
            .build();
    Nd4j.getExecutioner().exec(op);
    return out;
    */
}
 
Example 17
Source File: OpExecutionerTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testVPull1() {
    int indexes[] = new int[] {0, 2, 4};
    INDArray array = Nd4j.linspace(1, 25, 25).reshape(5, 5);
    INDArray assertion = Nd4j.createUninitialized(new int[] {3, 5}, 'f');
    for (int i = 0; i < 3; i++) {
        assertion.putRow(i, array.getRow(indexes[i]));
    }

    INDArray result = Nd4j.pullRows(array, 1, indexes, 'f');

    assertEquals(3, result.rows());
    assertEquals(5, result.columns());
    assertEquals(assertion, result);
}
 
Example 18
Source File: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testGatherGradient() {
    Nd4j.getRandom().setSeed(12345);

    List<String> failed = new ArrayList<>();

    for (int rank = 2; rank <= 3; rank++) {
        for (int dim = 0; dim < rank; dim++) {
            SameDiff sd = SameDiff.create();

            int[] inShape;
            if (rank == 2) {
                inShape = new int[]{10, 10};
            } else {
                inShape = new int[]{10, 10, 10};
            }

            SDVariable in = sd.var("in", Nd4j.rand(DataType.DOUBLE, inShape));
            SDVariable indices = sd.constant("indices", Nd4j.createFromArray(0, 3, 7));

            INDArray gatherExp = null;
            if(rank == 2){
                int tadDim = dim == 0 ? 1 : 0;  //Swap: pullRows dim is "tensor along dimension" vs. gather's "index is value for this dimension"
                gatherExp = Nd4j.pullRows(in.getArr(), tadDim, new int[]{0,3,7});
            }

            SDVariable gather = sd.gather(in, indices, dim);

            SDVariable loss = sd.standardDeviation("loss", gather, true, Integer.MAX_VALUE);

            String msg = "rank=" + rank + " dim=" + dim;

            TestCase tc = new TestCase(sd)
                    .testName(msg)
                    .gradCheckSkipVariables(indices.name());

            if (gatherExp != null) {
                tc.expected(gather, gatherExp);
            }

            String error = OpValidation.validate(tc);
            if(error != null){
                failed.add(msg + " - " + error);
            }
        }
    }

    assertEquals(failed.toString(), 0, failed.size());
}
 
Example 19
Source File: ROCBinary.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void eval(INDArray labels, INDArray predictions, INDArray mask, List<? extends Serializable> recordMetaData) {
    Triple<INDArray,INDArray, INDArray> p = BaseEvaluation.reshapeAndExtractNotMasked(labels, predictions, mask, axis);
    INDArray labels2d = p.getFirst();
    INDArray predictions2d = p.getSecond();
    INDArray maskArray = p.getThird();

    if (underlying != null && underlying.length != labels2d.size(1)) {
        throw new IllegalStateException("Labels array does not match stored state size. Expected labels array with "
                        + "size " + underlying.length + ", got labels array with size " + labels2d.size(1));
    }

    if (labels2d.rank() == 3) {
        evalTimeSeries(labels2d, predictions2d, maskArray);
        return;
    }

    if(labels2d.dataType() != predictions2d.dataType())
        labels2d = labels2d.castTo(predictions2d.dataType());

    int n = (int) labels2d.size(1);
    if (underlying == null) {
        underlying = new ROC[n];
        for (int i = 0; i < n; i++) {
            underlying[i] = new ROC(thresholdSteps, rocRemoveRedundantPts);
        }
    }

    int[] perExampleNonMaskedIdxs = null;
    for (int i = 0; i < n; i++) {
        INDArray prob = predictions2d.getColumn(i).reshape(predictions2d.size(0), 1);
        INDArray label = labels2d.getColumn(i).reshape(labels2d.size(0), 1);
        if (maskArray != null) {
            //If mask array is present, pull out the non-masked rows only
            INDArray m;
            boolean perExampleMasking = false;
            if (maskArray.isColumnVectorOrScalar()) {
                //Per-example masking
                m = maskArray;
                perExampleMasking = true;
            } else {
                //Per-output masking
                m = maskArray.getColumn(i);
            }
            int[] rowsToPull;

            if (perExampleNonMaskedIdxs != null) {
                //Reuse, per-example masking
                rowsToPull = perExampleNonMaskedIdxs;
            } else {
                int nonMaskedCount = m.sumNumber().intValue();
                rowsToPull = new int[nonMaskedCount];
                val maskSize = m.size(0);
                int used = 0;
                for (int j = 0; j < maskSize; j++) {
                    if (m.getDouble(j) != 0.0) {
                        rowsToPull[used++] = j;
                    }
                }
                if (perExampleMasking) {
                    perExampleNonMaskedIdxs = rowsToPull;
                }
            }

            //TODO Temporary workaround for: https://github.com/deeplearning4j/deeplearning4j/issues/7102
            if(prob.isView())
                prob = prob.dup();
            if(label.isView())
                label = label.dup();

            prob = Nd4j.pullRows(prob, 1, rowsToPull); //1: tensor along dim 1
            label = Nd4j.pullRows(label, 1, rowsToPull);
        }

        underlying[i].eval(label, prob);
    }
}