Package ai.djl.nn
Class ParallelBlock
java.lang.Object
ai.djl.nn.AbstractBaseBlock
ai.djl.nn.AbstractBlock
ai.djl.nn.ParallelBlock
- All Implemented Interfaces:
Block
ParallelBlock
is a Block
whose children form a parallel branch in the network and
are combined to produce a single output.
ParallelBlock
has no direct parameters.
-
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
ConstructorsConstructorDescriptionParallelBlock
(Function<List<NDList>, NDList> function) Creates a parallel block whose branches are combined to form a single output by the given function.Creates a parallel block whose branches are formed by each block in the list of blocks, and are combined to form a single output by the given function. -
Method Summary
Modifier and TypeMethodDescriptionfinal ParallelBlock
Adds the givenBlock
to the block, which is one parallel branch.final ParallelBlock
Adds aLambdaBlock
, that applies the given function, to the list of parallel branches.Adds aLambdaBlock
, that applies the given function, to the list of parallel branches.final ParallelBlock
Adds an array of blocks, each of which is a parallel branch.final ParallelBlock
addAll
(Collection<Block> blocks) Adds aCollection
of blocks, each of which is a parallel branch.Adds aLambdaBlock.singleton(Function)
, that applies the given function, to the list of parallel branches.addSingleton
(Function<NDArray, NDArray> f, String name) Adds aLambdaBlock.singleton(Function)
, that applies the given function, to the list of parallel branches.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.protected NDList
forwardInternal
(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)
after initialization.Shape[]
getOutputShapes
(Shape[] inputShapes) 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, DataInputStream is) Overwrite this to load additional metadata with the parameter values.Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addChildBlock, addChildBlockSingleton, addParameter, getChildren, getDirectParameters
Methods inherited from class ai.djl.nn.AbstractBaseBlock
beforeInitialize, cast, clear, describeInput, forward, forward, getInputShapes, getOutputDataTypes, getParameters, initialize, isInitialized, loadParameters, prepare, readInputShapes, saveInputShapes, saveMetadata, 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, getOutputShapes
-
Constructor Details
-
ParallelBlock
Creates a parallel block whose branches are combined to form a single output by the given function.- Parameters:
function
- the function to define how the parallel branches are combined to form a single output
-
ParallelBlock
Creates a parallel block whose branches are formed by each block in the list of blocks, and are combined to form a single output by the given function.- Parameters:
function
- the function to define how the parallel branches are combinedblocks
- the blocks that form each of the parallel branches
-
-
Method Details
-
addAll
Adds an array of blocks, each of which is a parallel branch.- Parameters:
blocks
- the array of blocks to add- Returns:
- this block
-
addAll
Adds aCollection
of blocks, each of which is a parallel branch.- Parameters:
blocks
- theCollection
of blocks to add- Returns:
- this block
-
add
Adds the givenBlock
to the block, which is one parallel branch.- Parameters:
block
- the block to be added as a parallel branch- Returns:
- this block
-
add
Adds aLambdaBlock
, that applies the given function, to the list of parallel branches.- Parameters:
f
- the function that forms theLambdaBlock
- Returns:
- this block
-
add
Adds aLambdaBlock
, that applies the given function, to the list of parallel branches.- Parameters:
f
- the function forms theLambdaBlock
name
- the function name- Returns:
- this block
-
addSingleton
Adds aLambdaBlock.singleton(Function)
, that applies the given function, to the list of parallel branches.- Parameters:
f
- the function forms theLambdaBlock
- Returns:
- this block
- See Also:
-
addSingleton
Adds aLambdaBlock.singleton(Function)
, that applies the given function, to the list of parallel branches.- Parameters:
f
- the function forms theLambdaBlock
name
- the function name- Returns:
- this block
- See Also:
-
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
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)
after initialization.- Overrides:
forwardInternal
in classAbstractBaseBlock
- Parameters:
parameterStore
- the parameter storedata
- the input data NDListlabels
- the input labels NDListparams
- optional parameters- Returns:
- the output of the forward pass
- See Also:
-
initializeChildBlocks
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 classAbstractBaseBlock
- Parameters:
manager
- the manager to use for initializationdataType
- the requested data typeinputShapes
- the expected input shapes for this block
-
getOutputShapes
Returns the expected output shapes of the block for the specified input shapes.- Parameters:
inputShapes
- the shapes of the inputs- Returns:
- the expected output shapes of the block
-
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
-