Package ai.djl.nn.core
Class Linear
java.lang.Object
ai.djl.nn.AbstractBaseBlock
ai.djl.nn.AbstractBlock
ai.djl.nn.core.Linear
- All Implemented Interfaces:
Block
A Linear block applies a linear transformation \(Y = XW^T + b\).
It has the following shapes:
- input X: [x1, x2, ..., xn, input_dim]
- weight W: [units, input_dim]
- Bias b: [units]
- output Y: [x1, x2, ..., xn, units]
It is most typically used with a simple batched 1D input. In that case, the shape would be:
- input X: [batch_num, input_dim]
- weight W: [units, input_dim]
- Bias b: [units]
- output Y: [batch_num, units]
The Linear block should be constructed using Linear.Builder
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
-
Field Summary
Fields inherited from class ai.djl.nn.AbstractBlock
children, parameters
Fields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
beforeInitialize
(Shape... inputShapes) Performs any action necessary before initialization.static Linear.Builder
builder()
Creates a builder to build aLinear
.Returns aPairList
of input names, and shapes.protected NDList
forwardInternal
(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)
after initialization.Shape[]
getOutputShapes
(Shape[] inputs) Returns the expected output shapes of the block for the specified input shapes.static NDList
Applies a linear transformation to the incoming data.static NDList
Applies a linear transformation to the incoming data.void
loadMetadata
(byte loadVersion, DataInputStream is) Overwrite this to load additional metadata with the parameter values.void
Sets the shape ofParameter
s.protected void
Override this method to save additional data apart from parameter values.Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addChildBlock, addChildBlockSingleton, addParameter, getChildren, getDirectParameters
Methods inherited from class ai.djl.nn.AbstractBaseBlock
cast, clear, forward, forward, forwardInternal, getInputShapes, getOutputDataTypes, getParameters, initialize, initializeChildBlocks, isInitialized, loadParameters, readInputShapes, saveInputShapes, saveParameters, setInitializer, setInitializer, setInitializer, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface ai.djl.nn.Block
forward, freezeParameters, freezeParameters, getCustomMetadata, getOutputShapes
-
Constructor Details
-
Linear
-
-
Method Details
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)
after initialization.- Specified by:
forwardInternal
in classAbstractBaseBlock
- Parameters:
parameterStore
- the parameter storeinputs
- the input NDListtraining
- true for a training forward passparams
- optional parameters- Returns:
- the output of the forward pass
-
getOutputShapes
Returns the expected output shapes of the block for the specified input shapes.- Parameters:
inputs
- the shapes of the inputs- Returns:
- the expected output shapes of the block
-
describeInput
Returns aPairList
of input names, and shapes.- Specified by:
describeInput
in interfaceBlock
- Overrides:
describeInput
in classAbstractBaseBlock
- Returns:
- the
PairList
of input names, and shapes
-
beforeInitialize
Performs any action necessary before initialization. For example, keep the input information or verify the layout.- Overrides:
beforeInitialize
in classAbstractBaseBlock
- Parameters:
inputShapes
- the expected shapes of the input
-
prepare
Sets the shape ofParameter
s.- Overrides:
prepare
in classAbstractBaseBlock
- Parameters:
inputShapes
- the shapes of inputs
-
saveMetadata
Override this method to save additional data apart from parameter values.This default implementation saves the currently set input shapes.
- Overrides:
saveMetadata
in classAbstractBaseBlock
- Parameters:
os
- the non-null output stream the parameter values and metadata are written to- Throws:
IOException
- saving failed
-
loadMetadata
public void loadMetadata(byte loadVersion, DataInputStream is) throws IOException, MalformedModelException Overwrite this to load additional metadata with the parameter values.If you overwrite
AbstractBaseBlock.saveMetadata(DataOutputStream)
or need to provide backward compatibility to older binary formats, you probably need to overwrite this. This default implementation checks if the version number fits, if not it throws anMalformedModelException
. After that it restores the input shapes.- Overrides:
loadMetadata
in classAbstractBaseBlock
- Parameters:
loadVersion
- the version used for loading this metadata.is
- the input stream we are loading from- Throws:
IOException
- loading failedMalformedModelException
- data can be loaded but has wrong format
-
linear
Applies a linear transformation to the incoming data.- Parameters:
input
- input X: [x1, x2, …, xn, input_dim]weight
- weight W: [units, input_dim]- Returns:
- output Y: [x1, x2, …, xn, units]
-
linear
Applies a linear transformation to the incoming data.- Parameters:
input
- input X: [x1, x2, …, xn, input_dim]weight
- weight W: [units, input_dim]bias
- bias b: [units]- Returns:
- output Y: [x1, x2, …, xn, units]
-
builder
Creates a builder to build aLinear
.- Returns:
- a new builder
-