Java Code Examples for org.deeplearning4j.nn.conf.inputs.InputType#recurrent()

The following examples show how to use org.deeplearning4j.nn.conf.inputs.InputType#recurrent() . 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: PrimaryCapsules.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != Type.CNN) {
        throw new IllegalStateException("Invalid input for Primary Capsules layer (layer name = \""
                + layerName + "\"): expect CNN input.  Got: " + inputType);
    }

    if(capsules > 0){
        return InputType.recurrent(capsules, capsuleDimensions);
    } else {

        InputTypeConvolutional out = (InputTypeConvolutional) InputTypeUtil
                .getOutputTypeCnnLayers(inputType, kernelSize, stride, padding, dilation, convolutionMode,
                        capsuleDimensions * channels, -1, getLayerName(), PrimaryCapsules.class);

        return InputType.recurrent((int) (out.getChannels() * out.getHeight() * out.getWidth() / capsuleDimensions),
                capsuleDimensions);
    }
}
 
Example 2
Source File: Subsampling1DLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Subsampling1D layer (layer name=\"" + getLayerName()
                        + "\"): Expected RNN input, got " + inputType);
    }
    InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) inputType;
    long inputTsLength = r.getTimeSeriesLength();
    long outLength;
    if (inputTsLength < 0) {
        //Probably: user did InputType.recurrent(x) without specifying sequence length
        outLength = -1;
    } else {
        outLength = Convolution1DUtils.getOutputSize(inputTsLength, kernelSize[0], stride[0], padding[0],
                        convolutionMode, dilation[0]);
    }
    return InputType.recurrent(r.getSize(), outLength, r.getFormat());
}
 
Example 3
Source File: SelfAttentionLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Self Attention layer (layer index = " + layerIndex
                + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                + inputType);
    }

    InputType.InputTypeRecurrent itr = (InputType.InputTypeRecurrent) inputType;

    if(projectInput){
        return InputType.recurrent(nOut, itr.getTimeSeriesLength());
    }else{
        return InputType.recurrent(nIn, itr.getTimeSeriesLength());
    }
}
 
Example 4
Source File: Bidirectional.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    InputType outOrig = fwd.getOutputType(layerIndex, inputType);

    if (fwd instanceof LastTimeStep) {
        InputType.InputTypeFeedForward ff = (InputType.InputTypeFeedForward) outOrig;
        if (mode == Mode.CONCAT) {
            return InputType.feedForward(2 * ff.getSize());
        } else {
            return ff;
        }
    } else {
        InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) outOrig;
        if (mode == Mode.CONCAT) {
            return InputType.recurrent(2 * r.getSize(), getRNNDataFormat());
        } else {
            return r;
        }
    }
}
 
Example 5
Source File: ReshapeVertex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
    //Infer output shape from specified shape:
    switch (newShape.length) {
        case 2:
            return InputType.feedForward(newShape[1]);
        case 3:
            return InputType.recurrent(newShape[1]);
        case 4:
            return InputType.convolutional(newShape[2], newShape[3], newShape[1]); //[mb,d,h,w] for activations
        default:
            throw new UnsupportedOperationException(
                            "Cannot infer input type for reshape array " + Arrays.toString(newShape));
    }
}
 
Example 6
Source File: TFOpLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int idx, InputType inputType){
    long[] shape = inputType.getShape(true);
    TFOpLayerImpl tempLayer = new TFOpLayerImpl(nodeDef, constants, null, null);
    long[] outputShape = tempLayer.getOutputShape(shape);
    if (outputShape.length == 3){
        return InputType.recurrent(outputShape[2], outputShape[1], RNNFormat.NWC);
    }
    return InputType.inferInputType(Nd4j.create(outputShape));

}
 
Example 7
Source File: EmbeddingSequenceLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || (inputType.getType() != InputType.Type.FF && inputType.getType() != InputType.Type.RNN)) {
        throw new IllegalStateException("Invalid input for Embedding layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect FF/RNN input type. Got: " + inputType);
    }
    return InputType.recurrent(nOut, inputLength, outputFormat);
}
 
Example 8
Source File: AttentionVertex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
    InputType.InputTypeRecurrent queries = (InputType.InputTypeRecurrent) vertexInputs[0];

    if(projectInput){
        return InputType.recurrent(nOut, queries.getTimeSeriesLength());
    }else{
        return InputType.recurrent(nInValues, queries.getTimeSeriesLength());
    }
}
 
Example 9
Source File: RnnOutputLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input type for RnnOutputLayer (layer index = " + layerIndex
                        + ", layer name=\"" + getLayerName() + "\"): Expected RNN input, got " + inputType);
    }
    InputType.InputTypeRecurrent itr = (InputType.InputTypeRecurrent) inputType;

    return InputType.recurrent(nOut, itr.getTimeSeriesLength(), itr.getFormat());
}
 
Example 10
Source File: Upsampling1D.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for 1D Upsampling layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                        + inputType);
    }
    InputType.InputTypeRecurrent recurrent = (InputType.InputTypeRecurrent) inputType;
    long outLength = recurrent.getTimeSeriesLength();
    if (outLength > 0) {
        outLength *= size[0];
    }
    return InputType.recurrent(recurrent.getSize(), outLength);
}
 
Example 11
Source File: RepeatVector.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.FF) {
        throw new IllegalStateException("Invalid input for RepeatVector layer (layer name=\"" + getLayerName()
                        + "\"): Expected FF input, got " + inputType);
    }
    InputType.InputTypeFeedForward ffInput = (InputType.InputTypeFeedForward) inputType;
    return InputType.recurrent(ffInput.getSize(), n, this.dataFormat);
}
 
Example 12
Source File: RecurrentAttentionLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Recurrent Attention layer (layer index = " + layerIndex
                + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                + inputType);
    }

    InputType.InputTypeRecurrent itr = (InputType.InputTypeRecurrent) inputType;


    return InputType.recurrent(nOut, itr.getTimeSeriesLength());
}
 
Example 13
Source File: TimeDistributed.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Only RNN input type is supported as input to TimeDistributed layer (layer #" + layerIndex + ")");
    }

    InputType.InputTypeRecurrent rnn = (InputType.InputTypeRecurrent) inputType;
    InputType ff = InputType.feedForward(rnn.getSize());
    InputType ffOut = underlying.getOutputType(layerIndex, ff);
    return InputType.recurrent(ffOut.arrayElementsPerExample(), rnn.getTimeSeriesLength(), rnnDataFormat);
}
 
Example 14
Source File: BaseRecurrentLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for RNN layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                        + inputType);
    }

    InputType.InputTypeRecurrent itr = (InputType.InputTypeRecurrent) inputType;

    return InputType.recurrent(nOut, itr.getTimeSeriesLength(), itr.getFormat());
}
 
Example 15
Source File: ZeroPadding1DLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for 1D CNN layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                        + inputType);
    }
    InputType.InputTypeRecurrent recurrent = (InputType.InputTypeRecurrent) inputType;
    return InputType.recurrent(recurrent.getSize(), recurrent.getTimeSeriesLength() + padding[0] + padding[1]);
}
 
Example 16
Source File: FeedForwardToRnnPreProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(InputType inputType) {
    if (inputType == null || (inputType.getType() != InputType.Type.FF
                    && inputType.getType() != InputType.Type.CNNFlat)) {
        throw new IllegalStateException("Invalid input: expected input of type FeedForward, got " + inputType);
    }

    if (inputType.getType() == InputType.Type.FF) {
        InputType.InputTypeFeedForward ff = (InputType.InputTypeFeedForward) inputType;
        return InputType.recurrent(ff.getSize(), rnnDataFormat);
    } else {
        InputType.InputTypeConvolutionalFlat cf = (InputType.InputTypeConvolutionalFlat) inputType;
        return InputType.recurrent(cf.getFlattenedSize(), rnnDataFormat);
    }
}
 
Example 17
Source File: CnnToRnnPreProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.CNN) {
        throw new IllegalStateException("Invalid input type: Expected input of type CNN, got " + inputType);
    }

    InputType.InputTypeConvolutional c = (InputType.InputTypeConvolutional) inputType;
    val outSize = c.getChannels() * c.getHeight() * c.getWidth();
    return InputType.recurrent(outSize, rnnDataFormat);
}
 
Example 18
Source File: SubsetVertex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
    if (vertexInputs.length != 1) {
        throw new InvalidInputTypeException(
                        "SubsetVertex expects single input type. Received: " + Arrays.toString(vertexInputs));
    }

    switch (vertexInputs[0].getType()) {
        case FF:
            return InputType.feedForward(to - from + 1);
        case RNN:
            return InputType.recurrent(to - from + 1);
        case CNN:
            InputType.InputTypeConvolutional conv = (InputType.InputTypeConvolutional) vertexInputs[0];
            val depth = conv.getChannels();
            if (to >= depth) {
                throw new InvalidInputTypeException("Invalid range: Cannot select channels subset [" + from + "," + to
                                + "] inclusive from CNN activations with " + " [channels,width,height] = [" + depth
                                + "," + conv.getWidth() + "," + conv.getHeight() + "]");
            }
            return InputType.convolutional(conv.getHeight(), conv.getWidth(), from - to + 1);
        case CNNFlat:
            //TODO work out how to do this - could be difficult...
            throw new UnsupportedOperationException(
                            "Subsetting data in flattened convolutional format not yet supported");
        default:
            throw new RuntimeException("Unknown input type: " + vertexInputs[0]);
    }
}
 
Example 19
Source File: PoolHelperVertex.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
    if (vertexInputs.length == 1)
        return vertexInputs[0];
    InputType first = vertexInputs[0];
    if (first.getType() == InputType.Type.CNNFlat) {
        //TODO
        //Merging flattened CNN format data could be messy?
        throw new InvalidInputTypeException(
                        "Invalid input: MergeVertex cannot currently merge CNN data in flattened format. Got: "
                                        + vertexInputs);
    } else if (first.getType() != InputType.Type.CNN) {
        //FF or RNN data inputs
        int size = 0;
        InputType.Type type = null;
        for (int i = 0; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != first.getType()) {
                throw new InvalidInputTypeException(
                                "Invalid input: MergeVertex cannot merge activations of different types:"
                                                + " first type = " + first.getType() + ", input type " + (i + 1)
                                                + " = " + vertexInputs[i].getType());
            }

            long thisSize;
            switch (vertexInputs[i].getType()) {
                case FF:
                    thisSize = ((InputType.InputTypeFeedForward) vertexInputs[i]).getSize();
                    type = InputType.Type.FF;
                    break;
                case RNN:
                    thisSize = ((InputType.InputTypeRecurrent) vertexInputs[i]).getSize();
                    type = InputType.Type.RNN;
                    break;
                default:
                    throw new IllegalStateException("Unknown input type: " + vertexInputs[i]); //Should never happen
            }
            if (thisSize <= 0) {//Size is not defined
                size = -1;
            } else {
                size += thisSize;
            }
        }

        if (size > 0) {
            //Size is specified
            if (type == InputType.Type.FF)
                return InputType.feedForward(size);
            else
                return InputType.recurrent(size);
        } else {
            //size is unknown
            if (type == InputType.Type.FF)
                return InputType.feedForward(-1);
            else
                return InputType.recurrent(-1);
        }
    } else {
        //CNN inputs... also check that the channels, width and heights match:
        InputType.InputTypeConvolutional firstConv = (InputType.InputTypeConvolutional) first;

        val fd = firstConv.getChannels();
        val fw = firstConv.getWidth();
        val fh = firstConv.getHeight();

        long depthSum = fd;

        for (int i = 1; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != InputType.Type.CNN) {
                throw new InvalidInputTypeException(
                                "Invalid input: MergeVertex cannot process activations of different types:"
                                                + " first type = " + InputType.Type.CNN + ", input type " + (i + 1)
                                                + " = " + vertexInputs[i].getType());
            }

            InputType.InputTypeConvolutional otherConv = (InputType.InputTypeConvolutional) vertexInputs[i];

            long od = otherConv.getChannels();
            long ow = otherConv.getWidth();
            long oh = otherConv.getHeight();

            if (fw != ow || fh != oh) {
                throw new InvalidInputTypeException(
                                "Invalid input: MergeVertex cannot merge CNN activations of different width/heights:"
                                                + "first [channels,width,height] = [" + fd + "," + fw + "," + fh
                                                + "], input " + i + " = [" + od + "," + ow + "," + oh + "]");
            }

            depthSum += od;
        }

        return InputType.convolutional(fh, fw, depthSum);
    }
}
 
Example 20
Source File: CapsuleLayer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    return InputType.recurrent(capsules, capsuleDimensions);
}