Package ai.djl.nn
Class LambdaBlock
java.lang.Object
ai.djl.nn.AbstractBaseBlock
ai.djl.nn.AbstractBlock
ai.djl.nn.LambdaBlock
- All Implemented Interfaces:
Block
LambdaBlock
is a Block
with no parameters or children.
LambdaBlock
allows converting any function that takes an NDList
as input and
returns an NDList
into a parameter-less and child-less Block
. This can be useful
in converting activation functions, identity blocks, and more. For example, identity block can be
created as new LambdaBlock(x -> x)
.
-
Field Summary
FieldsFields inherited from class ai.djl.nn.AbstractBlock
children, parameters
Fields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version
-
Constructor Summary
ConstructorsConstructorDescriptionLambdaBlock
(Function<NDList, NDList> lambda) Creates a LambdaBlock that can apply the specified function.LambdaBlock
(Function<NDList, NDList> lambda, String name) Creates a LambdaBlock that can apply the specified function. -
Method Summary
Modifier and TypeMethodDescriptionprotected 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.getName()
Returns the lambda function name.Shape[]
getOutputShapes
(Shape[] inputShapes) Returns the expected output shapes of the block for the specified input shapes.void
loadParameters
(NDManager manager, DataInputStream is) Loads the parameters from the given input stream.static LambdaBlock
Creates aLambdaBlock
for a singleton function.static LambdaBlock
Creates aLambdaBlock
for a singleton function.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, forwardInternal, getInputShapes, getOutputDataTypes, getParameters, initialize, initializeChildBlocks, isInitialized, loadMetadata, 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
-
Field Details
-
DEFAULT_NAME
- See Also:
-
-
Constructor Details
-
LambdaBlock
Creates a LambdaBlock that can apply the specified function.- Parameters:
lambda
- the function to apply
-
LambdaBlock
Creates a LambdaBlock that can apply the specified function.- Parameters:
lambda
- the function to applyname
- the function name
-
-
Method Details
-
getName
Returns the lambda function name.- Returns:
- the lambda function name
-
singleton
Creates aLambdaBlock
for a singleton function.- Parameters:
lambda
- a function accepting a singletonNDList
and returning another singletonNDList
- Returns:
- a new
LambdaBlock
for the function
-
singleton
Creates aLambdaBlock
for a singleton function.- Parameters:
lambda
- a function accepting a singletonNDList
and returning another singletonNDList
name
- the function name- Returns:
- a new
LambdaBlock
for the function
-
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
-
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
-
loadParameters
public void loadParameters(NDManager manager, DataInputStream is) throws IOException, MalformedModelException Loads the parameters from the given input stream.- Specified by:
loadParameters
in interfaceBlock
- Overrides:
loadParameters
in classAbstractBaseBlock
- Parameters:
manager
- an NDManager to create the parameter arraysis
- the inputstream that stream the parameter values- Throws:
IOException
- if an I/O error occursMalformedModelException
- if the model file is corrupted or unsupported
-