Package ai.djl.modality.nlp
Class Decoder
- java.lang.Object
-
- ai.djl.nn.AbstractBaseBlock
-
- ai.djl.nn.AbstractBlock
-
- ai.djl.modality.nlp.Decoder
-
- All Implemented Interfaces:
Block
public abstract class Decoder extends AbstractBlock
Decoderis an abstract block that be can used as decoder in encoder-decoder architecture. This abstraction, along withEncoder, comes into play in theEncoderDecoderclass, and facilitate implementing encoder-decoder models for different tasks and inputs.
-
-
Field Summary
Fields Modifier and Type Field Description protected Blockblock-
Fields inherited from class ai.djl.nn.AbstractBlock
children, parameters
-
Fields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version
-
-
Method Summary
All 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.voidinitializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes)Initializes the Child blocks of this block.voidloadParameters(NDManager manager, java.io.DataInputStream is)Loads the parameters from the given input stream.voidsaveParameters(java.io.DataOutputStream os)Writes the parameters of the block to the given outputStream.-
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, isInitialized, loadMetadata, prepare, readInputShapes, saveInputShapes, saveMetadata, 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 Detail
-
block
protected Block block
-
-
Constructor Detail
-
Decoder
public Decoder(byte version, Block block)Constructs a new instance ofDecoderwith the given block. Use this constructor if you are planning to use pre-trained embeddings that don't need further training.- Parameters:
block- the block to be used to decodeversion- the version to use for parameter and metadata serialization
-
-
Method Detail
-
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 classAbstractBaseBlock- Parameters:
parameterStore- the parameter storeinputs- the input NDListtraining- true for a training forward passparams- optional parameters- Returns:
- the output of the forward pass
-
initializeChildBlocks
public void initializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes)
Initializes the Child blocks of this block. You need to override this method if your subclass has child blocks. Used to determine the correct input shapes for child blocks based on the requested input shape for this block.- Overrides:
initializeChildBlocksin classAbstractBaseBlock- Parameters:
manager- the manager to use for initializationdataType- the requested data typeinputShapes- the expected input shapes for this block
-
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
-
saveParameters
public void saveParameters(java.io.DataOutputStream os) throws java.io.IOExceptionWrites the parameters of the block to the given outputStream.- Specified by:
saveParametersin interfaceBlock- Overrides:
saveParametersin classAbstractBaseBlock- Parameters:
os- the outputstream to save the parameters to- Throws:
java.io.IOException- if an I/O error occurs
-
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 classAbstractBaseBlock- 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
-
-