T - the type of item that should be embedded and map to the arraypublic abstract class Embedding<T> extends ParameterBlock implements AbstractIndexedEmbedding<T>
NDArrays.| 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 DataType |
dataType |
protected java.util.Map<T,java.lang.Integer> |
embedder |
protected Parameter |
embedding |
protected int |
embeddingSize |
protected AbstractIndexedEmbedding<T> |
fallthroughEmbedding |
protected int |
numItems |
protected boolean |
sparseGrad |
protected java.util.Map<java.lang.Integer,T> |
unembedder |
inputNames, inputShapes| Modifier | Constructor and Description |
|---|---|
protected |
Embedding(Embedding.BaseBuilder<T,?> baseBuilder) |
|
Embedding(NDArray embedding,
java.util.List<T> items)
Constructs a pretrained embedding.
|
|
Embedding(NDArray embedding,
java.util.List<T> items,
boolean sparseGrad)
Constructs a pretrained embedding.
|
| Modifier and Type | Method and Description |
|---|---|
NDArray |
embed(NDManager manager,
T[] items)
Embeds an array of items.
|
int |
embed(T item)
Embeds an item.
|
NDList |
forward(ParameterStore parameterStore,
NDList inputs,
boolean training,
ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
Applies the operating function of the block once.
|
java.util.List<Parameter> |
getDirectParameters()
Returns a list of all the direct parameters of the block.
|
Shape[] |
getOutputShapes(NDManager manager,
Shape[] inputShapes)
Returns the expected output shapes of the block for the specified input shapes.
|
Shape |
getParameterShape(java.lang.String name,
Shape[] inputShapes)
Returns the shape of the specified direct parameter of this block given the shapes of the
input to the block.
|
boolean |
hasItem(T item)
Returns whether an item is in the embedding.
|
void |
loadParameters(NDManager manager,
java.io.DataInputStream is)
Loads the parameters from the given input stream.
|
void |
saveParameters(java.io.DataOutputStream os)
Writes the parameters of the block to the given outputStream.
|
java.util.Optional<T> |
unembed(int index)
Returns the item corresponding to the given index.
|
getChildren, initialize, toStringbeforeInitialize, cast, clear, describeInput, getParameters, isInitialized, readInputShapes, saveInputShapes, setInitializer, setInitializerclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitdecode, encodeforward, validateLayoutprotected int embeddingSize
protected boolean sparseGrad
protected DataType dataType
protected java.util.Map<T,java.lang.Integer> embedder
protected java.util.Map<java.lang.Integer,T> unembedder
protected int numItems
protected AbstractIndexedEmbedding<T> fallthroughEmbedding
protected Parameter embedding
protected Embedding(Embedding.BaseBuilder<T,?> baseBuilder)
public Embedding(NDArray embedding, java.util.List<T> items)
embedding - the embedding arrayitems - the items in the embedding (in matching order to the embedding array)public Embedding(NDArray embedding, java.util.List<T> items, boolean sparseGrad)
embedding - the embedding arrayitems - the items in the embedding (in matching order to the embedding array)sparseGrad - whether to compute row sparse gradient in the backward calculationpublic Shape[] getOutputShapes(NDManager manager, Shape[] inputShapes)
getOutputShapes in interface Blockmanager - an NDManagerinputShapes - the shapes of the inputspublic java.util.List<Parameter> getDirectParameters()
getDirectParameters in interface BlockParameterpublic Shape getParameterShape(java.lang.String name, Shape[] inputShapes)
getParameterShape in interface Blockname - the name of the parameterinputShapes - the shapes of the input to the blockpublic NDList forward(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
public void saveParameters(java.io.DataOutputStream os)
throws java.io.IOException
saveParameters in interface Blockos - 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 Blockmanager - 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 boolean hasItem(T item)
hasItem in interface AbstractEmbedding<T>item - the item to testpublic 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 int embed(T item)
embed in interface AbstractIndexedEmbedding<T>item - the item to embedNDArray of Shape()public java.util.Optional<T> unembed(int index)
unembed in interface AbstractIndexedEmbedding<T>index - the index