Package ai.djl.nn
Class SequentialBlock
- java.lang.Object
-
- ai.djl.nn.AbstractBlock
-
- ai.djl.nn.SequentialBlock
-
- All Implemented Interfaces:
Block
- Direct Known Subclasses:
PointwiseFeedForwardBlock
public class SequentialBlock extends AbstractBlock
SequentialBlock
is aBlock
whose children form a chain of blocks with each child block feeding its output to the next. The output of the last child is returned as the output of theSequentialBlock
.SequentialBlock
has no direct parameters.
-
-
Field Summary
-
Fields inherited from class ai.djl.nn.AbstractBlock
children, inputNames, inputShapes, parameters, version
-
-
Constructor Summary
Constructors Constructor Description SequentialBlock()
Creates an empty sequential block.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SequentialBlock
add(Block block)
Adds the givenBlock
to the block to be executed in order.SequentialBlock
add(java.util.function.Function<NDList,NDList> f)
Adds aLambdaBlock
that applies the given function to the sequence of blocks.SequentialBlock
addAll(Block... blocks)
Adds an array of blocks to be executed in sequence, in order.SequentialBlock
addAll(java.util.Collection<Block> blocks)
Adds aCollection
of blocks to be executed in sequence, in order.SequentialBlock
addSingleton(java.util.function.Function<NDArray,NDArray> f)
Adds aLambdaBlock.singleton(Function)
that applies the given function to the sequence of blocks.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.protected NDList
forwardInternal(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)
after initialization.Shape[]
getOutputShapes(Shape[] inputs)
Returns the expected output shapes of the block for the specified input shapes.void
initializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes)
Initializes the Child blocks of this block.void
loadMetadata(byte loadVersion, java.io.DataInputStream is)
Overwrite this to load additional metadata with the parameter values.void
removeLastBlock()
Removes theBlock
added last from the sequence of blocks.void
replaceLastBlock(Block block)
Replaces theBlock
last added from the sequence of blocks, and adds the given block.java.lang.String
toString()
-
Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addParameter, beforeInitialize, cast, clear, describeInput, forward, forward, getChildren, getDirectParameters, getParameters, initialize, isInitialized, loadParameters, prepare, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer, setInitializer
-
-
-
-
Method Detail
-
addAll
public SequentialBlock addAll(Block... blocks)
Adds an array of blocks to be executed in sequence, in order.- Parameters:
blocks
- the array of blocks- Returns:
- this block
-
addAll
public SequentialBlock addAll(java.util.Collection<Block> blocks)
Adds aCollection
of blocks to be executed in sequence, in order.- Parameters:
blocks
- theCollection
of blocks- Returns:
- this block
-
add
public SequentialBlock add(Block block)
Adds the givenBlock
to the block to be executed in order.- Parameters:
block
- the block to be added to the sequence of blocks- Returns:
- this block
-
add
public SequentialBlock add(java.util.function.Function<NDList,NDList> f)
Adds aLambdaBlock
that applies the given function to the sequence of blocks.- Parameters:
f
- the function forms theLambdaBlock
- Returns:
- this block
-
addSingleton
public SequentialBlock addSingleton(java.util.function.Function<NDArray,NDArray> f)
Adds aLambdaBlock.singleton(Function)
that applies the given function to the sequence of blocks.- Parameters:
f
- the function forms theLambdaBlock
- Returns:
- this block
- See Also:
LambdaBlock.singleton(Function)
-
removeLastBlock
public void removeLastBlock()
Removes theBlock
added last from the sequence of blocks.
-
replaceLastBlock
public void replaceLastBlock(Block block)
Replaces theBlock
last added from the sequence of blocks, and adds the given block.- Parameters:
block
- the block to replace the last block with
-
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:
forwardInternal
in classAbstractBlock
- Parameters:
parameterStore
- the parameter storeinputs
- the input NDListtraining
- true for a training forward passparams
- optional parameters- Returns:
- the output of the forward pass
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)
after initialization.- Overrides:
forwardInternal
in classAbstractBlock
- Parameters:
parameterStore
- the parameter storedata
- the input data NDListlabels
- the input labels NDListparams
- optional parameters- Returns:
- the output of the forward pass
- See Also:
AbstractBlock.forward(ParameterStore, NDList, boolean, PairList)
-
initializeChildBlocks
public void initializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes)
Initializes the Child blocks of this block. You need to override this method if your subclass has child blocks. Used to determine the correct input shapes for child blocks based on the requested input shape for this block.- Overrides:
initializeChildBlocks
in classAbstractBlock
- Parameters:
manager
- the manager to use for initializationdataType
- the requested data typeinputShapes
- the expected input shapes for this block
-
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
-
loadMetadata
public void loadMetadata(byte loadVersion, java.io.DataInputStream is) throws java.io.IOException, MalformedModelException
Overwrite 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:
loadMetadata
in 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
-
toString
public java.lang.String toString()
- Overrides:
toString
in classAbstractBlock
-
-