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, parametersFields 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 NDListforwardInternal(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.voidloadParameters(NDManager manager, DataInputStream is) Loads the parameters from the given input stream.static LambdaBlockCreates aLambdaBlockfor a singleton function.static LambdaBlockCreates aLambdaBlockfor a singleton function.Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addChildBlock, addChildBlockSingleton, addParameter, getChildren, getDirectParametersMethods 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, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ai.djl.nn.Block
forward, freezeParameters, freezeParameters, getCustomMetadata, 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 aLambdaBlockfor a singleton function.- Parameters:
lambda- a function accepting a singletonNDListand returning another singletonNDList- Returns:
- a new
LambdaBlockfor the function
-
singleton
Creates aLambdaBlockfor a singleton function.- Parameters:
lambda- a function accepting a singletonNDListand returning another singletonNDListname- the function name- Returns:
- a new
LambdaBlockfor 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:
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
-
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:
loadParametersin interfaceBlock- Overrides:
loadParametersin 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
-