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
LambdaBlock
is aBlock
with no parameters or children.LambdaBlock
allows converting any function that takes anNDList
as input and returns anNDList
into 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 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.Shape[]
getOutputShapes(Shape[] inputShapes)
Returns the expected output shapes of the block for the specified input shapes.void
loadParameters(NDManager manager, java.io.DataInputStream is)
Loads the parameters from the given input stream.static LambdaBlock
singleton(java.util.function.Function<NDArray,NDArray> lambda)
Creates aLambdaBlock
for a singleton function.java.lang.String
toString()
-
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 aLambdaBlock
for a singleton function.- Parameters:
lambda
- a function accepting a singletonNDList
and returning another sinletonNDList
- Returns:
- a new
LambdaBlock
for 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:
forwardInternal
in 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:
loadParameters
in interfaceBlock
- Overrides:
loadParameters
in 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:
toString
in classAbstractBlock
-
-