Java Code Examples for org.nd4j.linalg.api.shape.Shape#strideDescendingCAscendingF()

The following examples show how to use org.nd4j.linalg.api.shape.Shape#strideDescendingCAscendingF() . 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: ImageFlatteningDataSetPreProcessor.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void preProcess(DataSet toPreProcess) {
    INDArray input = toPreProcess.getFeatures();
    if (input.rank() == 2)
        return; //No op: should usually never happen in a properly configured data pipeline

    //Assume input is standard rank 4 activations - i.e., CNN image data
    //First: we require input to be in c order. But c order (as declared in array order) isn't enough; also need strides to be correct
    if (input.ordering() != 'c' || !Shape.strideDescendingCAscendingF(input))
        input = input.dup('c');

    val inShape = input.shape(); //[miniBatch,depthOut,outH,outW]
    val outShape = new long[] {inShape[0], inShape[1] * inShape[2] * inShape[3]};

    INDArray reshaped = input.reshape('c', outShape);
    toPreProcess.setFeatures(reshaped);
}
 
Example 2
Source File: ImageFlatteningDataSetPreProcessor.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void preProcess(DataSet toPreProcess) {
    INDArray input = toPreProcess.getFeatures();
    if (input.rank() == 2)
        return; //No op: should usually never happen in a properly configured data pipeline

    //Assume input is standard rank 4 activations - i.e., CNN image data
    //First: we require input to be in c order. But c order (as declared in array order) isn't enough; also need strides to be correct
    if (input.ordering() != 'c' || !Shape.strideDescendingCAscendingF(input))
        input = input.dup('c');

    val inShape = input.shape(); //[miniBatch,depthOut,outH,outW]
    val outShape = new long[] {inShape[0], inShape[1] * inShape[2] * inShape[3]};

    INDArray reshaped = input.reshape('c', outShape);
    toPreProcess.setFeatures(reshaped);
}
 
Example 3
Source File: Deconvolution2DLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray activate(boolean training, LayerWorkspaceMgr workspaceMgr) {
    assertInputSet(false);

    if (cacheMode == null)
        cacheMode = CacheMode.NONE;

    applyDropOutIfNecessary(training, workspaceMgr);

    INDArray z = preOutput(training, false, workspaceMgr).getFirst();

    IActivation afn = layerConf().getActivationFn();

    if (helper != null && Shape.strideDescendingCAscendingF(z)) {
        INDArray ret = helper.activate(z, layerConf().getActivationFn(), training);
        if (ret != null) {
            return ret;
        }
    }

    INDArray activation = afn.getActivation(z, training);
    return activation;
}
 
Example 4
Source File: CnnToFeedForwardPreProcessor.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray backprop(INDArray epsilons, int miniBatchSize, LayerWorkspaceMgr workspaceMgr) {
    //Epsilons from layer above should be 2d, with shape [miniBatchSize, depthOut*outH*outW]
    if (epsilons.ordering() != 'c' || !Shape.strideDescendingCAscendingF(epsilons))
        epsilons = workspaceMgr.dup(ArrayType.ACTIVATION_GRAD, epsilons, 'c');

    if (epsilons.rank() == 4)
        return workspaceMgr.leverageTo(ArrayType.ACTIVATION_GRAD, epsilons); //Should never happen

    if (epsilons.columns() != inputWidth * inputHeight * numChannels)
        throw new IllegalArgumentException("Invalid input: expect output columns must be equal to rows "
                        + inputHeight + " x columns " + inputWidth + " x channels " + numChannels + " but was instead "
                        + Arrays.toString(epsilons.shape()));

    INDArray ret;
    if(format == CNN2DFormat.NCHW){
        ret = epsilons.reshape('c', epsilons.size(0), numChannels, inputHeight, inputWidth);
    } else {
        ret = epsilons.reshape('c', epsilons.size(0), inputHeight, inputWidth, numChannels);
    }

    return workspaceMgr.leverageTo(ArrayType.ACTIVATION_GRAD, ret); //Move if required to specified workspace
}
 
Example 5
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 6
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 7
Source File: CudnnLSTMHelper.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private static INDArray toCOrder(INDArray arr) {
    if (arr.isView() || arr.ordering() != 'c' || !Shape.strideDescendingCAscendingF(arr)) {
        arr = arr.dup('c');
    }
    return arr;
}