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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#isColumnVectorOrScalar() . 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: LossUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param to
 * @param mask
 */
public static void applyMask(INDArray to, INDArray mask) {
    //Two possibilities exist: it's *per example* masking, or it's *per output* masking
    //These cases have different mask shapes. Per example: column vector. Per output: same shape as score array
    if (mask.isColumnVectorOrScalar()) {
        to.muliColumnVector(mask);
    } else if (Arrays.equals(to.shape(), mask.shape())) {
        to.muli(mask);
    } else {
        throw new IllegalStateException("Invalid mask array: per-example masking should be a column vector, "
                        + "per output masking arrays should be the same shape as the labels array. Mask shape: "
                        + Arrays.toString(mask.shape()) + ", output shape: " + Arrays.toString(to.shape()));
    }
}
 
Example 2
Source File: LossUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param to
 * @param mask
 */
public static void applyMask(INDArray to, INDArray mask) {
    //Two possibilities exist: it's *per example* masking, or it's *per output* masking
    //These cases have different mask shapes. Per example: column vector. Per output: same shape as score array
    if (mask.isColumnVectorOrScalar()) {
        to.muliColumnVector(mask.castTo(to.dataType()));
    } else if (Arrays.equals(to.shape(), mask.shape())) {
        to.muli(mask.castTo(to.dataType()));
    } else {
        throw new IllegalStateException("Invalid mask array: per-example masking should be a column vector, "
                        + "per output masking arrays should be the same shape as the labels array. Mask shape: "
                        + Arrays.toString(mask.shape()) + ", output shape: " + Arrays.toString(to.shape()));
    }
}
 
Example 3
Source File: VpTreeNodeTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static INDArray generateNaturalsMatrix(int nrows, int ncols) {
    INDArray col = Nd4j.arange(0, nrows).reshape(nrows, 1).castTo(DataType.DOUBLE);
    INDArray points = Nd4j.create(DataType.DOUBLE, nrows, ncols);
    if (points.isColumnVectorOrScalar())
        points = col.dup();
    else {
        for (int i = 0; i < ncols; i++)
            points.putColumn(i, col);
    }
    return points;
}
 
Example 4
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;
}
 
Example 5
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);
    }
}
 
Example 6
Source File: MaskLayer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private static INDArray applyMask(INDArray input, INDArray maskArray, LayerWorkspaceMgr workspaceMgr, ArrayType type){
    if(maskArray == null){
        return workspaceMgr.leverageTo(type, input);
    }
    switch (input.rank()){
        case 2:
            if(!maskArray.isColumnVectorOrScalar() || maskArray.size(0) != input.size(0)){
                throw new IllegalStateException("Expected column vector for mask with 2d input, with same size(0)" +
                        " as input. Got mask with shape: " + Arrays.toString(maskArray.shape()) +
                        ", input shape = " + Arrays.toString(input.shape()));
            }
            return workspaceMgr.leverageTo(type, input.mulColumnVector(maskArray));
        case 3:
            //Time series input, shape [Minibatch, size, tsLength], Expect rank 2 mask
            if(maskArray.rank() != 2 || input.size(0) != maskArray.size(0) || input.size(2) != maskArray.size(1)){
                throw new IllegalStateException("With 3d (time series) input with shape [minibatch, size, sequenceLength]=" +
                        Arrays.toString(input.shape()) + ", expected 2d mask array with shape [minibatch, sequenceLength]." +
                        " Got mask with shape: "+ Arrays.toString(maskArray.shape()));
            }
            INDArray fwd = workspaceMgr.createUninitialized(type, input.dataType(), input.shape(), 'f');
            Broadcast.mul(input, maskArray, fwd, 0, 2);
            return fwd;
        case 4:
            //CNN input. Expect column vector to be shape [mb,1,h,1], [mb,1,1,w], or [mb,1,h,w]
            int[] dimensions = new int[4];
            int count = 0;
            for(int i=0; i<4; i++ ){
                if(input.size(i) == maskArray.size(i)){
                    dimensions[count++] = i;
                }
            }
            if(count < 4){
                dimensions = Arrays.copyOfRange(dimensions, 0, count);
            }

            INDArray fwd2 = workspaceMgr.createUninitialized(type, input.dataType(), input.shape(), 'c');
            Broadcast.mul(input, maskArray, fwd2, dimensions);
            return fwd2;
        default:
            throw new RuntimeException("Expected rank 2 to 4 input. Got rank " + input.rank() + " with shape "
                    + Arrays.toString(input.shape()));
    }
}