public final class BertBlock extends AbstractBlock
This closely follows the original Devlin et. al. paper and its reference implementation.
| Modifier and Type | Class and Description |
|---|---|
static class |
BertBlock.Builder
|
children, inputNames, inputShapes, parameters, parameterShapeCallbacks, version| Modifier and Type | Method and Description |
|---|---|
static BertBlock.Builder |
builder()
Returns a new BertBlock builder.
|
static NDArray |
createAttentionMaskFromInputMask(NDArray ids,
NDArray mask)
Creates a 3D attention mask from a 2D tensor mask.
|
NDList |
forward(ParameterStore ps,
NDArray tokenIds,
NDArray typeIds,
NDArray masks,
boolean training)
Embeds the input, runs it through a transformer encoder stack and outputs the resulting
embeddings and the pooled classifier.
|
NDList |
forward(ParameterStore ps,
NDList inputs,
boolean training)
Applies the operating function of the block once.
|
protected NDList |
forwardInternal(ParameterStore ps,
NDList inputs,
boolean training,
ai.djl.util.PairList<java.lang.String,java.lang.Object> params) |
int |
getEmbeddingSize()
Returns the embedding size used for tokens.
|
Shape[] |
getOutputShapes(NDManager manager,
Shape[] inputShapes)
Returns the expected output shapes of the block for the specified input shapes.
|
int |
getTokenDictionarySize()
Returns the size of the token dictionary.
|
IdEmbedding |
getTokenEmbedding()
Returns the token embedding used by this Bert model.
|
int |
getTypeDictionarySize()
Returns the size of the type dictionary.
|
void |
initializeChildBlocks(NDManager manager,
DataType dataType,
Shape... inputShapes)
Initializes the Child blocks of this block.
|
addChildBlock, addParameter, addParameter, addParameter, beforeInitialize, cast, clear, describeInput, forward, getChildren, getDirectParameters, getParameters, getParameterShape, initialize, isInitialized, loadMetadata, loadParameters, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforward, validateLayoutpublic IdEmbedding getTokenEmbedding()
public int getEmbeddingSize()
public int getTokenDictionarySize()
public int getTypeDictionarySize()
public Shape[] getOutputShapes(NDManager manager, Shape[] inputShapes)
manager - an NDManagerinputShapes - the shapes of the inputspublic void initializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes)
initializeChildBlocks in class AbstractBlockmanager - the manager to use for initializationdataType - the requested data typeinputShapes - the expected input shapes for this blockpublic static NDArray createAttentionMaskFromInputMask(NDArray ids, NDArray mask)
ids - 2D Tensor of shape (B, F)mask - 2D Tensor of shape (B, T)protected NDList forwardInternal(ParameterStore ps, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
forwardInternal in class AbstractBlockpublic NDList forward(ParameterStore ps, NDList inputs, boolean training)
ps - the parameter storeinputs - the input NDListtraining - true for a training forward passpublic NDList forward(ParameterStore ps, NDArray tokenIds, NDArray typeIds, NDArray masks, boolean training)
ps - the parameter storetokenIds - Ids for the tokens (word pieces, BPE pieces, words etc.) (B, S)typeIds - Ids for the token type, during pretraining this is used to encode the
first/second sentence (B, S)masks - Masks for the input, used to mask out tokens in input that is shorter than the
max number of input tokens (B, S)training - true=apply dropout etc.public static BertBlock.Builder builder()