public abstract class AbstractLayer<LayerConfT extends Layer> extends Object implements Layer
Layer.TrainingMode, Layer.Type
Modifier and Type | Field and Description |
---|---|
protected CacheMode |
cacheMode |
protected NeuralNetConfiguration |
conf |
protected DataType |
dataType |
protected boolean |
dropoutApplied |
protected int |
epochCount |
protected int |
index |
protected INDArray |
input |
protected boolean |
inputModificationAllowed |
protected int |
iterationCount |
protected INDArray |
maskArray |
protected MaskState |
maskState |
protected INDArray |
preOutput |
protected Collection<TrainingListener> |
trainingListeners |
Constructor and Description |
---|
AbstractLayer(NeuralNetConfiguration conf,
DataType dataType) |
Modifier and Type | Method and Description |
---|---|
INDArray |
activate(INDArray input,
boolean training,
LayerWorkspaceMgr workspaceMgr)
Perform forward pass and return the activations array with the specified input
|
void |
addListeners(TrainingListener... listeners)
This method ADDS additional TrainingListener to existing listeners
|
void |
allowInputModification(boolean allow)
A performance optimization: mark whether the layer is allowed to modify its input array in-place.
|
void |
applyConstraints(int iteration,
int epoch)
Apply any constraints to the model
|
protected void |
applyDropOutIfNecessary(boolean training,
LayerWorkspaceMgr workspaceMgr) |
protected void |
applyMask(INDArray to) |
void |
assertInputSet(boolean backprop) |
protected INDArray |
backpropDropOutIfPresent(INDArray epsilon) |
int |
batchSize()
The current inputs batch size
|
double |
calcRegularizationScore(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 function |
void |
clear()
Clear input
|
void |
close() |
void |
computeGradientAndScore(LayerWorkspaceMgr workspaceMgr)
Update the score
|
NeuralNetConfiguration |
conf()
The configuration for the neural network
|
Pair<INDArray,MaskState> |
feedForwardMaskArray(INDArray maskArray,
MaskState currentMaskState,
int minibatchSize)
Feed forward the input mask array, setting in the layer as appropriate.
|
void |
fit()
All models have a fit method
|
void |
fit(INDArray input,
LayerWorkspaceMgr workspaceMgr)
Fit the model to the given data
|
TrainingConfig |
getConfig() |
int |
getEpochCount() |
INDArray |
getGradientsViewArray() |
LayerHelper |
getHelper() |
int |
getIndex()
Get the layer index.
|
INDArray |
getInput() |
int |
getInputMiniBatchSize()
Get current/last input mini-batch size, as set by setInputMiniBatchSize(int)
|
Collection<TrainingListener> |
getListeners()
Get the iteration listeners for this layer.
|
INDArray |
getMaskArray() |
ConvexOptimizer |
getOptimizer()
Returns this models optimizer
|
INDArray |
getParam(String param)
Get the parameter
|
Gradient |
gradient()
Get the gradient.
|
Pair<Gradient,Double> |
gradientAndScore()
Get the gradient and score
|
void |
init()
Init the model
|
INDArray |
input()
The input/feature matrix for the model
|
LayerConfT |
layerConf() |
protected String |
layerId() |
long |
numParams()
The number of parameters for the model
|
long |
numParams(boolean backwards)
the number of parameters for the model
|
INDArray |
params()
Returns the parameters of the neural network as a flattened row vector
|
Map<String,INDArray> |
paramTable()
The param table
|
Map<String,INDArray> |
paramTable(boolean backpropParamsOnly)
Table of parameters by key, for backprop
For many models (dense layers, etc) - all parameters are backprop parameters
|
double |
score()
The score for the model
|
void |
setBackpropGradientsViewArray(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.
|
void |
setCacheMode(CacheMode mode)
This method sets given CacheMode for current layer
|
void |
setConf(NeuralNetConfiguration conf)
Setter for the configuration
|
void |
setEpochCount(int epochCount)
Set the current epoch count (number of epochs passed ) for the layer/network
|
void |
setIndex(int index)
Set the layer index.
|
void |
setInput(INDArray input,
LayerWorkspaceMgr workspaceMgr)
Set the layer input.
|
void |
setInputMiniBatchSize(int size)
Set current/last input mini-batch size.
Used for score and gradient calculations. |
void |
setListeners(Collection<TrainingListener> listeners)
Set the
TrainingListener s for this model. |
void |
setListeners(TrainingListener... listeners)
Set the
TrainingListener s for this model. |
void |
setMaskArray(INDArray maskArray)
Set the mask array.
|
void |
setParam(String key,
INDArray val)
Set the parameter with a new ndarray
|
void |
setParams(INDArray params)
Set the parameters for this model.
|
protected void |
setParams(INDArray params,
char order) |
void |
setParamsViewArray(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.
|
void |
setParamTable(Map<String,INDArray> paramTable)
Setter for the param table
|
Layer.Type |
type()
Returns the layer type
|
void |
update(Gradient gradient)
Update layer weights and biases with gradient change
|
void |
update(INDArray gradient,
String paramType)
Perform one update applying the gradient
|
boolean |
updaterDivideByMinibatch(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. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
activate, backpropGradient, clearNoiseWeightParams, getIterationCount, isPretrainLayer, setIterationCount
protected INDArray input
protected INDArray preOutput
protected NeuralNetConfiguration conf
protected boolean dropoutApplied
protected Collection<TrainingListener> trainingListeners
protected int index
protected INDArray maskArray
protected MaskState maskState
protected CacheMode cacheMode
protected boolean inputModificationAllowed
protected DataType dataType
protected int iterationCount
protected int epochCount
public AbstractLayer(NeuralNetConfiguration conf, DataType dataType)
public void setCacheMode(CacheMode mode)
Layer
setCacheMode
in interface Layer
public LayerConfT layerConf()
public TrainingConfig getConfig()
protected String layerId()
public INDArray getInput()
public int getEpochCount()
getEpochCount
in interface Layer
public void setEpochCount(int epochCount)
Layer
setEpochCount
in interface Layer
public void setInput(INDArray input, LayerWorkspaceMgr workspaceMgr)
Layer
public int getIndex()
Layer
public void setIndex(int index)
Layer
public Collection<TrainingListener> getListeners()
Layer
getListeners
in interface Layer
public void setListeners(Collection<TrainingListener> listeners)
Layer
TrainingListener
s for this model. If any listeners have previously been set, they will be
replaced by this methodsetListeners
in interface Layer
setListeners
in interface Model
public void addListeners(TrainingListener... listeners)
addListeners
in interface Model
listeners
- public void setListeners(TrainingListener... listeners)
Layer
TrainingListener
s for this model. If any listeners have previously been set, they will be
replaced by this methodsetListeners
in interface Layer
setListeners
in interface Model
public void computeGradientAndScore(LayerWorkspaceMgr workspaceMgr)
Model
computeGradientAndScore
in interface Model
public void update(Gradient gradient)
Model
public void update(INDArray gradient, String paramType)
Model
public ConvexOptimizer getOptimizer()
Model
getOptimizer
in interface Model
public void setConf(NeuralNetConfiguration conf)
Model
public INDArray params()
public INDArray getParam(String param)
Model
public void setParam(String key, INDArray val)
Model
public void setParams(INDArray params)
Model
protected void setParams(INDArray params, char order)
public void setParamsViewArray(INDArray params)
Model
setParamsViewArray
in interface Model
params
- a 1 x nParams row vector that is a view of the larger (MLN/CG) parameters arraypublic INDArray getGradientsViewArray()
getGradientsViewArray
in interface Model
getGradientsViewArray
in interface Trainable
public void setBackpropGradientsViewArray(INDArray gradients)
Model
setBackpropGradientsViewArray
in interface Model
gradients
- a 1 x nParams row vector that is a view of the larger (MLN/CG) gradients arraypublic void setParamTable(Map<String,INDArray> paramTable)
Model
setParamTable
in interface Model
public Map<String,INDArray> paramTable()
Model
paramTable
in interface Model
public Map<String,INDArray> paramTable(boolean backpropParamsOnly)
Model
paramTable
in interface Model
paramTable
in interface Trainable
backpropParamsOnly
- If true, return backprop params only. If false: return all params (equivalent to
paramsTable())protected void applyMask(INDArray to)
public INDArray activate(INDArray input, boolean training, LayerWorkspaceMgr workspaceMgr)
Layer
activate
in interface Layer
input
- the input to usetraining
- train or test modeworkspaceMgr
- Workspace manager.ArrayType.ACTIVATIONS
workspace via the workspace managerpublic double calcRegularizationScore(boolean backpropParamsOnly)
Layer
calcRegularizationScore
in interface Layer
backpropParamsOnly
- If true: calculate regularization score based on backprop params only. If false: calculate
based on all params (including pretrain params, if any)public int batchSize()
Model
public NeuralNetConfiguration conf()
Model
public void clear()
Model
protected void applyDropOutIfNecessary(boolean training, LayerWorkspaceMgr workspaceMgr)
public Layer.Type type()
Layer
public long numParams()
public long numParams(boolean backwards)
Model
public void fit(INDArray input, LayerWorkspaceMgr workspaceMgr)
Model
public Pair<Gradient,Double> gradientAndScore()
Model
gradientAndScore
in interface Model
public INDArray input()
Model
public void setInputMiniBatchSize(int size)
Layer
setInputMiniBatchSize
in interface Layer
public int getInputMiniBatchSize()
Layer
getInputMiniBatchSize
in interface Layer
Layer.setInputMiniBatchSize(int)
public void setMaskArray(INDArray maskArray)
Layer
Layer.feedForwardMaskArray(INDArray, MaskState, int)
should be used in
preference to this.setMaskArray
in interface Layer
maskArray
- Mask array to setpublic INDArray getMaskArray()
getMaskArray
in interface Layer
public Pair<INDArray,MaskState> feedForwardMaskArray(INDArray maskArray, MaskState currentMaskState, int minibatchSize)
Layer
feedForwardMaskArray
in interface Layer
maskArray
- Mask array to setcurrentMaskState
- 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)public Gradient gradient()
Model
Model.computeGradientAndScore(LayerWorkspaceMgr)
} .public void fit()
Model
public double score()
Model
public void applyConstraints(int iteration, int epoch)
Model
applyConstraints
in interface Model
public void assertInputSet(boolean backprop)
public void allowInputModification(boolean allow)
Layer
allowInputModification
in interface Layer
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)public LayerHelper getHelper()
public boolean updaterDivideByMinibatch(String paramName)
Trainable
updaterDivideByMinibatch
in interface Trainable
paramName
- Name of the parameterCopyright © 2020. All rights reserved.