Package com.jml.neural_network.layers
Class Dense
java.lang.Object
com.jml.neural_network.layers.Dense
- All Implemented Interfaces:
BaseLayer
,TrainableLayer
A fully connected layer with an
activation function
.
For an activation function g(x), this layer applies the transform f(x) = g(Wx+b) for an input vector x.-
Field Summary
-
Constructor Summary
ConstructorDescriptionDense(int inDim, int outDim, ActivationFunction activation)
Creates a Linear layer with specified input and output dimensions.Dense(int inDim, int outDim, ActivationFunction activation, Initializer weightInitializer)
Creates a Linear layer with specified input and output dimensions.Dense(int inDim, int outDim, ActivationFunction activation, Initializer weightInitializer, Initializer biasInitializer)
Creates a Linear layer with specified input and output dimensions.Dense(int outDim, ActivationFunction activation)
Creates a Linear layer with specified input and output dimensions.
NOTE: this constructor infers the input dimension from the previous layer in the network.Dense(int outDim, ActivationFunction activation, Initializer weightInitializer)
Creates a Linear layer with specified input and output dimensions.
NOTE: this constructor infers the input dimension from the previous layer in the network.Dense(int outDim, ActivationFunction activation, Initializer weightInitializer, Initializer biasInitializer)
Creates a Linear layer with specified input and output dimensions.
NOTE: this constructor infers the input dimension from the previous layer in the network. -
Method Summary
Modifier and TypeMethodDescriptionlinalg.Matrix
back(linalg.Matrix upstreamGrad)
Computes the backward pass for this layer.linalg.Matrix
forward(linalg.Matrix input)
Computes the forward pass of this layer.Constructs a string containing this all details of the model pertinent for saving the model to a file.int
getInDim()
Gets the input dimension of this layer.int
Gets the output dimension of this layer.linalg.Matrix[]
Gets the trainable parameters for this layer as an array of matrices.linalg.Matrix[]
Gets the update matrices for parameters of this layer.inspect()
Gets and formats details of this layer in a human-readable String.void
Resists the accumulation of gradients for this layer.void
setParams(linalg.Matrix... params)
Sets the parameters for this layer.toString()
Gets the name of the layer type as a string.void
updateInDim(int newInDim)
Updates this layers input dimension.
-
Field Details
-
LAYER_TYPE
- See Also:
- Constant Field Values
-
-
Constructor Details
-
Dense
Creates a Linear layer with specified input and output dimensions.- Parameters:
inDim
- Input dimension for this layer. (i.e. the number of nodes in the previous layer)outDim
- Output dimension for this layer. (i.e. the number of nodes in this layer.)activation
- Activation function for this layer.
-
Dense
Creates a Linear layer with specified input and output dimensions.- Parameters:
inDim
- Input dimension for this layer. (i.e. the number of nodes in the previous layer)outDim
- Output dimension for this layer. (i.e. the number of nodes in this layer.)activation
- Activation function for this layer.weightInitializer
- Initializer for the weights of this layer.
-
Dense
public Dense(int inDim, int outDim, ActivationFunction activation, Initializer weightInitializer, Initializer biasInitializer)Creates a Linear layer with specified input and output dimensions.- Parameters:
inDim
- Input dimension for this layer. (i.e. the number of nodes in the previous layer)outDim
- Output dimension for this layer. (i.e. the number of nodes in this layer.)activation
- Activation function for this layer.weightInitializer
- Initializer for the weights of this layer.biasInitializer
- Initializer for the bias terms of this layer.
-
Dense
Creates a Linear layer with specified input and output dimensions.
NOTE: this constructor infers the input dimension from the previous layer in the network. Thus, it cannot be used as the first layer of the neural network. For the first layer use todo...- Parameters:
outDim
- Output dimension for this layer (i.e. the number of nodes in this layer).activation
- Activation function for this layer.
-
Dense
Creates a Linear layer with specified input and output dimensions.
NOTE: this constructor infers the input dimension from the previous layer in the network. Thus, it cannot be used as the first layer of the neural network. For the first layer use todo...- Parameters:
outDim
- Output dimension for this layer (i.e. the number of nodes in this layer).activation
- Activation function for this layer.weightInitializer
- Initializer for the weights of this layer.
-
Dense
public Dense(int outDim, ActivationFunction activation, Initializer weightInitializer, Initializer biasInitializer)Creates a Linear layer with specified input and output dimensions.
NOTE: this constructor infers the input dimension from the previous layer in the network. Thus, it cannot be used as the first layer of the neural network. For the first layer use todo...- Parameters:
outDim
- Output dimension for this layer (i.e. the number of nodes in this layer).activation
- Activation function for this layer.weightInitializer
- Initializer for the weights of this layer.biasInitializer
- Initializer for the bias terms of this layer.
-
-
Method Details
-
forward
public linalg.Matrix forward(linalg.Matrix input)Computes the forward pass of this layer. -
back
public linalg.Matrix back(linalg.Matrix upstreamGrad)Computes the backward pass for this layer. -
getInDim
public int getInDim()Gets the input dimension of this layer. -
getOutDim
public int getOutDim()Gets the output dimension of this layer. -
updateInDim
public void updateInDim(int newInDim)Updates this layers input dimension. This is useful for creating a layer with an unknown input dimension and inferring it from the previous layer in the network.- Specified by:
updateInDim
in interfaceBaseLayer
- Parameters:
newInDim
- New input dimension for layer.
-
getParams
public linalg.Matrix[] getParams()Gets the trainable parameters for this layer as an array of matrices.- Specified by:
getParams
in interfaceBaseLayer
- Specified by:
getParams
in interfaceTrainableLayer
- Returns:
- The trainable parameters for this layer as an array of matrices {weights, bias}.
-
setParams
public void setParams(linalg.Matrix... params)Sets the parameters for this layer.- Specified by:
setParams
in interfaceBaseLayer
- Specified by:
setParams
in interfaceTrainableLayer
- Parameters:
params
- Parameter Matrices for this layer.
-
getUpdates
public linalg.Matrix[] getUpdates()Gets the update matrices for parameters of this layer.- Specified by:
getUpdates
in interfaceBaseLayer
- Specified by:
getUpdates
in interfaceTrainableLayer
- Returns:
- The parameter update matrices.
-
inspect
Gets and formats details of this layer in a human-readable String. -
resetGradients
public void resetGradients()Resists the accumulation of gradients for this layer.- Specified by:
resetGradients
in interfaceBaseLayer
- Specified by:
resetGradients
in interfaceTrainableLayer
-
getDetails
Constructs a string containing this all details of the model pertinent for saving the model to a file.- Specified by:
getDetails
in interfaceBaseLayer
- Returns:
- A string containing all information, including trainable parameters, needed to recreate the layer.
-
toString
Gets the name of the layer type as a string.
-