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
public class ParallelBlock extends AbstractBlock
ParallelBlockis aBlockwhose children form a parallel branch in the network and are combined to produce a single output.ParallelBlockhas 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
Constructors Constructor Description ParallelBlock(java.util.function.Function<java.util.List<NDList>,NDList> function)Creates a parallel block whose branches are combined to form a single output by the given function.ParallelBlock(java.util.function.Function<java.util.List<NDList>,NDList> function, java.util.List<Block> blocks)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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ParallelBlockadd(Block block)Adds the givenBlockto the block, which is one parallel branch.ParallelBlockadd(java.util.function.Function<NDList,NDList> f)Adds aLambdaBlock, that applies the given function, to the list of parallel branches.ParallelBlockadd(java.util.function.Function<NDList,NDList> f, java.lang.String name)Adds aLambdaBlock, that applies the given function, to the list of parallel branches.ParallelBlockaddAll(Block... blocks)Adds an array of blocks, each of which is a parallel branch.ParallelBlockaddAll(java.util.Collection<Block> blocks)Adds aCollectionof blocks, each of which is a parallel branch.ParallelBlockaddSingleton(java.util.function.Function<NDArray,NDArray> f)Adds aLambdaBlock.singleton(Function), that applies the given function, to the list of parallel branches.ParallelBlockaddSingleton(java.util.function.Function<NDArray,NDArray> f, java.lang.String name)Adds aLambdaBlock.singleton(Function), that applies the given function, to the list of parallel branches.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.protected NDListforwardInternal(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[] inputShapes)Returns the expected output shapes of the block for the specified input shapes.voidinitializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes)Initializes the Child blocks of this block.voidloadMetadata(byte loadVersion, java.io.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 Detail
-
ParallelBlock
public ParallelBlock(java.util.function.Function<java.util.List<NDList>,NDList> function)
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
public ParallelBlock(java.util.function.Function<java.util.List<NDList>,NDList> function, java.util.List<Block> blocks)
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 Detail
-
addAll
public final ParallelBlock addAll(Block... blocks)
Adds an array of blocks, each of which is a parallel branch.- Parameters:
blocks- the array of blocks to add- Returns:
- this block
-
addAll
public final ParallelBlock addAll(java.util.Collection<Block> blocks)
Adds aCollectionof blocks, each of which is a parallel branch.- Parameters:
blocks- theCollectionof blocks to add- Returns:
- this block
-
add
public final ParallelBlock add(Block block)
Adds the givenBlockto the block, which is one parallel branch.- Parameters:
block- the block to be added as a parallel branch- Returns:
- this block
-
add
public final ParallelBlock add(java.util.function.Function<NDList,NDList> f)
Adds aLambdaBlock, that applies the given function, to the list of parallel branches.- Parameters:
f- the function that forms theLambdaBlock- Returns:
- this block
-
add
public ParallelBlock add(java.util.function.Function<NDList,NDList> f, java.lang.String name)
Adds aLambdaBlock, that applies the given function, to the list of parallel branches.- Parameters:
f- the function forms theLambdaBlockname- the function name- Returns:
- this block
-
addSingleton
public ParallelBlock addSingleton(java.util.function.Function<NDArray,NDArray> f)
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:
LambdaBlock.singleton(Function)
-
addSingleton
public ParallelBlock addSingleton(java.util.function.Function<NDArray,NDArray> f, java.lang.String name)
Adds aLambdaBlock.singleton(Function), that applies the given function, to the list of parallel branches.- Parameters:
f- the function forms theLambdaBlockname- the function name- Returns:
- this block
- See Also:
LambdaBlock.singleton(Function)
-
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
-
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:
forwardInternalin 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:
AbstractBaseBlock.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:
initializeChildBlocksin classAbstractBaseBlock- Parameters:
manager- the manager to use for initializationdataType- the requested data typeinputShapes- the expected input shapes for this block
-
getOutputShapes
public Shape[] getOutputShapes(Shape[] inputShapes)
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, 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
-
-