public class SequentialBlock extends AbstractBlock
SequentialBlock
is a Block
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
the SequentialBlock
.
SequentialBlock
has no direct parameters.
children, inputNames, inputShapes, parameters, parameterShapeCallbacks, version
Constructor and Description |
---|
SequentialBlock()
Creates an empty sequential block.
|
Modifier and Type | Method and Description |
---|---|
SequentialBlock |
add(Block block)
Adds the given
Block to the block to be executed in order. |
SequentialBlock |
add(java.util.function.Function<NDList,NDList> f)
Adds a
LambdaBlock 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 a
Collection of blocks to be executed in sequence, in order. |
SequentialBlock |
addSingleton(java.util.function.Function<NDArray,NDArray> f)
Adds a
LambdaBlock.singleton(Function) that applies the given function to the
sequence of blocks. |
NDList |
forward(ParameterStore parameterStore,
NDList inputs,
boolean training,
ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
Applies the operating function of the block once.
|
Shape[] |
getOutputShapes(NDManager manager,
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 version,
java.io.DataInputStream is)
Overwrite this to load additional metadata with the parameter values.
|
void |
removeLastBlock()
Removes the
Block added last from the sequence of blocks. |
void |
replaceLastBlock(Block block)
Replaces the
Block last added from the sequence of blocks, and adds the given block. |
java.lang.String |
toString() |
addChildBlock, addParameter, addParameter, addParameter, beforeInitialize, cast, clear, describeInput, getChildren, getDirectParameters, getParameters, getParameterShape, initialize, isInitialized, loadParameters, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forward, forward, validateLayout
public SequentialBlock()
add
and addAll
to add blocks to be
executed in sequence.public SequentialBlock addAll(Block... blocks)
blocks
- the array of blockspublic SequentialBlock addAll(java.util.Collection<Block> blocks)
Collection
of blocks to be executed in sequence, in order.blocks
- the Collection
of blockspublic SequentialBlock add(Block block)
Block
to the block to be executed in order.block
- the block to be added to the sequence of blockspublic SequentialBlock add(java.util.function.Function<NDList,NDList> f)
LambdaBlock
that applies the given function to the sequence of blocks.f
- the function forms the LambdaBlock
public SequentialBlock addSingleton(java.util.function.Function<NDArray,NDArray> f)
LambdaBlock.singleton(Function)
that applies the given function to the
sequence of blocks.f
- the function forms the LambdaBlock
LambdaBlock.singleton(Function)
public void removeLastBlock()
Block
added last from the sequence of blocks.public void replaceLastBlock(Block block)
Block
last added from the sequence of blocks, and adds the given block.block
- the block to replace the last block withpublic NDList forward(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
parameterStore
- the parameter storeinputs
- the input NDListtraining
- true for a training forward passparams
- optional parameterspublic void initializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes)
initializeChildBlocks
in class AbstractBlock
manager
- the manager to use for initializationdataType
- the requested data typeinputShapes
- the expected input shapes for this blockpublic Shape[] getOutputShapes(NDManager manager, Shape[] inputs)
manager
- an NDManagerinputs
- the shapes of the inputspublic void loadMetadata(byte version, java.io.DataInputStream is) throws java.io.IOException, MalformedModelException
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 an MalformedModelException
. After that it restores the input shapes.
loadMetadata
in class AbstractBlock
version
- the version used for loading this metadata.is
- the input stream we are loading fromjava.io.IOException
- loading failedMalformedModelException
- data can be loaded but has wrong formatpublic java.lang.String toString()
toString
in class AbstractBlock