Package smile.base.mlp
Class Layer
java.lang.Object
smile.base.mlp.Layer
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
HiddenLayer,InputLayer,OutputLayer
A layer in the neural network.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected double[]The bias.protected ThreadLocal<double[]> The bias gradient.protected ThreadLocal<double[]> The first moment of bias gradient.protected ThreadLocal<double[]> The second moment of bias gradient.protected ThreadLocal<double[]> The bias update.protected final doubleThe dropout rate.protected ThreadLocal<byte[]> The dropout mask.protected final intThe number of neurons in this layerprotected ThreadLocal<double[]> The output vector.protected ThreadLocal<double[]> The output gradient.protected final intThe number of input variables.protected MatrixThe affine transformation matrix.protected ThreadLocal<Matrix> The weight gradient.protected ThreadLocal<Matrix> The first moment of weight gradient.protected ThreadLocal<Matrix> The second moment of weight gradient.protected ThreadLocal<Matrix> The weight update. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidPropagates the errors back through the (implicit) dropout layer.abstract voidbackpropagate(double[] lowerLayerGradient) Propagates the errors back to a lower layer.static HiddenLayerBuilderReturns a hidden layer.voidcomputeGradient(double[] x) Computes the parameter gradient for a sample of (mini-)batch.voidcomputeGradientUpdate(double[] 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.double[]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)|...".double[]output()Returns the output vector.voidpropagate(double[] x) Propagates 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 voidtransform(double[] x) The activation or output function.voidupdate(int m, double learningRate, double momentum, double decay, double rho, double epsilon) Adjust network weights by back-propagation algorithm.
-
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
protected double[] biasThe 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
-
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.
-
output
public double[] output()Returns the output vector.- Returns:
- the output vector.
-
gradient
public double[] gradient()Returns the output gradient vector.- Returns:
- the output gradient vector.
-
propagate
public void propagate(double[] x) 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
public abstract void transform(double[] x) The activation or output function.- Parameters:
x- the input and output values.
-
backpropagate
public abstract void backpropagate(double[] lowerLayerGradient) 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
public void computeGradientUpdate(double[] x, double learningRate, double momentum, double decay) 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
public void computeGradient(double[] x) 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.
-