Search in sources :

Example 6 with DL4JInvalidInputException

use of org.deeplearning4j.exception.DL4JInvalidInputException in project deeplearning4j by deeplearning4j.

the class SubsamplingLayer method activate.

@Override
public INDArray activate(boolean training) {
    if (training && conf.getLayer().getDropOut() > 0) {
        Dropout.applyDropout(input, conf.getLayer().getDropOut());
    }
    //Input validation: expect rank 4 matrix
    if (input.rank() != 4) {
        throw new DL4JInvalidInputException("Got rank " + input.rank() + " array as input to SubsamplingLayer with shape " + Arrays.toString(input.shape()) + ". Expected rank 4 array with shape [minibatchSize, depth, inputHeight, inputWidth].");
    }
    int miniBatch = input.size(0);
    int inDepth = input.size(1);
    int inH = input.size(2);
    int inW = input.size(3);
    int[] kernel = layerConf().getKernelSize();
    int[] strides = layerConf().getStride();
    int[] pad;
    int[] outSize;
    if (convolutionMode == ConvolutionMode.Same) {
        //Also performs validation
        outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, null, convolutionMode);
        pad = ConvolutionUtils.getSameModeTopLeftPadding(outSize, new int[] { inH, inW }, kernel, strides);
    } else {
        pad = layerConf().getPadding();
        //Also performs validation
        outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, pad, convolutionMode);
    }
    int outH = outSize[0];
    int outW = outSize[1];
    if (helper != null && Nd4j.dataType() != DataBuffer.Type.HALF) {
        INDArray ret = helper.activate(input, training, kernel, strides, pad, layerConf().getPoolingType(), convolutionMode);
        if (ret != null) {
            return ret;
        }
    }
    //Similar to convolution layer forward pass: do im2col, but permute so that pooling can be done with efficient strides...
    //Current im2col implementation expects input with shape [miniBatch,depth,kH,kW,outH,outW]
    INDArray col = Nd4j.create(new int[] { miniBatch, inDepth, outH, outW, kernel[0], kernel[1] }, 'c');
    INDArray col2 = col.permute(0, 1, 4, 5, 2, 3);
    Convolution.im2col(input, kernel[0], kernel[1], strides[0], strides[1], pad[0], pad[1], convolutionMode == ConvolutionMode.Same, col2);
    //Reshape to 2d; should be zero-copy reshape due to permute above
    INDArray col2d = col.reshape('c', miniBatch * inDepth * outH * outW, kernel[0] * kernel[1]);
    INDArray reduced;
    switch(layerConf().getPoolingType()) {
        case AVG:
            reduced = col2d.mean(1);
            break;
        case MAX:
            reduced = col2d.max(1);
            break;
        case PNORM:
            // pnorm pooling is used for signal loss recovery it is mixed with avg pooling,
            // applying the exponent to the input and recovering the signal by multiplying the kernel of
            // the pooling layer and then applying the same inverse exponent
            int pnorm = layerConf().getPnorm();
            Transforms.abs(col2d, false);
            Transforms.pow(col2d, pnorm, false);
            reduced = col2d.sum(1);
            Transforms.pow(reduced, (1.0 / pnorm), false);
            break;
        case NONE:
            return input;
        default:
            throw new IllegalStateException("Unknown/not supported pooling type: " + layerConf().getPoolingType());
    }
    return reduced.reshape('c', miniBatch, inDepth, outH, outW);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DL4JInvalidInputException(org.deeplearning4j.exception.DL4JInvalidInputException)

Example 7 with DL4JInvalidInputException

use of org.deeplearning4j.exception.DL4JInvalidInputException in project deeplearning4j by deeplearning4j.

the class EmbeddingLayer method preOutput.

@Override
public INDArray preOutput(boolean training) {
    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");
    }
    int[] indexes = new int[input.length()];
    for (int i = 0; i < indexes.length; i++) indexes[i] = input.getInt(i, 0);
    INDArray weights = getParam(DefaultParamInitializer.WEIGHT_KEY);
    INDArray bias = getParam(DefaultParamInitializer.BIAS_KEY);
    //INDArray rows = weights.getRows(indexes);
    /*        INDArray rows = Nd4j.createUninitialized(new int[]{indexes.length,weights.size(1)},'c');
        
        for( int i=0; i<indexes.length; i++ ){
            rows.putRow(i,weights.getRow(indexes[i]));
        }
        */
    INDArray rows = Nd4j.pullRows(weights, 1, indexes);
    rows.addiRowVector(bias);
    return rows;
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DL4JInvalidInputException(org.deeplearning4j.exception.DL4JInvalidInputException)

Example 8 with DL4JInvalidInputException

use of org.deeplearning4j.exception.DL4JInvalidInputException in project deeplearning4j by deeplearning4j.

the class BaseOutputLayer method getGradientsAndDelta.

/** Returns tuple: {Gradient,Delta,Output} given preOut */
private Pair<Gradient, INDArray> getGradientsAndDelta(INDArray preOut) {
    ILossFunction lossFunction = layerConf().getLossFn();
    INDArray labels2d = getLabels2d();
    if (labels2d.size(1) != preOut.size(1)) {
        throw new DL4JInvalidInputException("Labels array numColumns (size(1) = " + labels2d.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOut.size(1) + ")");
    }
    //INDArray delta = lossFunction.computeGradient(labels2d, preOut, layerConf().getActivationFunction(), maskArray);
    INDArray delta = lossFunction.computeGradient(labels2d, preOut, layerConf().getActivationFn(), maskArray);
    Gradient gradient = new DefaultGradient();
    INDArray weightGradView = gradientViews.get(DefaultParamInitializer.WEIGHT_KEY);
    INDArray biasGradView = gradientViews.get(DefaultParamInitializer.BIAS_KEY);
    //Equivalent to:  weightGradView.assign(input.transpose().mmul(delta));
    Nd4j.gemm(input, delta, weightGradView, true, false, 1.0, 0.0);
    biasGradView.assign(delta.sum(0));
    gradient.gradientForVariable().put(DefaultParamInitializer.WEIGHT_KEY, weightGradView);
    gradient.gradientForVariable().put(DefaultParamInitializer.BIAS_KEY, biasGradView);
    return new Pair<>(gradient, delta);
}
Also used : Gradient(org.deeplearning4j.nn.gradient.Gradient) DefaultGradient(org.deeplearning4j.nn.gradient.DefaultGradient) DefaultGradient(org.deeplearning4j.nn.gradient.DefaultGradient) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ILossFunction(org.nd4j.linalg.lossfunctions.ILossFunction) DL4JInvalidInputException(org.deeplearning4j.exception.DL4JInvalidInputException) Pair(org.deeplearning4j.berkeley.Pair)

Example 9 with DL4JInvalidInputException

use of org.deeplearning4j.exception.DL4JInvalidInputException in project deeplearning4j by deeplearning4j.

the class ConvolutionLayer method preOutput.

public INDArray preOutput(boolean training) {
    INDArray weights = getParam(ConvolutionParamInitializer.WEIGHT_KEY);
    INDArray bias = getParam(ConvolutionParamInitializer.BIAS_KEY);
    if (conf.isUseDropConnect() && training && conf.getLayer().getDropOut() > 0) {
        weights = Dropout.applyDropConnect(this, ConvolutionParamInitializer.WEIGHT_KEY);
    }
    //Input validation: expect rank 4 matrix
    if (input.rank() != 4) {
        String layerName = conf.getLayer().getLayerName();
        if (layerName == null)
            layerName = "(not named)";
        throw new DL4JInvalidInputException("Got rank " + input.rank() + " array as input to ConvolutionLayer (layer name = " + layerName + ", layer index = " + index + ") with shape " + Arrays.toString(input.shape()) + ". " + "Expected rank 4 array with shape [minibatchSize, layerInputDepth, inputHeight, inputWidth]." + (input.rank() == 2 ? " (Wrong input type (see InputType.convolutionalFlat()) or wrong data type?)" : ""));
    }
    int miniBatch = input.size(0);
    int outDepth = weights.size(0);
    int inDepth = weights.size(1);
    if (input.size(1) != inDepth) {
        String layerName = conf.getLayer().getLayerName();
        if (layerName == null)
            layerName = "(not named)";
        throw new DL4JInvalidInputException("Cannot do forward pass in Convolution layer (layer name = " + layerName + ", layer index = " + index + "): input array depth does not match CNN layer configuration" + " (data input depth = " + input.size(1) + ", [minibatch,inputDepth,height,width]=" + Arrays.toString(input.shape()) + "; expected" + " input depth = " + inDepth + ")");
    }
    int kH = weights.size(2);
    int kW = weights.size(3);
    int[] kernel = layerConf().getKernelSize();
    int[] strides = layerConf().getStride();
    int[] pad;
    int[] outSize;
    if (convolutionMode == ConvolutionMode.Same) {
        //Also performs validation
        outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, null, convolutionMode);
        pad = ConvolutionUtils.getSameModeTopLeftPadding(outSize, new int[] { input.size(2), input.size(3) }, kernel, strides);
    } else {
        pad = layerConf().getPadding();
        //Also performs validation
        outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, pad, convolutionMode);
    }
    int outH = outSize[0];
    int outW = outSize[1];
    if (helper != null && Nd4j.dataType() != DataBuffer.Type.HALF) {
        INDArray ret = helper.preOutput(input, weights, bias, kernel, strides, pad, layerConf().getCudnnAlgoMode(), convolutionMode);
        if (ret != null) {
            return ret;
        }
    }
    //im2col in the required order: want [outW,outH,miniBatch,depthIn,kH,kW], but need to input [miniBatch,depth,kH,kW,outH,outW] given the current im2col implementation
    //To get this: create an array of the order we want, permute it to the order required by im2col implementation, and then do im2col on that
    //to get old order from required order: permute(0,3,4,5,1,2)
    //Post reshaping: rows are such that minibatch varies slowest, outW fastest as we step through the rows post-reshape
    INDArray col = Nd4j.createUninitialized(new int[] { miniBatch, outH, outW, inDepth, kH, kW }, 'c');
    INDArray col2 = col.permute(0, 3, 4, 5, 1, 2);
    Convolution.im2col(input, kH, kW, strides[0], strides[1], pad[0], pad[1], convolutionMode == ConvolutionMode.Same, col2);
    INDArray reshapedCol = Shape.newShapeNoCopy(col, new int[] { miniBatch * outH * outW, inDepth * kH * kW }, false);
    //Current order of weights: [depthOut,depthIn,kH,kW], c order
    //Permute to give [kW,kH,depthIn,depthOut], f order
    //Reshape to give [kW*kH*depthIn, depthOut]. This should always be zero-copy reshape, unless weights aren't in c order for some reason
    INDArray permutedW = weights.permute(3, 2, 1, 0);
    INDArray reshapedW = permutedW.reshape('f', kW * kH * inDepth, outDepth);
    //Do the MMUL; c and f orders in, f order out. output shape: [miniBatch*outH*outW,depthOut]
    INDArray z = reshapedCol.mmul(reshapedW);
    //Add biases, before reshaping. Note that biases are [1,depthOut] and currently z is [miniBatch*outH*outW,depthOut] -> addiRowVector
    z.addiRowVector(bias);
    //Now, reshape to [outW,outH,miniBatch,outDepth], and permute to have correct output order: [miniBath,outDepth,outH,outW];
    z = Shape.newShapeNoCopy(z, new int[] { outW, outH, miniBatch, outDepth }, true);
    return z.permute(2, 3, 1, 0);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DL4JInvalidInputException(org.deeplearning4j.exception.DL4JInvalidInputException)

Example 10 with DL4JInvalidInputException

use of org.deeplearning4j.exception.DL4JInvalidInputException in project deeplearning4j by deeplearning4j.

the class Subsampling1DLayer method backpropGradient.

@Override
public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon) {
    if (epsilon.rank() != 3)
        throw new DL4JInvalidInputException("Got rank " + epsilon.rank() + " array as epsilon for Subsampling1DLayer backprop with shape " + Arrays.toString(epsilon.shape()) + ". Expected rank 3 array with shape [minibatchSize, features, length].");
    // add singleton fourth dimension to input and next layer's epsilon
    INDArray origInput = input;
    input = input.reshape(input.size(0), input.size(1), input.size(2), 1);
    epsilon = epsilon.reshape(epsilon.size(0), epsilon.size(1), epsilon.size(2), 1);
    // call 2D SubsamplingLayer's backpropGradient method
    Pair<Gradient, INDArray> gradientEpsNext = super.backpropGradient(epsilon);
    INDArray epsNext = gradientEpsNext.getSecond();
    // remove singleton fourth dimension from input and current epsilon
    input = origInput;
    epsNext = epsNext.reshape(epsNext.size(0), epsNext.size(1), epsNext.size(2));
    return new Pair<>(gradientEpsNext.getFirst(), epsNext);
}
Also used : Gradient(org.deeplearning4j.nn.gradient.Gradient) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DL4JInvalidInputException(org.deeplearning4j.exception.DL4JInvalidInputException) Pair(org.deeplearning4j.berkeley.Pair)

Aggregations

DL4JInvalidInputException (org.deeplearning4j.exception.DL4JInvalidInputException)15 INDArray (org.nd4j.linalg.api.ndarray.INDArray)14 Pair (org.deeplearning4j.berkeley.Pair)5 Gradient (org.deeplearning4j.nn.gradient.Gradient)4 ArrayList (java.util.ArrayList)3 Writable (org.datavec.api.writable.Writable)3 NDArrayWritable (org.datavec.common.data.NDArrayWritable)3 DefaultGradient (org.deeplearning4j.nn.gradient.DefaultGradient)2 ILossFunction (org.nd4j.linalg.lossfunctions.ILossFunction)2 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 WritableConverterException (org.datavec.api.io.converters.WritableConverterException)1 InMemoryLookupTable (org.deeplearning4j.models.embeddings.inmemory.InMemoryLookupTable)1 ShallowSequenceElement (org.deeplearning4j.models.sequencevectors.sequence.ShallowSequenceElement)1 StaticWord2Vec (org.deeplearning4j.models.word2vec.StaticWord2Vec)1 VocabWord (org.deeplearning4j.models.word2vec.VocabWord)1 Word2Vec (org.deeplearning4j.models.word2vec.Word2Vec)1