Java Code Examples for org.deeplearning4j.nn.conf.inputs.InputType#InputTypeRecurrent

The following examples show how to use org.deeplearning4j.nn.conf.inputs.InputType#InputTypeRecurrent . 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: KerasLoss.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get DL4J LossLayer.
 *
 * @return LossLayer
 */
public FeedForwardLayer getLossLayer(InputType type) throws UnsupportedKerasConfigurationException {
    if (type instanceof InputType.InputTypeFeedForward) {
        this.layer = new LossLayer.Builder(loss).name(this.layerName).activation(Activation.IDENTITY).build();
    }
    else if (type instanceof  InputType.InputTypeRecurrent) {
        this.layer = new RnnLossLayer.Builder(loss).name(this.layerName).activation(Activation.IDENTITY).build();
    }
    else if (type instanceof InputType.InputTypeConvolutional) {
        this.layer = new CnnLossLayer.Builder(loss).name(this.layerName).activation(Activation.IDENTITY).build();
    } else {
        throw new UnsupportedKerasConfigurationException("Unsupported output layer type"
                + "got : " + type.toString());
    }
    return (FeedForwardLayer) this.layer;
}
 
Example 2
Source File: Upsampling1D.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public LayerMemoryReport getMemoryReport(InputType inputType) {
    InputType.InputTypeRecurrent recurrent = (InputType.InputTypeRecurrent) inputType;
    InputType.InputTypeRecurrent outputType = (InputType.InputTypeRecurrent) getOutputType(-1, inputType);

    long im2colSizePerEx = recurrent.getSize() * outputType.getTimeSeriesLength() * size[0];
    long trainingWorkingSizePerEx = im2colSizePerEx;
    if (getIDropout() != null) {
        trainingWorkingSizePerEx += inputType.arrayElementsPerExample();
    }

    return new LayerMemoryReport.Builder(layerName, Upsampling1D.class, inputType, outputType).standardMemory(0, 0) //No params
                    .workingMemory(0, im2colSizePerEx, 0, trainingWorkingSizePerEx)
                    .cacheMemory(MemoryReport.CACHE_MODE_ALL_ZEROS, MemoryReport.CACHE_MODE_ALL_ZEROS) //No caching
                    .build();
}
 
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: 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 5
Source File: Convolution1DLayer.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 1D CNN layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                        + inputType);
    }
    InputType.InputTypeRecurrent it = (InputType.InputTypeRecurrent) inputType;
    long inputTsLength = it.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(nOut, outLength, rnnDataFormat);
}
 
Example 6
Source File: LocallyConnected1D.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 IllegalArgumentException("Provided input type for locally connected 1D layers has to be "
                        + "of CNN1D/RNN type, got: " + inputType);
    }
    // dynamically compute input size from input type
    InputType.InputTypeRecurrent rnnType = (InputType.InputTypeRecurrent) inputType;
    this.inputSize = (int) rnnType.getTimeSeriesLength();
    computeOutputSize();

    return InputTypeUtil.getOutputTypeCnn1DLayers(inputType, kernel, stride, padding, 1, cm, nOut, layerIndex,
                    getLayerName(), LocallyConnected1D.class);
}
 
Example 7
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 8
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 9
Source File: RecurrentAttentionLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Recurrent Attention layer (layer name = \"" + getLayerName()
                + "\"): expect RNN input type with size > 0. Got: " + inputType);
    }

    if (nIn <= 0 || override) {
        InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) inputType;
        this.nIn = r.getSize();
    }
}
 
Example 10
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 11
Source File: TimeDistributed.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Only RNN input type is supported as input to TimeDistributed layer");
    }

    InputType.InputTypeRecurrent rnn = (InputType.InputTypeRecurrent) inputType;
    InputType ff = InputType.feedForward(rnn.getSize());
    this.rnnDataFormat = rnn.getFormat();
    underlying.setNIn(ff, override);
}
 
Example 12
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 13
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 14
Source File: RnnOutputLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input type for RnnOutputLayer (layer name=\"" + getLayerName()
                        + "\"): Expected RNN input, got " + inputType);
    }

    InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) inputType;
    this.rnnDataFormat = r.getFormat();
    if (nIn <= 0 || override) {
        this.nIn = r.getSize();
    }
}
 
Example 15
Source File: SelfAttentionLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Self Attention layer (layer name = \"" + getLayerName()
                + "\"): expect RNN input type with size > 0. Got: " + inputType);
    }

    if (nIn <= 0 || override) {
        InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) inputType;
        this.nIn = r.getSize();
    }
}
 
Example 16
Source File: Convolution1DLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for 1D CNN layer (layer name = \"" + getLayerName()
                        + "\"): expect RNN input type with size > 0. Got: " + inputType);
    }

    InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) inputType;
    if (nIn <= 0 || override) {
        this.nIn = r.getSize();
    }
    this.rnnDataFormat = r.getFormat();
}
 
Example 17
Source File: Cropping1D.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 Cropping layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                        + inputType);
    }
    InputType.InputTypeRecurrent cnn1d = (InputType.InputTypeRecurrent) inputType;
    val length = cnn1d.getTimeSeriesLength();
    val outLength = length - cropping[0] - cropping[1];
    return InputType.recurrent(cnn1d.getSize(), outLength);
}
 
Example 18
Source File: RnnToFeedForwardPreProcessor.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.RNN) {
        throw new IllegalStateException("Invalid input: expected input of type RNN, got " + inputType);
    }

    InputType.InputTypeRecurrent rnn = (InputType.InputTypeRecurrent) inputType;
    return InputType.feedForward(rnn.getSize(), rnn.getFormat());
}
 
Example 19
Source File: CapsuleLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if(inputType == null || inputType.getType() != Type.RNN) {
        throw new IllegalStateException("Invalid input for Capsule layer (layer name = \""
                + layerName + "\"): expect RNN input.  Got: " + inputType);
    }

    if(inputCapsules <= 0 || inputCapsuleDimensions <= 0){
        InputType.InputTypeRecurrent ir = (InputTypeRecurrent) inputType;
        inputCapsules = ir.getSize();
        inputCapsuleDimensions = ir.getTimeSeriesLength();
    }

}
 
Example 20
Source File: KerasInput.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Get layer output type.
 *
 * @param inputType Array of InputTypes
 * @return output type as InputType
 * @throws InvalidKerasConfigurationException     Invalid Keras config
 * @throws UnsupportedKerasConfigurationException Unsupported Keras config
 */
@Override
public InputType getOutputType(InputType... inputType)
        throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
    if (inputType.length > 0)
        log.warn("Keras Input layer does not accept inputs (received " + inputType.length + "). Ignoring.");
    InputType myInputType;
    switch (this.inputShape.length) {
        case 1:
            myInputType = new InputType.InputTypeFeedForward(this.inputShape[0], null);
            break;
        case 2:
            if(this.dimOrder != null) {
                switch (this.dimOrder) {
                    case TENSORFLOW:    //NWC == channels_last
                        myInputType = new InputType.InputTypeRecurrent(this.inputShape[1], this.inputShape[0], RNNFormat.NWC);
                        break;
                    case THEANO:        //NCW == channels_first
                        myInputType = new InputType.InputTypeRecurrent(this.inputShape[0], this.inputShape[1], RNNFormat.NCW);
                        break;
                    case NONE:
                        //Assume RNN in [mb, seqLen, size] format
                        myInputType = new InputType.InputTypeRecurrent(this.inputShape[1], this.inputShape[0], RNNFormat.NWC);
                        break;
                    default:
                        throw new IllegalStateException("Unknown/not supported dimension ordering: " + this.dimOrder);
                }
            } else {
                //Assume RNN in [mb, seqLen, size] format
                myInputType = new InputType.InputTypeRecurrent(this.inputShape[1], this.inputShape[0], RNNFormat.NWC);
            }

            break;
        case 3:
            switch (this.dimOrder) {
                case TENSORFLOW:
                    /* TensorFlow convolutional input: # rows, # cols, # channels */
                    myInputType = new InputType.InputTypeConvolutional(this.inputShape[0], this.inputShape[1],
                            this.inputShape[2], CNN2DFormat.NHWC);
                    break;
                case THEANO:
                    /* Theano convolutional input:     # channels, # rows, # cols */
                    myInputType = new InputType.InputTypeConvolutional(this.inputShape[1], this.inputShape[2],
                            this.inputShape[0], CNN2DFormat.NCHW);
                    break;
                default:
                    this.dimOrder = DimOrder.THEANO;
                    myInputType = new InputType.InputTypeConvolutional(this.inputShape[1], this.inputShape[2],
                            this.inputShape[0], CNN2DFormat.NCHW);
                    log.warn("Couldn't determine dim ordering / data format from model file. Older Keras " +
                            "versions may come without specified backend, in which case we assume the model was " +
                            "built with theano." );
            }
            break;
        case 4:
            switch (this.dimOrder) {
                case TENSORFLOW:
                    myInputType = new InputType.InputTypeConvolutional3D(Convolution3D.DataFormat.NDHWC,
                            this.inputShape[0], this.inputShape[1],
                            this.inputShape[2],this.inputShape[3]);
                    break;
                case THEANO:
                    myInputType = new InputType.InputTypeConvolutional3D(Convolution3D.DataFormat.NCDHW,
                            this.inputShape[3], this.inputShape[0],
                            this.inputShape[1],this.inputShape[2]);
                    break;
                default:
                    this.dimOrder = DimOrder.THEANO;
                    myInputType = new InputType.InputTypeConvolutional3D(Convolution3D.DataFormat.NCDHW,
                            this.inputShape[3], this.inputShape[0],
                            this.inputShape[1],this.inputShape[2]);
                    log.warn("Couldn't determine dim ordering / data format from model file. Older Keras " +
                            "versions may come without specified backend, in which case we assume the model was " +
                            "built with theano." );
            }
            break;
        default:
            throw new UnsupportedKerasConfigurationException(
                    "Inputs with " + this.inputShape.length + " dimensions not supported");
    }
    return myInputType;
}