public class LambdaBlock extends AbstractBlock
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)
.
children, inputNames, inputShapes, parameters, parameterShapeCallbacks, version
Constructor and Description |
---|
LambdaBlock(java.util.function.Function<NDList,NDList> lambda)
Creates a LambdaBlock that can apply the specified function.
|
Modifier and Type | Method and Description |
---|---|
protected NDList |
forwardInternal(ParameterStore parameterStore,
NDList inputs,
boolean training,
ai.djl.util.PairList<java.lang.String,java.lang.Object> params) |
Shape[] |
getOutputShapes(NDManager manager,
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 a
LambdaBlock for a singleton function. |
java.lang.String |
toString() |
addChildBlock, addParameter, addParameter, addParameter, beforeInitialize, cast, clear, describeInput, forward, getChildren, getDirectParameters, getParameters, getParameterShape, initialize, initializeChildBlocks, isInitialized, loadMetadata, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forward, forward, validateLayout
public static LambdaBlock singleton(java.util.function.Function<NDArray,NDArray> lambda)
LambdaBlock
for a singleton function.lambda
- a function accepting a singleton NDList
and returning another sinleton
NDList
LambdaBlock
for the functionprotected NDList forwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
forwardInternal
in class AbstractBlock
public Shape[] getOutputShapes(NDManager manager, Shape[] inputShapes)
manager
- an NDManagerinputShapes
- the shapes of the inputspublic void loadParameters(NDManager manager, java.io.DataInputStream is) throws java.io.IOException, MalformedModelException
loadParameters
in interface Block
loadParameters
in class AbstractBlock
manager
- an NDManager to create the parameter arraysis
- the inputstream that stream the parameter valuesjava.io.IOException
- if an I/O error occursMalformedModelException
- if the model file is corrupted or unsupportedpublic java.lang.String toString()
toString
in class AbstractBlock