Class RecurrentBlock
- java.lang.Object
-
- ai.djl.nn.AbstractBlock
-
- ai.djl.nn.recurrent.RecurrentBlock
-
- All Implemented Interfaces:
Block
public abstract class RecurrentBlock extends AbstractBlock
RecurrentBlockis an abstract implementation of recurrent neural networks.Recurrent neural networks are neural networks with hidden states. They are very popular for natural language processing tasks, and other tasks which involve sequential data.
This [article](http://karpathy.github.io/2015/05/21/rnn-effectiveness/) written by Andrej Karpathy provides a detailed explanation of recurrent neural networks.
Currently, vanilla RNN, LSTM and GRU are implemented, with both multi-layer and bidirectional support.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRecurrentBlock.BaseBuilder<T extends RecurrentBlock.BaseBuilder>The Builder to construct aRecurrentBlocktype ofBlock.
-
Field Summary
Fields Modifier and Type Field Description protected booleanbatchFirstprotected booleanbidirectionalprotected floatdropRateprotected intgatesprotected booleanhasBiasesprotected intnumLayersprotected booleanreturnStateprotected longstateSize-
Fields inherited from class ai.djl.nn.AbstractBlock
children, inputNames, inputShapes, parameters, version
-
-
Constructor Summary
Constructors Constructor Description RecurrentBlock(RecurrentBlock.BaseBuilder<?> builder)Creates aRecurrentBlockobject.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidbeforeInitialize(Shape... inputShapes)Performs any action necessary before initialization.protected intgetNumDirections()Shape[]getOutputShapes(Shape[] inputs)Returns the expected output shapes of the block for the specified input shapes.voidloadMetadata(byte loadVersion, java.io.DataInputStream is)Overwrite this to load additional metadata with the parameter values.voidprepare(Shape[] inputs)Sets the shape ofParameters.-
Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addParameter, cast, clear, describeInput, forward, forward, forwardInternal, forwardInternal, getChildren, getDirectParameters, getParameters, initialize, initializeChildBlocks, isInitialized, loadParameters, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer, setInitializer, toString
-
-
-
-
Field Detail
-
stateSize
protected long stateSize
-
dropRate
protected float dropRate
-
numLayers
protected int numLayers
-
gates
protected int gates
-
batchFirst
protected boolean batchFirst
-
hasBiases
protected boolean hasBiases
-
bidirectional
protected boolean bidirectional
-
returnState
protected boolean returnState
-
-
Constructor Detail
-
RecurrentBlock
public RecurrentBlock(RecurrentBlock.BaseBuilder<?> builder)
Creates aRecurrentBlockobject.- Parameters:
builder- theBuilderthat has the necessary configurations
-
-
Method Detail
-
getOutputShapes
public Shape[] getOutputShapes(Shape[] inputs)
Returns the expected output shapes of the block for the specified input shapes.- Parameters:
inputs- the shapes of the inputs- Returns:
- the expected output shapes of the block
-
beforeInitialize
protected void beforeInitialize(Shape... inputShapes)
Performs any action necessary before initialization. For example, keep the input information or verify the layout.- Overrides:
beforeInitializein classAbstractBlock- Parameters:
inputShapes- the expected shapes of the input
-
prepare
public void prepare(Shape[] inputs)
Sets the shape ofParameters.- Overrides:
preparein classAbstractBlock- Parameters:
inputs- the shapes of inputs
-
loadMetadata
public void loadMetadata(byte loadVersion, java.io.DataInputStream is) throws java.io.IOException, MalformedModelExceptionOverwrite this to load additional metadata with the parameter values.If you overwrite
AbstractBlock.saveMetadata(DataOutputStream)or need to provide backward compatibility to older binary formats, you prabably need to overwrite this. This default implementation checks if the version number fits, if not it throws anMalformedModelException. After that it restores the input shapes.- Overrides:
loadMetadatain classAbstractBlock- Parameters:
loadVersion- the version used for loading this metadata.is- the input stream we are loading from- Throws:
java.io.IOException- loading failedMalformedModelException- data can be loaded but has wrong format
-
getNumDirections
protected int getNumDirections()
-
-