Package ai.djl.nn
Class LambdaBlock
- java.lang.Object
-
- ai.djl.nn.AbstractBlock
-
- ai.djl.nn.LambdaBlock
-
- All Implemented Interfaces:
Block
public class LambdaBlock extends AbstractBlock
LambdaBlockis aBlockwith no parameters or children.LambdaBlockallows converting any function that takes anNDListas input and returns anNDListinto a parameter-less and child-lessBlock. This can be useful in converting activation functions, identity blocks, and more. For example, identity block can be created asnew LambdaBlock(x -> x).
-
-
Field Summary
-
Fields inherited from class ai.djl.nn.AbstractBlock
children, inputNames, inputShapes, parameters, version
-
-
Constructor Summary
Constructors Constructor Description LambdaBlock(java.util.function.Function<NDList,NDList> lambda)Creates a LambdaBlock that can apply the specified function.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.Shape[]getOutputShapes(Shape[] inputShapes)Returns the expected output shapes of the block for the specified input shapes.voidloadParameters(NDManager manager, java.io.DataInputStream is)Loads the parameters from the given input stream.static LambdaBlocksingleton(java.util.function.Function<NDArray,NDArray> lambda)Creates aLambdaBlockfor a singleton function.java.lang.StringtoString()-
Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addParameter, beforeInitialize, cast, clear, describeInput, forward, forward, forwardInternal, getChildren, getDirectParameters, getParameters, initialize, initializeChildBlocks, isInitialized, loadMetadata, prepare, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer, setInitializer
-
-
-
-
Method Detail
-
singleton
public static LambdaBlock singleton(java.util.function.Function<NDArray,NDArray> lambda)
Creates aLambdaBlockfor a singleton function.- Parameters:
lambda- a function accepting a singletonNDListand returning another sinletonNDList- Returns:
- a new
LambdaBlockfor the 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 classAbstractBlock- Parameters:
parameterStore- the parameter storeinputs- the input NDListtraining- true for a training forward passparams- optional parameters- Returns:
- the output of the forward pass
-
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
-
loadParameters
public void loadParameters(NDManager manager, java.io.DataInputStream is) throws java.io.IOException, MalformedModelException
Loads the parameters from the given input stream.- Specified by:
loadParametersin interfaceBlock- Overrides:
loadParametersin classAbstractBlock- Parameters:
manager- an NDManager to create the parameter arraysis- the inputstream that stream the parameter values- Throws:
java.io.IOException- if an I/O error occursMalformedModelException- if the model file is corrupted or unsupported
-
toString
public java.lang.String toString()
- Overrides:
toStringin classAbstractBlock
-
-