Embedding Layers
DL4J equivalents and API reference for Keras embedding layers — Embedding mapped to EmbeddingSequenceLayer.
Keras Embedding Layer Import
Embedding layer support is implemented in the layers/embeddings package.
Support Summary
Embedding
EmbeddingSequenceLayer
Yes
KerasEmbedding
Imports a Keras Embedding layer as a DL4J EmbeddingSequenceLayer. The embedding matrix (shape [vocab_size, embedding_dim]) is loaded from the HDF5 file.
Constructor (no-arg, for unit tests)
public KerasEmbedding() throws UnsupportedKerasConfigurationExceptionPass-through constructor used in unit tests.
getEmbeddingLayer
public EmbeddingSequenceLayer getEmbeddingLayer()Returns the configured DL4J EmbeddingSequenceLayer. Configuration parameters derived from Keras:
input_dim→ vocabulary sizeoutput_dim→ embedding dimensioninput_length→ sequence length (optional)embeddings_initializer→ weight initializerembeddings_regularizer→ L1/L2 regularization on the embedding matrixmask_zero→ whether to mask the zero index (affects masking in downstream RNN layers)
getOutputType
Returns an InputType.recurrent(embeddingDim) reflecting the sequence output of the embedding layer.
Parameters:
inputType— array of inputInputTypeobjects
Returns: output InputType
getNumParams
Returns 1 (the embedding matrix is a single parameter tensor).
setWeights
Loads the embedding matrix from the weight map. In the HDF5 file, Keras stores this as embeddings_0 or a similar key depending on the version.
Parameters:
weights— map from parameter name toINDArraycontaining the embedding matrix
Keras Embedding Parameters
input_dim
nIn() (vocabulary size)
output_dim
nOut() (embedding dimension)
input_length
sequence length
embeddings_initializer
weight initializer
embeddings_regularizer
weight regularization
mask_zero
masking configuration
trainable
weight update flag
Example
Notes
DL4J's
EmbeddingSequenceLayerhandles sequence inputs natively and is preferred overEmbeddingLayerfor Keras Embedding import because it preserves the temporal dimension for downstream RNN layers.Sparse embedding updates (Keras
embeddings_constraint) are read but DL4J uses dense gradient updates for the embedding matrix.If
mask_zero=Truewas used in Keras, downstream LSTM/SimpleRNN layers will apply masking. Verify that the DL4J model handles masking as expected for your data.
Last updated
Was this helpful?