Class Multiplication
- java.lang.Object
-
- ai.djl.nn.AbstractBaseBlock
-
- ai.djl.nn.AbstractBlock
-
- ai.djl.nn.core.Multiplication
-
- All Implemented Interfaces:
Block
public class Multiplication extends AbstractBlock
A Multiplication block performs an element-wise multiplication of inputs and weights as opposed to aLinearblock which additionally sums up each element-wise multiplication.Similar to a
LinearCollection, multiple split dimensions are supported but they remain optional (i.e. \(t\) can be zero). Other differences to aLinearblock are that the weight has an additional dimension of size 1 interspersed (to broadcast the weight to every input of the batch when applying the internally used algebraic operationNDArray.mul(NDArray)) and that biases are not supported.Caution: the output-channel is the left-most dimension as opposed to traditionally being the right-most dimension. As the output is one dimension larger than that of a
Linearblock, it is more efficient and therefore recommended to apply an aggregating function (like the sum) first and only then shift the first axis of the aggregated and thus smallerNDArrayinstance into last position.It has the following shapes:
- input X: [x_1, s_1, s_2, …, s_t, input_dim]
- weight W: [units, 1, s_1, s_2, …, s_t, input_dim]
- output Y: [units, x_1, s_1, s_2, …, s_t, input_dim]
The Multiplication block should be constructed using
Multiplication.Builder.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMultiplication.BuilderThe Builder to construct aMultiplicationtype ofBlock.
-
Field Summary
-
Fields inherited from class ai.djl.nn.AbstractBlock
children, parameters
-
Fields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, 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 Multiplication.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.voidloadMetadata(byte loadVersion, java.io.DataInputStream is)Overwrite this to load additional metadata with the parameter values.NDListmultiply(NDArray input, NDArray weight)Applies an element-wise multiplication to the incoming data.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, 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, getOutputShapes
-
-
-
-
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 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
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 classAbstractBaseBlock- 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 classAbstractBaseBlock- Parameters:
inputShapes- the expected shapes of the input
-
prepare
public void prepare(Shape[] inputShapes)
Sets the shape ofParameters.- Overrides:
preparein classAbstractBaseBlock- 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 classAbstractBaseBlock- 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
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:
loadMetadatain classAbstractBaseBlock- 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
-
multiply
public NDList multiply(NDArray input, NDArray weight)
Applies an element-wise multiplication to the incoming data.- Parameters:
input- The incoming dataweight- The weight of this block- Returns:
- element-wise multiplication of input and weight using broadcasting rules
-
builder
public static Multiplication.Builder builder()
Creates a builder to build aLinear.- Returns:
- a new builder
-
-