Class Layer
java.lang.Object
smile.base.mlp.Layer
- All Implemented Interfaces:
Serializable, AutoCloseable
- Direct Known Subclasses:
HiddenLayer, InputLayer, OutputLayer
A layer in the neural network.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected VectorThe bias.protected ThreadLocal<Vector> The bias gradient.protected ThreadLocal<Vector> The first moment of bias gradient.protected ThreadLocal<Vector> The second moment of bias gradient.protected ThreadLocal<Vector> The bias update.protected final doubleThe dropout rate.protected ThreadLocal<byte[]> The dropout mask.protected final intThe number of neurons in this layerprotected ThreadLocal<Vector> The output vector.protected ThreadLocal<Vector> The output gradient.protected final intThe number of input variables.protected DenseMatrixThe affine transformation matrix.protected ThreadLocal<DenseMatrix> The weight gradient.protected ThreadLocal<DenseMatrix> The first moment of weight gradient.protected ThreadLocal<DenseMatrix> The second moment of weight gradient.protected ThreadLocal<DenseMatrix> The weight update. -
Constructor Summary
ConstructorsConstructorDescriptionLayer(int n, int p) Constructor.Layer(int n, int p, double dropout) Constructor.Layer(DenseMatrix weight, Vector bias) Constructor.Layer(DenseMatrix weight, Vector bias, double dropout) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidPropagates the errors back through the (implicit) dropout layer.abstract voidbackpropagate(Vector lowerLayerGradient) Propagates the errors back to a lower layer.bias()Returns the bias vector.static HiddenLayerBuilderReturns a hidden layer.voidclose()voidComputes the parameter gradient for a sample of (mini-)batch.voidcomputeGradientUpdate(Vector x, double learningRate, double momentum, double decay) Computes the parameter gradient and update the weights.intReturns the dimension of input vector (not including bias value).intReturns the dimension of output vector.gradient()Returns the output gradient vector.static LayerBuilderinput(int neurons) Returns an input layer.static LayerBuilderinput(int neurons, double dropout) Returns an input layer.static HiddenLayerBuilderleaky(int neurons) Returns a hidden layer with leaky rectified linear activation function.static HiddenLayerBuilderleaky(int neurons, double dropout) Returns a hidden layer with leaky rectified linear activation function.static HiddenLayerBuilderleaky(int neurons, double dropout, double a) Returns a hidden layer with leaky rectified linear activation function.static HiddenLayerBuilderlinear(int neurons) Returns a hidden layer with linear activation function.static HiddenLayerBuilderlinear(int neurons, double dropout) Returns a hidden layer with linear activation function.static OutputLayerBuildermle(int neurons, OutputFunction output) Returns an output layer with (log-)likelihood cost function.static OutputLayerBuildermse(int neurons, OutputFunction output) Returns an output layer with mean squared error cost function.static LayerBuilder[]Returns the layer builders given a string representation such as "Input(10, 0.2)|ReLU(50, 0.5)|Sigmoid(30, 0.5)|...".output()Returns the output vector.voidPropagates the signals from a lower layer to this layer.voidPropagates the output signals through the implicit dropout layer.static HiddenLayerBuilderrectifier(int neurons) Returns a hidden layer with rectified linear activation function.static HiddenLayerBuilderrectifier(int neurons, double dropout) Returns a hidden layer with rectified linear activation function.static HiddenLayerBuildersigmoid(int neurons) Returns a hidden layer with sigmoid activation function.static HiddenLayerBuildersigmoid(int neurons, double dropout) Returns a hidden layer with sigmoid activation function.static HiddenLayerBuildertanh(int neurons) Returns a hidden layer with hyperbolic tangent activation function.static HiddenLayerBuildertanh(int neurons, double dropout) Returns a hidden layer with hyperbolic tangent activation function.abstract voidThe activation or output function.voidupdate(int m, double learningRate, double momentum, double decay, double rho, double epsilon) Adjust network weights by back-propagation algorithm.weight()Returns the weight matrix.
-
Field Details
-
n
protected final int nThe number of neurons in this layer -
p
protected final int pThe number of input variables. -
dropout
protected final double dropoutThe dropout rate. Dropout randomly sets input units to 0 with this rate at each step during training time, which helps prevent overfitting. -
weight
The affine transformation matrix. -
bias
The bias. -
output
The output vector. -
outputGradient
The output gradient. -
weightGradient
The weight gradient. -
biasGradient
The bias gradient. -
weightGradientMoment1
The first moment of weight gradient. -
weightGradientMoment2
The second moment of weight gradient. -
biasGradientMoment1
The first moment of bias gradient. -
biasGradientMoment2
The second moment of bias gradient. -
weightUpdate
The weight update. -
biasUpdate
The bias update. -
mask
The dropout mask.
-
-
Constructor Details
-
Layer
public Layer(int n, int p) Constructor. Randomly initialized weights and zero bias.- Parameters:
n- the number of neurons.p- the number of input variables (not including bias value).
-
Layer
public Layer(int n, int p, double dropout) Constructor. Randomly initialized weights and zero bias.- Parameters:
n- the number of neurons.p- the number of input variables (not including bias value).dropout- the dropout rate.
-
Layer
Constructor.- Parameters:
weight- the weight matrix.bias- the bias vector.
-
Layer
Constructor.- Parameters:
weight- the weight matrix.bias- the bias vector.dropout- the dropout rate.
-
-
Method Details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getOutputSize
public int getOutputSize()Returns the dimension of output vector.- Returns:
- the dimension of output vector.
-
getInputSize
public int getInputSize()Returns the dimension of input vector (not including bias value).- Returns:
- the dimension of input vector.
-
weight
-
bias
-
output
-
gradient
-
propagate
Propagates the signals from a lower layer to this layer.- Parameters:
x- the lower layer signals.
-
propagateDropout
public void propagateDropout()Propagates the output signals through the implicit dropout layer. Dropout randomly sets output units to 0. It should only be applied during training. -
transform
The activation or output function.- Parameters:
x- the input and output values.
-
backpropagate
Propagates the errors back to a lower layer.- Parameters:
lowerLayerGradient- the gradient vector of lower layer.
-
backpopagateDropout
public void backpopagateDropout()Propagates the errors back through the (implicit) dropout layer. -
computeGradientUpdate
Computes the parameter gradient and update the weights.- Parameters:
x- the input vector.learningRate- the learning rate.momentum- the momentum factor.decay- weight decay factor.
-
computeGradient
Computes the parameter gradient for a sample of (mini-)batch.- Parameters:
x- the input vector.
-
update
public void update(int m, double learningRate, double momentum, double decay, double rho, double epsilon) Adjust network weights by back-propagation algorithm.- Parameters:
m- the size of mini-batch.learningRate- the learning rate.momentum- the momentum factor.decay- weight decay factor.rho- RMSProp discounting factor for the history/coming gradient.epsilon- a small constant for numerical stability.
-
builder
public static HiddenLayerBuilder builder(String activation, int neurons, double dropout, double param) Returns a hidden layer.- Parameters:
activation- the activation function.neurons- the number of neurons.dropout- the dropout rate.param- the optional activation function parameter.- Returns:
- the layer builder.
-
input
Returns an input layer.- Parameters:
neurons- the number of neurons.- Returns:
- the layer builder.
-
input
Returns an input layer.- Parameters:
neurons- the number of neurons.dropout- the dropout rate.- Returns:
- the layer builder.
-
linear
Returns a hidden layer with linear activation function.- Parameters:
neurons- the number of neurons.- Returns:
- the layer builder.
-
linear
Returns a hidden layer with linear activation function.- Parameters:
neurons- the number of neurons.dropout- the dropout rate.- Returns:
- the layer builder.
-
rectifier
Returns a hidden layer with rectified linear activation function.- Parameters:
neurons- the number of neurons.- Returns:
- the layer builder.
-
rectifier
Returns a hidden layer with rectified linear activation function.- Parameters:
neurons- the number of neurons.dropout- the dropout rate.- Returns:
- the layer builder.
-
leaky
Returns a hidden layer with leaky rectified linear activation function.- Parameters:
neurons- the number of neurons.- Returns:
- the layer builder.
-
leaky
Returns a hidden layer with leaky rectified linear activation function.- Parameters:
neurons- the number of neurons.dropout- the dropout rate.- Returns:
- the layer builder.
-
leaky
Returns a hidden layer with leaky rectified linear activation function.- Parameters:
neurons- the number of neurons.dropout- the dropout rate.a- the parameter of leaky ReLU.- Returns:
- the layer builder.
-
sigmoid
Returns a hidden layer with sigmoid activation function.- Parameters:
neurons- the number of neurons.- Returns:
- the layer builder.
-
sigmoid
Returns a hidden layer with sigmoid activation function.- Parameters:
neurons- the number of neurons.dropout- the dropout rate.- Returns:
- the layer builder.
-
tanh
Returns a hidden layer with hyperbolic tangent activation function.- Parameters:
neurons- the number of neurons.- Returns:
- the layer builder.
-
tanh
Returns a hidden layer with hyperbolic tangent activation function.- Parameters:
neurons- the number of neurons.dropout- the dropout rate.- Returns:
- the layer builder.
-
mse
Returns an output layer with mean squared error cost function.- Parameters:
neurons- the number of neurons.output- the output function.- Returns:
- the layer builder.
-
mle
Returns an output layer with (log-)likelihood cost function.- Parameters:
neurons- the number of neurons.output- the output function.- Returns:
- the layer builder.
-
of
Returns the layer builders given a string representation such as "Input(10, 0.2)|ReLU(50, 0.5)|Sigmoid(30, 0.5)|...".- Parameters:
k- the number of classes.k < 2for regression.p- the number of input variables (not including bias value).spec- the hidden layer specification.- Returns:
- the layer builders.
-