Package org.deeplearning4j.nn.layers
Class BaseLayer<LayerConfT extends BaseLayer>
- java.lang.Object
-
- org.deeplearning4j.nn.layers.AbstractLayer<LayerConfT>
-
- org.deeplearning4j.nn.layers.BaseLayer<LayerConfT>
-
- All Implemented Interfaces:
Serializable,Cloneable,Layer,Model,Trainable
- Direct Known Subclasses:
BaseOutputLayer,BasePretrainNetwork,BaseRecurrentLayer,BatchNormalization,Cnn3DLossLayer,CnnLossLayer,ConvolutionLayer,Deconvolution3DLayer,DenseLayer,DropoutLayer,ElementWiseMultiplicationLayer,EmbeddingLayer,EmbeddingSequenceLayer,LossLayer,PReLU,RnnLossLayer
public abstract class BaseLayer<LayerConfT extends BaseLayer> extends AbstractLayer<LayerConfT>
A layer with parameters- Author:
- Adam Gibson
- 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 Gradientgradientprotected INDArraygradientsFlattenedprotected Map<String,INDArray>gradientViewsprotected ConvexOptimizeroptimizerprotected Map<String,INDArray>paramsprotected INDArrayparamsFlattenedprotected doublescoreprotected Solversolverprotected Map<String,INDArray>weightNoiseParams-
Fields inherited from class org.deeplearning4j.nn.layers.AbstractLayer
cacheMode, conf, dataType, dropoutApplied, epochCount, index, input, inputModificationAllowed, iterationCount, maskArray, maskState, preOutput, trainingListeners
-
-
Constructor Summary
Constructors Constructor Description BaseLayer(NeuralNetConfiguration conf, DataType dataType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description INDArrayactivate(boolean training, LayerWorkspaceMgr workspaceMgr)Perform forward pass and return the activations array with the last set inputPair<Gradient,INDArray>backpropGradient(INDArray epsilon, LayerWorkspaceMgr workspaceMgr)Calculate the gradient relative to the error in the next layerdoublecalcRegularizationScore(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 inputvoidclearNoiseWeightParams()Layerclone()voidcomputeGradientAndScore(LayerWorkspaceMgr workspaceMgr)Update the scorevoidfit()All models have a fit methodvoidfit(INDArray input, LayerWorkspaceMgr workspaceMgr)Fit the model to the given dataINDArraygetGradientsViewArray()ConvexOptimizergetOptimizer()Returns this models optimizerINDArraygetParam(String param)Get the parameterprotected INDArraygetParamWithNoise(String param, boolean training, LayerWorkspaceMgr workspaceMgr)Get the parameter, after applying any weight noise (such as DropConnect) if necessary.Gradientgradient()Get the gradient.booleanhasBias()Does this layer have no bias term? Many layers (dense, convolutional, output, embedding) have biases by default, but no-bias versions are possible via configurationbooleanhasLayerNorm()Does this layer support and is it enabled layer normalization? Only Dense and SimpleRNN Layers support layer normalization.LayerConfTlayerConf()longnumParams()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 parametersprotected INDArraypreOutput(boolean training, LayerWorkspaceMgr workspaceMgr)protected Pair<INDArray,INDArray>preOutputWithPreNorm(boolean training, boolean forBackprop, LayerWorkspaceMgr workspaceMgr)doublescore()Objective function: the specified objectivevoidsetBackpropGradientsViewArray(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.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 tableprotected voidsetScoreWithZ(INDArray z)StringtoString()voidupdate(Gradient gradient)Update layer weights and biases with gradient changevoidupdate(INDArray gradient, String paramType)Perform one update applying the gradient-
Methods inherited from class org.deeplearning4j.nn.layers.AbstractLayer
activate, addListeners, allowInputModification, applyConstraints, applyDropOutIfNecessary, applyMask, assertInputSet, backpropDropOutIfPresent, batchSize, close, conf, feedForwardMaskArray, getConfig, getEpochCount, getHelper, getIndex, getInput, getInputMiniBatchSize, getListeners, getMaskArray, gradientAndScore, init, input, layerId, numParams, setCacheMode, setConf, setEpochCount, setIndex, setInput, setInputMiniBatchSize, setListeners, setListeners, setMaskArray, type, updaterDivideByMinibatch
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.deeplearning4j.nn.api.Layer
getIterationCount, isPretrainLayer, setIterationCount
-
-
-
-
Field Detail
-
paramsFlattened
protected INDArray paramsFlattened
-
gradientsFlattened
protected INDArray gradientsFlattened
-
score
protected double score
-
optimizer
protected ConvexOptimizer optimizer
-
gradient
protected Gradient gradient
-
solver
protected Solver solver
-
-
Constructor Detail
-
BaseLayer
public BaseLayer(NeuralNetConfiguration conf, DataType dataType)
-
-
Method Detail
-
layerConf
public LayerConfT layerConf()
- Overrides:
layerConfin classAbstractLayer<LayerConfT extends BaseLayer>
-
backpropGradient
public Pair<Gradient,INDArray> backpropGradient(INDArray epsilon, LayerWorkspaceMgr workspaceMgr)
Description copied from interface:LayerCalculate the gradient relative to the error in the next layer- Parameters:
epsilon- w^(L+1)*delta^(L+1). Or, equiv: dC/da, i.e., (dC/dz)*(dz/da) = dC/da, where C is cost function a=sigma(z) is activation.workspaceMgr- Workspace manager- Returns:
- Pair
where Gradient is gradient for this layer, INDArray is epsilon (activation gradient) needed by next layer, but before element-wise multiply by sigmaPrime(z). So for standard feed-forward layer, if this layer is L, then return.getSecond() == dL/dIn = (w^(L)*(delta^(L))^T)^T. Note that the returned array should be placed in the ArrayType.ACTIVATION_GRADworkspace via the workspace manager
-
fit
public void fit()
Description copied from interface:ModelAll models have a fit method- Specified by:
fitin interfaceModel- Overrides:
fitin classAbstractLayer<LayerConfT extends BaseLayer>
-
computeGradientAndScore
public void computeGradientAndScore(LayerWorkspaceMgr workspaceMgr)
Description copied from interface:ModelUpdate the score- Specified by:
computeGradientAndScorein interfaceModel- Overrides:
computeGradientAndScorein classAbstractLayer<LayerConfT extends BaseLayer>
-
setScoreWithZ
protected void setScoreWithZ(INDArray z)
-
score
public double score()
Objective function: the specified objective- Specified by:
scorein interfaceModel- Overrides:
scorein classAbstractLayer<LayerConfT extends BaseLayer>- Returns:
- the score for the objective
-
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)} .- Specified by:
gradientin interfaceModel- Overrides:
gradientin classAbstractLayer<LayerConfT extends BaseLayer>- Returns:
- the gradient for this model, as calculated before
-
update
public void update(Gradient gradient)
Description copied from interface:ModelUpdate layer weights and biases with gradient change- Specified by:
updatein interfaceModel- Overrides:
updatein classAbstractLayer<LayerConfT extends BaseLayer>
-
update
public void update(INDArray gradient, String paramType)
Description copied from interface:ModelPerform one update applying the gradient- Specified by:
updatein interfaceModel- Overrides:
updatein classAbstractLayer<LayerConfT extends BaseLayer>- Parameters:
gradient- the gradient to apply
-
getOptimizer
public ConvexOptimizer getOptimizer()
Description copied from interface:ModelReturns this models optimizer- Specified by:
getOptimizerin interfaceModel- Overrides:
getOptimizerin classAbstractLayer<LayerConfT extends BaseLayer>- Returns:
- this models optimizer
-
params
public INDArray params()
Returns the parameters of the neural network as a flattened row vector- Specified by:
paramsin interfaceModel- Specified by:
paramsin interfaceTrainable- Overrides:
paramsin classAbstractLayer<LayerConfT extends BaseLayer>- Returns:
- the parameters of the neural network
-
getParam
public INDArray getParam(String param)
Description copied from interface:ModelGet the parameter- Specified by:
getParamin interfaceModel- Overrides:
getParamin classAbstractLayer<LayerConfT extends BaseLayer>- 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:ModelSet the parameter with a new ndarray- Specified by:
setParamin interfaceModel- Overrides:
setParamin classAbstractLayer<LayerConfT extends BaseLayer>- Parameters:
key- the key to se tval- the 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- Specified by:
setParamsin interfaceModel- Overrides:
setParamsin classAbstractLayer<LayerConfT extends BaseLayer>- Parameters:
params- the parameters for the model
-
setParams
protected void setParams(INDArray params, char order)
- Overrides:
setParamsin classAbstractLayer<LayerConfT extends BaseLayer>
-
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- Overrides:
setParamsViewArrayin classAbstractLayer<LayerConfT extends BaseLayer>- 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- Overrides:
getGradientsViewArrayin classAbstractLayer<LayerConfT extends BaseLayer>- 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- Overrides:
setBackpropGradientsViewArrayin classAbstractLayer<LayerConfT extends BaseLayer>- 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- Overrides:
setParamTablein classAbstractLayer<LayerConfT extends BaseLayer>
-
paramTable
public Map<String,INDArray> paramTable()
Description copied from interface:ModelThe param table- Specified by:
paramTablein interfaceModel- Overrides:
paramTablein classAbstractLayer<LayerConfT extends BaseLayer>- 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- Overrides:
paramTablein classAbstractLayer<LayerConfT extends BaseLayer>- Parameters:
backpropParamsOnly- If true, return backprop params only. If false: return all params (equivalent to paramsTable())- Returns:
- Parameter table
-
getParamWithNoise
protected INDArray getParamWithNoise(String param, boolean training, LayerWorkspaceMgr workspaceMgr)
Get the parameter, after applying any weight noise (such as DropConnect) if necessary. Note that during training, this will store the post-noise parameters, as these should be used for both forward pass and backprop, for a single iteration. Consequently, the parameters (post noise) should be cleared after each training iteration- Parameters:
param- Parameter keytraining- If true: during training- Returns:
- The parameter, after applying any noise
-
preOutput
protected INDArray preOutput(boolean training, LayerWorkspaceMgr workspaceMgr)
-
preOutputWithPreNorm
protected Pair<INDArray,INDArray> preOutputWithPreNorm(boolean training, boolean forBackprop, LayerWorkspaceMgr workspaceMgr)
-
activate
public INDArray activate(boolean training, LayerWorkspaceMgr workspaceMgr)
Description copied from interface:LayerPerform forward pass and return the activations array with the last set input- Parameters:
training- training or test modeworkspaceMgr- Workspace manager- Returns:
- the activation (layer output) of the last specified input. 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- Overrides:
calcRegularizationScorein classAbstractLayer<LayerConfT extends BaseLayer>- 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
-
numParams
public long numParams()
The number of parameters for the model- Specified by:
numParamsin interfaceModel- Specified by:
numParamsin interfaceTrainable- Overrides:
numParamsin classAbstractLayer<LayerConfT extends BaseLayer>- Returns:
- the 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- Specified by:
fitin interfaceModel- Overrides:
fitin classAbstractLayer<LayerConfT extends BaseLayer>- Parameters:
input- the data to fit the model to
-
clear
public void clear()
Description copied from interface:ModelClear input- Specified by:
clearin interfaceModel- Overrides:
clearin classAbstractLayer<LayerConfT extends BaseLayer>
-
clearNoiseWeightParams
public void clearNoiseWeightParams()
-
hasBias
public boolean hasBias()
Does this layer have no bias term? Many layers (dense, convolutional, output, embedding) have biases by default, but no-bias versions are possible via configuration- Returns:
- True if a bias term is present, false otherwise
-
hasLayerNorm
public boolean hasLayerNorm()
Does this layer support and is it enabled layer normalization? Only Dense and SimpleRNN Layers support layer normalization.- Returns:
- True if layer normalization is enabled on this layer, false otherwise
-
-