Package ai.djl.nn.core
Class Linear
- java.lang.Object
-
- ai.djl.nn.AbstractBlock
-
- ai.djl.nn.core.Linear
-
- All Implemented Interfaces:
Block
public class Linear extends AbstractBlock
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]
The Linear block should be constructed using
Linear.Builder.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLinear.Builder
-
Field Summary
-
Fields inherited from class ai.djl.nn.AbstractBlock
children, inputNames, inputShapes, parameters, version
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidbeforeInitialize(Shape... inputShapes)Performs any action necessary before initialization.static Linear.Builderbuilder()Creates a builder to build aLinear.ai.djl.util.PairList<java.lang.String,Shape>describeInput()Returns aPairListof input names, and shapes.protected NDListforwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.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 NDListlinear(NDArray input, NDArray weight)Applies a linear transformation to the incoming data.static NDListlinear(NDArray input, NDArray weight, NDArray bias)Applies a linear transformation to the incoming data.voidloadMetadata(byte loadVersion, java.io.DataInputStream is)Overwrite this to load additional metadata with the parameter values.voidprepare(Shape[] inputShapes)Sets the shape ofParameters.protected voidsaveMetadata(java.io.DataOutputStream os)Override this method to save additional data apart from parameter values.-
Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addParameter, cast, clear, forward, forward, forwardInternal, getChildren, getDirectParameters, getParameters, initialize, initializeChildBlocks, isInitialized, loadParameters, readInputShapes, saveInputShapes, saveParameters, setInitializer, setInitializer, setInitializer, toString
-
-
-
-
Method Detail
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)after initialization.- Specified by:
forwardInternalin classAbstractBlock- Parameters:
parameterStore- the parameter storeinputs- the input NDListtraining- true for a training forward passparams- optional parameters- Returns:
- the output of the forward pass
-
getOutputShapes
public Shape[] getOutputShapes(Shape[] inputs)
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
public ai.djl.util.PairList<java.lang.String,Shape> describeInput()
Returns aPairListof input names, and shapes.- Specified by:
describeInputin interfaceBlock- Overrides:
describeInputin classAbstractBlock- Returns:
- the
PairListof input names, and shapes
-
beforeInitialize
protected void beforeInitialize(Shape... inputShapes)
Performs any action necessary before initialization. For example, keep the input information or verify the layout.- Overrides:
beforeInitializein classAbstractBlock- Parameters:
inputShapes- the expected shapes of the input
-
prepare
public void prepare(Shape[] inputShapes)
Sets the shape ofParameters.- Overrides:
preparein classAbstractBlock- Parameters:
inputShapes- the shapes of inputs
-
saveMetadata
protected void saveMetadata(java.io.DataOutputStream os) throws java.io.IOExceptionOverride this method to save additional data apart from parameter values.This default implementation saves the currently set input shapes.
- Overrides:
saveMetadatain classAbstractBlock- Parameters:
os- the non-null output stream the parameter values and metadata are written to- Throws:
java.io.IOException- saving failed
-
loadMetadata
public void loadMetadata(byte loadVersion, java.io.DataInputStream is) throws java.io.IOException, MalformedModelExceptionOverwrite this to load additional metadata with the parameter values.If you overwrite
AbstractBlock.saveMetadata(DataOutputStream)or need to provide backward compatibility to older binary formats, you prabably 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:
loadMetadatain classAbstractBlock- Parameters:
loadVersion- the version used for loading this metadata.is- the input stream we are loading from- Throws:
java.io.IOException- loading failedMalformedModelException- data can be loaded but has wrong format
-
linear
public static NDList linear(NDArray input, NDArray weight)
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
public static NDList linear(NDArray input, NDArray weight, NDArray bias)
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
public static Linear.Builder builder()
Creates a builder to build aLinear.- Returns:
- a new builder
-
-