T
- the type of item that should be embedded and map to the arraypublic abstract class Embedding<T> extends AbstractBlock implements AbstractIndexedEmbedding<T>
NDArray
s.Modifier and Type | Class and Description |
---|---|
static class |
Embedding.BaseBuilder<T,B extends Embedding.BaseBuilder<T,B>>
|
protected class |
Embedding.DefaultEmbedding |
protected class |
Embedding.DefaultItem |
Modifier and Type | Field and Description |
---|---|
protected Parameter |
embedding |
protected int |
embeddingSize |
protected AbstractIndexedEmbedding<T> |
fallthroughEmbedding |
protected int |
numEmbeddings |
protected SparseFormat |
sparseFormat |
children, inputNames, inputShapes, parameters, version
Modifier | Constructor and Description |
---|---|
protected |
Embedding(Embedding.BaseBuilder<T,?> baseBuilder) |
|
Embedding(NDArray embedding)
Constructs a pretrained embedding.
|
|
Embedding(NDArray embedding,
SparseFormat format)
Constructs a pretrained embedding.
|
Modifier and Type | Method and Description |
---|---|
NDArray |
embed(NDManager manager,
T[] items)
Embeds an array of items.
|
static NDList |
embedding(NDArray input,
NDArray weight,
SparseFormat sparse)
A simple lookup table that looks up embeddings in a fixed dictionary and size.
|
protected NDList |
forwardInternal(ParameterStore parameterStore,
NDList inputs,
boolean training,
ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
A helper for
Block.forward(ParameterStore, NDList, boolean, PairList) after
initialization. |
Shape[] |
getOutputShapes(Shape[] inputShapes)
Returns the expected output shapes of the block for the specified input shapes.
|
void |
loadParameters(NDManager manager,
java.io.DataInputStream is)
Loads the parameters from the given input stream.
|
void |
prepare(Shape[] inputShapes)
Sets the shape of
Parameter s. |
void |
saveParameters(java.io.DataOutputStream os)
Writes the parameters of the block to the given outputStream.
|
addChildBlock, addParameter, beforeInitialize, cast, clear, describeInput, forward, forward, forwardInternal, getChildren, getDirectParameters, getParameters, initialize, initializeChildBlocks, isInitialized, loadMetadata, readInputShapes, saveInputShapes, saveMetadata, setInitializer, setInitializer, setInitializer, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
decode, embed, encode, unembed
hasItem
forward, validateLayout
protected int numEmbeddings
protected int embeddingSize
protected SparseFormat sparseFormat
protected AbstractIndexedEmbedding<T> fallthroughEmbedding
protected Parameter embedding
protected Embedding(Embedding.BaseBuilder<T,?> baseBuilder)
public Embedding(NDArray embedding)
embedding
- the embedding arraypublic Embedding(NDArray embedding, SparseFormat format)
embedding
- the embedding arrayformat
- whether to compute row sparse gradient in the backward calculationpublic void prepare(Shape[] inputShapes)
Parameter
s.prepare
in class AbstractBlock
inputShapes
- the shapes of inputspublic Shape[] getOutputShapes(Shape[] inputShapes)
getOutputShapes
in interface Block
inputShapes
- the shapes of the inputsprotected NDList forwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
Block.forward(ParameterStore, NDList, boolean, PairList)
after
initialization.forwardInternal
in class AbstractBlock
parameterStore
- the parameter storeinputs
- the input NDListtraining
- true for a training forward passparams
- optional parameterspublic void saveParameters(java.io.DataOutputStream os) throws java.io.IOException
saveParameters
in interface Block
saveParameters
in class AbstractBlock
os
- the outputstream to save the parameters tojava.io.IOException
- if an I/O error occurspublic void loadParameters(NDManager manager, java.io.DataInputStream is) throws java.io.IOException, MalformedModelException
loadParameters
in interface Block
loadParameters
in class AbstractBlock
manager
- an NDManager to create the parameter arraysis
- the inputstream that stream the parameter valuesjava.io.IOException
- if an I/O error occursMalformedModelException
- if the model file is corrupted or unsupportedpublic NDArray embed(NDManager manager, T[] items)
embed
in interface AbstractEmbedding<T>
manager
- the manager for the new embeddingsitems
- the items to embedNDArray
of Shape(items.length, embeddingSize)public static NDList embedding(NDArray input, NDArray weight, SparseFormat sparse)
input
- NDArray containing indices into the embedding matrixweight
- The embedding matrix with number of rows equal to the maximum possible index +
1, and number of columns equal to the embedding sizesparse
- SparseFormat of the gradient