Package com.jml.neural_network.layers
Class Linear
java.lang.Object
com.jml.neural_network.layers.Linear
- All Implemented Interfaces:
BaseLayer
,TrainableLayer
Simple fully-connected linear layer. Applies the linear transform f(x)=Wx+b for an input vector x.
This is equivalent to a
- The default weightInitializer is the
- The default biasInitializer is the
Dense
layer with a linear
activation.
- The default weightInitializer is the
GlorotNormal
initializer. - The default biasInitializer is the
Zeros
initializer.-
Field Summary
-
Constructor Summary
ConstructorDescriptionLinear(int outDim)
Creates a Linear layer with specified input and output dimensions.
NOTE: this constructor infers the input dimension from the previous layer in the network.Linear(int inDim, int outDim)
Creates a Linear layer with specified input and output dimensions.Linear(int inDim, int outDim, Initializer weightInitializer)
Creates a Linear layer with specified input and output dimensions.Linear(int inDim, int outDim, Initializer weightInitializer, Initializer biasInitializer)
Creates a Linear layer with specified input and output dimensions.Linear(int outDim, 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.Linear(int outDim, 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 backward pass for layer.linalg.Matrix
forward(linalg.Matrix input)
Computes forward pass for 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
-
Linear
public Linear(int inDim, int outDim)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.)
-
Linear
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.)weightInitializer
- Initializer for the weights of this layer.
-
Linear
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.)weightInitializer
- Initializer for the weights of this layer.biasInitializer
- Initializer for the bias terms of this layer.
-
Linear
public Linear(int outDim)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 useLinear(int, int)
.- Parameters:
outDim
- Output dimension for this layer (i.e. the number of nodes in this layer).
-
Linear
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 useLinear(int, int, Initializer)
.- Parameters:
outDim
- Output dimension for this layer (i.e. the number of nodes in this layer).weightInitializer
- Initializer for the weights of this layer.
-
Linear
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 useLinear(int, int, Initializer, Initializer)
.- Parameters:
outDim
- Output dimension for this layer (i.e. the number of nodes in 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 forward pass for layer. -
back
public linalg.Matrix back(linalg.Matrix upstreamGrad)Computes backward pass for layer. -
resetGradients
public void resetGradients()Resists the accumulation of gradients for this layer.- Specified by:
resetGradients
in interfaceBaseLayer
- Specified by:
resetGradients
in interfaceTrainableLayer
-
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}.
-
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.
-
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.
-
inspect
Gets and formats details of this layer in a human-readable String. -
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.
-