Package ai.djl.modality.nlp
Class Encoder
java.lang.Object
ai.djl.nn.AbstractBaseBlock
ai.djl.nn.AbstractBlock
ai.djl.modality.nlp.Encoder
- All Implemented Interfaces:
Block
Encoder
is an abstract block that be can used as encoder in encoder-decoder architecture.
This abstraction, along with Decoder
, comes into play in the EncoderDecoder
class, and facilitate implementing encoder-decoder models for different tasks and inputs.-
Field Summary
FieldsFields inherited from class ai.djl.nn.AbstractBlock
children, parameters
Fields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected 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.protected NDList
forwardInternal
(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)
after initialization.Shape[]
getOutputShapes
(Shape[] inputShapes) Returns the expected output shapes of the block for the specified input shapes.abstract NDList
Gets the state of the encoder from the given encoder output.void
initializeChildBlocks
(NDManager manager, DataType dataType, Shape... inputShapes) Initializes the Child blocks of this block.void
loadParameters
(NDManager manager, DataInputStream is) Loads the parameters from the given input stream.void
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, 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 Details
-
block
-
-
Constructor Details
-
Encoder
Constructs a new instance ofEncoder
with the given block.- Parameters:
version
- the version to use for parameter and metadata serializationblock
- the encoder block
-
-
Method Details
-
getStates
Gets the state of the encoder from the given encoder output.- Parameters:
encoderOutput
- anNDList
that contains the encoder output- Returns:
- the state of the encoder
-
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:
forwardInternal
in classAbstractBaseBlock
- Parameters:
parameterStore
- the parameter storeinputs
- the input NDListtraining
- true for a training forward passparams
- optional parameters- Returns:
- the output of the forward pass
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<String, Object> params) Description copied from class:AbstractBaseBlock
A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)
after initialization.- Overrides:
forwardInternal
in classAbstractBaseBlock
- Parameters:
parameterStore
- the parameter storedata
- the input data NDListlabels
- the input labels NDListparams
- optional parameters- Returns:
- the output of the forward pass
- See Also:
-
initializeChildBlocks
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:
initializeChildBlocks
in classAbstractBaseBlock
- Parameters:
manager
- the manager to use for initializationdataType
- the requested data typeinputShapes
- the expected input shapes for this block
-
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
-
saveParameters
Writes the parameters of the block to the given outputStream.- Specified by:
saveParameters
in interfaceBlock
- Overrides:
saveParameters
in classAbstractBaseBlock
- Parameters:
os
- the outputstream to save the parameters to- Throws:
IOException
- if an I/O error occurs
-
loadParameters
public void loadParameters(NDManager manager, DataInputStream is) throws IOException, MalformedModelException Loads the parameters from the given input stream.- Specified by:
loadParameters
in interfaceBlock
- Overrides:
loadParameters
in 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
-