Class AbstractLayer<LayerConfT extends Layer>

    • Field Detail

      • preOutput

        protected INDArray preOutput
      • dropoutApplied

        protected boolean dropoutApplied
      • index

        protected int index
      • maskArray

        protected INDArray maskArray
      • inputModificationAllowed

        protected boolean inputModificationAllowed
      • iterationCount

        protected int iterationCount
      • epochCount

        protected int epochCount
    • Method Detail

      • setCacheMode

        public void setCacheMode​(CacheMode mode)
        Description copied from interface: Layer
        This method sets given CacheMode for current layer
        Specified by:
        setCacheMode in interface Layer
      • layerId

        protected String layerId()
      • getEpochCount

        public int getEpochCount()
        Specified by:
        getEpochCount in interface Layer
        Returns:
        The current epoch count (number of training epochs passed) for the layer/network
      • setEpochCount

        public void setEpochCount​(int epochCount)
        Description copied from interface: Layer
        Set the current epoch count (number of epochs passed ) for the layer/network
        Specified by:
        setEpochCount in interface Layer
      • init

        public void init()
        Init the model
        Specified by:
        init in interface Model
      • getIndex

        public int getIndex()
        Description copied from interface: Layer
        Get the layer index.
        Specified by:
        getIndex in interface Layer
      • setIndex

        public void setIndex​(int index)
        Description copied from interface: Layer
        Set the layer index.
        Specified by:
        setIndex in interface Layer
      • addListeners

        public void addListeners​(TrainingListener... listeners)
        This method ADDS additional TrainingListener to existing listeners
        Specified by:
        addListeners in interface Model
        Parameters:
        listeners -
      • update

        public void update​(Gradient gradient)
        Description copied from interface: Model
        Update layer weights and biases with gradient change
        Specified by:
        update in interface Model
      • update

        public void update​(INDArray gradient,
                           String paramType)
        Description copied from interface: Model
        Perform one update applying the gradient
        Specified by:
        update in interface Model
        Parameters:
        gradient - the gradient to apply
      • getOptimizer

        public ConvexOptimizer getOptimizer()
        Description copied from interface: Model
        Returns this models optimizer
        Specified by:
        getOptimizer in interface Model
        Returns:
        this models optimizer
      • params

        public INDArray params()
        Returns the parameters of the neural network as a flattened row vector
        Specified by:
        params in interface Model
        Specified by:
        params in interface Trainable
        Returns:
        the parameters of the neural network
      • getParam

        public INDArray getParam​(String param)
        Description copied from interface: Model
        Get the parameter
        Specified by:
        getParam in interface Model
        Parameters:
        param - the key of the parameter
        Returns:
        the parameter vector/matrix with that particular key
      • setParam

        public void setParam​(String key,
                             INDArray val)
        Description copied from interface: Model
        Set the parameter with a new ndarray
        Specified by:
        setParam in interface Model
        Parameters:
        key - the key to se t
        val - the new ndarray
      • setParams

        public void setParams​(INDArray params)
        Description copied from interface: Model
        Set the parameters for this model. This expects a linear ndarray which then be unpacked internally relative to the expected ordering of the model
        Specified by:
        setParams in interface Model
        Parameters:
        params - the parameters for the model
      • setParams

        protected void setParams​(INDArray params,
                                 char order)
      • setParamsViewArray

        public void setParamsViewArray​(INDArray params)
        Description copied from interface: Model
        Set the initial parameters array as a view of the full (backprop) network parameters NOTE: this is intended to be used internally in MultiLayerNetwork and ComputationGraph, not by users.
        Specified by:
        setParamsViewArray in interface Model
        Parameters:
        params - a 1 x nParams row vector that is a view of the larger (MLN/CG) parameters array
      • setBackpropGradientsViewArray

        public void setBackpropGradientsViewArray​(INDArray gradients)
        Description copied from interface: Model
        Set the gradients array as a view of the full (backprop) network parameters NOTE: this is intended to be used internally in MultiLayerNetwork and ComputationGraph, not by users.
        Specified by:
        setBackpropGradientsViewArray in interface Model
        Parameters:
        gradients - a 1 x nParams row vector that is a view of the larger (MLN/CG) gradients array
      • setParamTable

        public void setParamTable​(Map<String,​INDArray> paramTable)
        Description copied from interface: Model
        Setter for the param table
        Specified by:
        setParamTable in interface Model
      • paramTable

        public Map<String,​INDArray> paramTable​(boolean backpropParamsOnly)
        Description copied from interface: Model
        Table of parameters by key, for backprop For many models (dense layers, etc) - all parameters are backprop parameters
        Specified by:
        paramTable in interface Model
        Specified by:
        paramTable in interface Trainable
        Parameters:
        backpropParamsOnly - If true, return backprop params only. If false: return all params (equivalent to paramsTable())
        Returns:
        Parameter table
      • applyMask

        protected void applyMask​(INDArray to)
      • activate

        public INDArray activate​(INDArray input,
                                 boolean training,
                                 LayerWorkspaceMgr workspaceMgr)
        Description copied from interface: Layer
        Perform forward pass and return the activations array with the specified input
        Specified by:
        activate in interface Layer
        Parameters:
        input - the input to use
        training - train or test mode
        workspaceMgr - Workspace manager.
        Returns:
        Activations array. Note that the returned array should be placed in the ArrayType.ACTIVATIONS workspace via the workspace manager
      • calcRegularizationScore

        public double calcRegularizationScore​(boolean backpropParamsOnly)
        Description copied from interface: Layer
        Calculate the regularization component of the score, for the parameters in this layer
        For example, the L1, L2 and/or weight decay components of the loss function
        Specified by:
        calcRegularizationScore in interface Layer
        Parameters:
        backpropParamsOnly - If true: calculate regularization score based on backprop params only. If false: calculate based on all params (including pretrain params, if any)
        Returns:
        the regularization score of
      • batchSize

        public int batchSize()
        Description copied from interface: Model
        The current inputs batch size
        Specified by:
        batchSize in interface Model
        Returns:
        the current inputs batch size
      • conf

        public NeuralNetConfiguration conf()
        Description copied from interface: Model
        The configuration for the neural network
        Specified by:
        conf in interface Model
        Returns:
        the configuration for the neural network
      • clear

        public void clear()
        Description copied from interface: Model
        Clear input
        Specified by:
        clear in interface Model
      • applyDropOutIfNecessary

        protected void applyDropOutIfNecessary​(boolean training,
                                               LayerWorkspaceMgr workspaceMgr)
      • backpropDropOutIfPresent

        protected INDArray backpropDropOutIfPresent​(INDArray epsilon)
      • type

        public Layer.Type type()
        Description copied from interface: Layer
        Returns the layer type
        Specified by:
        type in interface Layer
        Returns:
      • numParams

        public long numParams()
        The number of parameters for the model
        Specified by:
        numParams in interface Model
        Specified by:
        numParams in interface Trainable
        Returns:
        the number of parameters for the model
      • numParams

        public long numParams​(boolean backwards)
        Description copied from interface: Model
        the number of parameters for the model
        Specified by:
        numParams in interface Model
        Returns:
        the number of parameters for the model
      • fit

        public void fit​(INDArray input,
                        LayerWorkspaceMgr workspaceMgr)
        Description copied from interface: Model
        Fit the model to the given data
        Specified by:
        fit in interface Model
        Parameters:
        input - the data to fit the model to
      • gradientAndScore

        public Pair<Gradient,​Double> gradientAndScore()
        Description copied from interface: Model
        Get the gradient and score
        Specified by:
        gradientAndScore in interface Model
        Returns:
        the gradient and score
      • input

        public INDArray input()
        Description copied from interface: Model
        The input/feature matrix for the model
        Specified by:
        input in interface Model
        Returns:
        the input/feature matrix for the model
      • setInputMiniBatchSize

        public void setInputMiniBatchSize​(int size)
        Description copied from interface: Layer
        Set current/last input mini-batch size.
        Used for score and gradient calculations. Mini batch size may be different from getInput().size(0) due to reshaping operations - for example, when using RNNs with DenseLayer and OutputLayer. Called automatically during forward pass.
        Specified by:
        setInputMiniBatchSize in interface Layer
      • feedForwardMaskArray

        public Pair<INDArray,​MaskState> feedForwardMaskArray​(INDArray maskArray,
                                                                   MaskState currentMaskState,
                                                                   int minibatchSize)
        Description copied from interface: Layer
        Feed forward the input mask array, setting in the layer as appropriate. This allows different layers to handle masks differently - for example, bidirectional RNNs and normal RNNs operate differently with masks (the former sets activations to 0 outside of the data present region (and keeps the mask active for future layers like dense layers), whereas normal RNNs don't zero out the activations/errors )instead relying on backpropagated error arrays to handle the variable length case.
        This is also used for example for networks that contain global pooling layers, arbitrary preprocessors, etc.
        Specified by:
        feedForwardMaskArray in interface Layer
        Parameters:
        maskArray - Mask array to set
        currentMaskState - Current state of the mask - see MaskState
        minibatchSize - Current minibatch size. Needs to be known as it cannot always be inferred from the activations array due to reshaping (such as a DenseLayer within a recurrent neural network)
        Returns:
        New mask array after this layer, along with the new mask state.
      • gradient

        public Gradient gradient()
        Description copied from interface: Model
        Get the gradient. Note that this method will not calculate the gradient, it will rather return the gradient that has been computed before. For calculating the gradient, see Model.computeGradientAndScore(LayerWorkspaceMgr) } .
        Specified by:
        gradient in interface Model
        Returns:
        the gradient for this model, as calculated before
      • fit

        public void fit()
        Description copied from interface: Model
        All models have a fit method
        Specified by:
        fit in interface Model
      • score

        public double score()
        Description copied from interface: Model
        The score for the model
        Specified by:
        score in interface Model
        Returns:
        the score for the model
      • applyConstraints

        public void applyConstraints​(int iteration,
                                     int epoch)
        Description copied from interface: Model
        Apply any constraints to the model
        Specified by:
        applyConstraints in interface Model
      • assertInputSet

        public void assertInputSet​(boolean backprop)
      • allowInputModification

        public void allowInputModification​(boolean allow)
        Description copied from interface: Layer
        A performance optimization: mark whether the layer is allowed to modify its input array in-place. In many cases, this is totally safe - in others, the input array will be shared by multiple layers, and hence it's not safe to modify the input array. This is usually used by ops such as dropout.
        Specified by:
        allowInputModification in interface Layer
        Parameters:
        allow - If true: the input array is safe to modify. If false: the input array should be copied before it is modified (i.e., in-place modifications are un-safe)
      • getHelper

        public LayerHelper getHelper()
        Specified by:
        getHelper in interface Layer
        Returns:
        Get the layer helper, if any
      • updaterDivideByMinibatch

        public boolean updaterDivideByMinibatch​(String paramName)
        Description copied from interface: Trainable
        DL4J layers typically produce the sum of the gradients during the backward pass for each layer, and if required (if minibatch=true) then divide by the minibatch size.
        However, there are some exceptions, such as the batch norm mean/variance estimate parameters: these "gradients" are actually not gradients, but are updates to be applied directly to the parameter vector. Put another way, most gradients should be divided by the minibatch to get the average; some "gradients" are actually final updates already, and should not be divided by the minibatch size.
        Specified by:
        updaterDivideByMinibatch in interface Trainable
        Parameters:
        paramName - Name of the parameter
        Returns:
        True if gradients should be divided by minibatch (most params); false otherwise (edge cases like batch norm mean/variance estimates)
      • close

        public void close()
        Specified by:
        close in interface Model