Package ai.djl.nn.transformer
Class BertBlock
java.lang.Object
ai.djl.nn.AbstractBaseBlock
ai.djl.nn.AbstractBlock
ai.djl.nn.transformer.BertBlock
- All Implemented Interfaces:
Block
Implements the core bert model (without next sentence and masked language task) of bert.
This closely follows the original Devlin et. al. paper and its reference implementation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class -
Field Summary
Fields inherited from class ai.djl.nn.AbstractBlock
children, parametersFields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version -
Method Summary
Modifier and TypeMethodDescriptionstatic BertBlock.Builderbuilder()Returns a new BertBlock builder.static NDArraycreateAttentionMaskFromInputMask(NDArray ids, NDArray mask) Creates a 3D attention mask from a 2D tensor mask.protected NDListforwardInternal(ParameterStore ps, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)after initialization.intReturns the embedding size used for tokens.Shape[]getOutputShapes(Shape[] inputShapes) Returns the expected output shapes of the block for the specified input shapes.intReturns the size of the token dictionary.Returns the token embedding used by this Bert model.intReturns the size of the type dictionary.voidinitializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes) Initializes the Child blocks of this block.Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addChildBlock, addChildBlockSingleton, addParameter, getChildren, getDirectParametersMethods inherited from class ai.djl.nn.AbstractBaseBlock
beforeInitialize, cast, clear, describeInput, forward, forward, forwardInternal, getInputShapes, getOutputDataTypes, getParameters, initialize, isInitialized, loadMetadata, loadParameters, prepare, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer, setInitializer, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ai.djl.nn.Block
forward, freezeParameters, freezeParameters, getOutputShapes
-
Method Details
-
getTokenEmbedding
Returns the token embedding used by this Bert model.- Returns:
- the token embedding used by this Bert model
-
getEmbeddingSize
public int getEmbeddingSize()Returns the embedding size used for tokens.- Returns:
- the embedding size used for tokens
-
getTokenDictionarySize
public int getTokenDictionarySize()Returns the size of the token dictionary.- Returns:
- the size of the token dictionary
-
getTypeDictionarySize
public int getTypeDictionarySize()Returns the size of the type dictionary.- Returns:
- the size of the type dictionary
-
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
-
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:
initializeChildBlocksin classAbstractBaseBlock- Parameters:
manager- the manager to use for initializationdataType- the requested data typeinputShapes- the expected input shapes for this block
-
createAttentionMaskFromInputMask
Creates a 3D attention mask from a 2D tensor mask.- Parameters:
ids- 2D Tensor of shape (B, F)mask- 2D Tensor of shape (B, T)- Returns:
- float tensor of shape (B, F, T)
-
forwardInternal
protected NDList forwardInternal(ParameterStore ps, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)after initialization.- Specified by:
forwardInternalin classAbstractBaseBlock- Parameters:
ps- the parameter storeinputs- the input NDListtraining- true for a training forward passparams- optional parameters- Returns:
- the output of the forward pass
-
builder
Returns a new BertBlock builder.- Returns:
- a new BertBlock builder.
-