Package org.deeplearning4j.nn.layers
Class AbstractLayer<LayerConfT extends Layer>
- java.lang.Object
-
- org.deeplearning4j.nn.layers.AbstractLayer<LayerConfT>
-
- All Implemented Interfaces:
Serializable,Cloneable,Layer,Model,Trainable
- Direct Known Subclasses:
ActivationLayer,BaseLayer,Cropping1DLayer,Cropping2DLayer,Cropping3DLayer,GlobalPoolingLayer,LocalResponseNormalization,MaskLayer,RepeatVector,SameDiffLayer,SameDiffOutputLayer,SpaceToBatch,SpaceToDepth,Subsampling3DLayer,SubsamplingLayer,Upsampling2D,Upsampling3D,Yolo2OutputLayer,ZeroPadding1DLayer,ZeroPadding3DLayer,ZeroPaddingLayer
public abstract class AbstractLayer<LayerConfT extends Layer> extends Object implements Layer
A layer with input and output, no parameters or gradients- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.deeplearning4j.nn.api.Layer
Layer.TrainingMode, Layer.Type
-
-
Field Summary
Fields Modifier and Type Field Description protected CacheModecacheModeprotected NeuralNetConfigurationconfprotected DataTypedataTypeprotected booleandropoutAppliedprotected intepochCountprotected intindexprotected INDArrayinputprotected booleaninputModificationAllowedprotected intiterationCountprotected INDArraymaskArrayprotected MaskStatemaskStateprotected INDArraypreOutputprotected Collection<TrainingListener>trainingListeners
-
Constructor Summary
Constructors Constructor Description AbstractLayer(NeuralNetConfiguration conf, DataType dataType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description INDArrayactivate(INDArray input, boolean training, LayerWorkspaceMgr workspaceMgr)Perform forward pass and return the activations array with the specified inputvoidaddListeners(TrainingListener... listeners)This method ADDS additional TrainingListener to existing listenersvoidallowInputModification(boolean allow)A performance optimization: mark whether the layer is allowed to modify its input array in-place.voidapplyConstraints(int iteration, int epoch)Apply any constraints to the modelprotected voidapplyDropOutIfNecessary(boolean training, LayerWorkspaceMgr workspaceMgr)protected voidapplyMask(INDArray to)voidassertInputSet(boolean backprop)protected INDArraybackpropDropOutIfPresent(INDArray epsilon)intbatchSize()The current inputs batch sizedoublecalcRegularizationScore(boolean backpropParamsOnly)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 functionvoidclear()Clear inputvoidclose()voidcomputeGradientAndScore(LayerWorkspaceMgr workspaceMgr)Update the scoreNeuralNetConfigurationconf()The configuration for the neural networkPair<INDArray,MaskState>feedForwardMaskArray(INDArray maskArray, MaskState currentMaskState, int minibatchSize)Feed forward the input mask array, setting in the layer as appropriate.voidfit()All models have a fit methodvoidfit(INDArray input, LayerWorkspaceMgr workspaceMgr)Fit the model to the given dataTrainingConfiggetConfig()intgetEpochCount()INDArraygetGradientsViewArray()LayerHelpergetHelper()intgetIndex()Get the layer index.INDArraygetInput()intgetInputMiniBatchSize()Get current/last input mini-batch size, as set by setInputMiniBatchSize(int)Collection<TrainingListener>getListeners()Get the iteration listeners for this layer.INDArraygetMaskArray()ConvexOptimizergetOptimizer()Returns this models optimizerINDArraygetParam(String param)Get the parameterGradientgradient()Get the gradient.Pair<Gradient,Double>gradientAndScore()Get the gradient and scorevoidinit()Init the modelINDArrayinput()The input/feature matrix for the modelLayerConfTlayerConf()protected StringlayerId()longnumParams()The number of parameters for the modellongnumParams(boolean backwards)the number of parameters for the modelINDArrayparams()Returns the parameters of the neural network as a flattened row vectorMap<String,INDArray>paramTable()The param tableMap<String,INDArray>paramTable(boolean backpropParamsOnly)Table of parameters by key, for backprop For many models (dense layers, etc) - all parameters are backprop parametersdoublescore()The score for the modelvoidsetBackpropGradientsViewArray(INDArray gradients)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.voidsetCacheMode(CacheMode mode)This method sets given CacheMode for current layervoidsetConf(NeuralNetConfiguration conf)Setter for the configurationvoidsetEpochCount(int epochCount)Set the current epoch count (number of epochs passed ) for the layer/networkvoidsetIndex(int index)Set the layer index.voidsetInput(INDArray input, LayerWorkspaceMgr workspaceMgr)Set the layer input.voidsetInputMiniBatchSize(int size)Set current/last input mini-batch size.
Used for score and gradient calculations.voidsetListeners(Collection<TrainingListener> listeners)Set theTrainingListeners for this model.voidsetListeners(TrainingListener... listeners)Set theTrainingListeners for this model.voidsetMaskArray(INDArray maskArray)Set the mask array.voidsetParam(String key, INDArray val)Set the parameter with a new ndarrayvoidsetParams(INDArray params)Set the parameters for this model.protected voidsetParams(INDArray params, char order)voidsetParamsViewArray(INDArray params)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.voidsetParamTable(Map<String,INDArray> paramTable)Setter for the param tableLayer.Typetype()Returns the layer typevoidupdate(Gradient gradient)Update layer weights and biases with gradient changevoidupdate(INDArray gradient, String paramType)Perform one update applying the gradientbooleanupdaterDivideByMinibatch(String paramName)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.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.deeplearning4j.nn.api.Layer
activate, backpropGradient, clearNoiseWeightParams, getIterationCount, isPretrainLayer, setIterationCount
-
-
-
-
Field Detail
-
input
protected INDArray input
-
preOutput
protected INDArray preOutput
-
conf
protected NeuralNetConfiguration conf
-
dropoutApplied
protected boolean dropoutApplied
-
trainingListeners
protected Collection<TrainingListener> trainingListeners
-
index
protected int index
-
maskArray
protected INDArray maskArray
-
maskState
protected MaskState maskState
-
cacheMode
protected CacheMode cacheMode
-
inputModificationAllowed
protected boolean inputModificationAllowed
-
dataType
protected DataType dataType
-
iterationCount
protected int iterationCount
-
epochCount
protected int epochCount
-
-
Constructor Detail
-
AbstractLayer
public AbstractLayer(NeuralNetConfiguration conf, DataType dataType)
-
-
Method Detail
-
setCacheMode
public void setCacheMode(CacheMode mode)
Description copied from interface:LayerThis method sets given CacheMode for current layer- Specified by:
setCacheModein interfaceLayer
-
layerConf
public LayerConfT layerConf()
-
getConfig
public TrainingConfig getConfig()
-
layerId
protected String layerId()
-
getInput
public INDArray getInput()
-
getEpochCount
public int getEpochCount()
- Specified by:
getEpochCountin interfaceLayer- Returns:
- The current epoch count (number of training epochs passed) for the layer/network
-
setEpochCount
public void setEpochCount(int epochCount)
Description copied from interface:LayerSet the current epoch count (number of epochs passed ) for the layer/network- Specified by:
setEpochCountin interfaceLayer
-
setInput
public void setInput(INDArray input, LayerWorkspaceMgr workspaceMgr)
Description copied from interface:LayerSet the layer input.
-
getIndex
public int getIndex()
Description copied from interface:LayerGet the layer index.
-
setIndex
public void setIndex(int index)
Description copied from interface:LayerSet the layer index.
-
getListeners
public Collection<TrainingListener> getListeners()
Description copied from interface:LayerGet the iteration listeners for this layer.- Specified by:
getListenersin interfaceLayer
-
setListeners
public void setListeners(Collection<TrainingListener> listeners)
Description copied from interface:LayerSet theTrainingListeners for this model. If any listeners have previously been set, they will be replaced by this method- Specified by:
setListenersin interfaceLayer- Specified by:
setListenersin interfaceModel
-
addListeners
public void addListeners(TrainingListener... listeners)
This method ADDS additional TrainingListener to existing listeners- Specified by:
addListenersin interfaceModel- Parameters:
listeners-
-
setListeners
public void setListeners(TrainingListener... listeners)
Description copied from interface:LayerSet theTrainingListeners for this model. If any listeners have previously been set, they will be replaced by this method- Specified by:
setListenersin interfaceLayer- Specified by:
setListenersin interfaceModel
-
computeGradientAndScore
public void computeGradientAndScore(LayerWorkspaceMgr workspaceMgr)
Description copied from interface:ModelUpdate the score- Specified by:
computeGradientAndScorein interfaceModel
-
update
public void update(Gradient gradient)
Description copied from interface:ModelUpdate layer weights and biases with gradient change
-
update
public void update(INDArray gradient, String paramType)
Description copied from interface:ModelPerform one update applying the gradient
-
getOptimizer
public ConvexOptimizer getOptimizer()
Description copied from interface:ModelReturns this models optimizer- Specified by:
getOptimizerin interfaceModel- Returns:
- this models optimizer
-
setConf
public void setConf(NeuralNetConfiguration conf)
Description copied from interface:ModelSetter for the configuration
-
params
public INDArray params()
Returns the parameters of the neural network as a flattened row vector
-
getParam
public INDArray getParam(String param)
Description copied from interface:ModelGet the parameter
-
setParam
public void setParam(String key, INDArray val)
Description copied from interface:ModelSet the parameter with a new ndarray
-
setParams
public void setParams(INDArray params)
Description copied from interface:ModelSet the parameters for this model. This expects a linear ndarray which then be unpacked internally relative to the expected ordering of the model
-
setParams
protected void setParams(INDArray params, char order)
-
setParamsViewArray
public void setParamsViewArray(INDArray params)
Description copied from interface:ModelSet 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:
setParamsViewArrayin interfaceModel- Parameters:
params- a 1 x nParams row vector that is a view of the larger (MLN/CG) parameters array
-
getGradientsViewArray
public INDArray getGradientsViewArray()
- Specified by:
getGradientsViewArrayin interfaceModel- Specified by:
getGradientsViewArrayin interfaceTrainable- Returns:
- 1D gradients view array
-
setBackpropGradientsViewArray
public void setBackpropGradientsViewArray(INDArray gradients)
Description copied from interface:ModelSet 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:
setBackpropGradientsViewArrayin interfaceModel- 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:ModelSetter for the param table- Specified by:
setParamTablein interfaceModel
-
paramTable
public Map<String,INDArray> paramTable()
Description copied from interface:ModelThe param table- Specified by:
paramTablein interfaceModel- Returns:
-
paramTable
public Map<String,INDArray> paramTable(boolean backpropParamsOnly)
Description copied from interface:ModelTable of parameters by key, for backprop For many models (dense layers, etc) - all parameters are backprop parameters- Specified by:
paramTablein interfaceModel- Specified by:
paramTablein interfaceTrainable- 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:LayerPerform forward pass and return the activations array with the specified input- Specified by:
activatein interfaceLayer- Parameters:
input- the input to usetraining- train or test modeworkspaceMgr- Workspace manager.- Returns:
- Activations array. Note that the returned array should be placed in the
ArrayType.ACTIVATIONSworkspace via the workspace manager
-
calcRegularizationScore
public double calcRegularizationScore(boolean backpropParamsOnly)
Description copied from interface:LayerCalculate 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:
calcRegularizationScorein interfaceLayer- 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:ModelThe current inputs batch size
-
conf
public NeuralNetConfiguration conf()
Description copied from interface:ModelThe configuration for the neural network
-
clear
public void clear()
Description copied from interface:ModelClear input
-
applyDropOutIfNecessary
protected void applyDropOutIfNecessary(boolean training, LayerWorkspaceMgr workspaceMgr)
-
type
public Layer.Type type()
Description copied from interface:LayerReturns the layer type
-
numParams
public long numParams()
The number of parameters for the model
-
numParams
public long numParams(boolean backwards)
Description copied from interface:Modelthe number of parameters for the model
-
fit
public void fit(INDArray input, LayerWorkspaceMgr workspaceMgr)
Description copied from interface:ModelFit the model to the given data
-
gradientAndScore
public Pair<Gradient,Double> gradientAndScore()
Description copied from interface:ModelGet the gradient and score- Specified by:
gradientAndScorein interfaceModel- Returns:
- the gradient and score
-
input
public INDArray input()
Description copied from interface:ModelThe input/feature matrix for the model
-
setInputMiniBatchSize
public void setInputMiniBatchSize(int size)
Description copied from interface:LayerSet 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:
setInputMiniBatchSizein interfaceLayer
-
getInputMiniBatchSize
public int getInputMiniBatchSize()
Description copied from interface:LayerGet current/last input mini-batch size, as set by setInputMiniBatchSize(int)- Specified by:
getInputMiniBatchSizein interfaceLayer- See Also:
Layer.setInputMiniBatchSize(int)
-
setMaskArray
public void setMaskArray(INDArray maskArray)
Description copied from interface:LayerSet the mask array. Note: In general,Layer.feedForwardMaskArray(INDArray, MaskState, int)should be used in preference to this.- Specified by:
setMaskArrayin interfaceLayer- Parameters:
maskArray- Mask array to set
-
getMaskArray
public INDArray getMaskArray()
- Specified by:
getMaskArrayin interfaceLayer
-
feedForwardMaskArray
public Pair<INDArray,MaskState> feedForwardMaskArray(INDArray maskArray, MaskState currentMaskState, int minibatchSize)
Description copied from interface:LayerFeed 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:
feedForwardMaskArrayin interfaceLayer- Parameters:
maskArray- Mask array to setcurrentMaskState- Current state of the mask - seeMaskStateminibatchSize- 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:ModelGet 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, seeModel.computeGradientAndScore(LayerWorkspaceMgr)} .
-
fit
public void fit()
Description copied from interface:ModelAll models have a fit method
-
score
public double score()
Description copied from interface:ModelThe score for the model
-
applyConstraints
public void applyConstraints(int iteration, int epoch)Description copied from interface:ModelApply any constraints to the model- Specified by:
applyConstraintsin interfaceModel
-
assertInputSet
public void assertInputSet(boolean backprop)
-
allowInputModification
public void allowInputModification(boolean allow)
Description copied from interface:LayerA 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:
allowInputModificationin interfaceLayer- 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()
-
updaterDivideByMinibatch
public boolean updaterDivideByMinibatch(String paramName)
Description copied from interface:TrainableDL4J 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:
updaterDivideByMinibatchin interfaceTrainable- 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)
-
-