keras-team/keras · error · AttributeError
You must build the layer before accessing `embeddings`.
Error message
You must build the layer before accessing `embeddings`.
What it means
Embedding.embeddings is a property returning the embedding weight matrix, which only exists after the layer has been built (weights allocated on first call or explicit build). Accessing it before build raises AttributeError. With int4 quantization it additionally unpacks stored weights, which also presumes built state.
Source
Thrown at keras/src/layers/core/embedding.py:172
config=self.quantization_config,
)
if self.quantization_mode not in ("int8", "int4"):
self._embeddings = self.add_weight(
shape=embeddings_shape,
initializer=self.embeddings_initializer,
name="embeddings",
regularizer=self.embeddings_regularizer,
constraint=self.embeddings_constraint,
trainable=True,
)
self.built = True
if self.lora_rank:
self.enable_lora(self.lora_rank)
@property
def embeddings(self):
if not self.built:
raise AttributeError(
"You must build the layer before accessing `embeddings`."
)
embeddings = self._embeddings
if self.quantization_mode == "int4":
embeddings = quantizers.unpack_int4(
embeddings, self._orig_output_dim, axis=-1
)
if self.lora_enabled:
embeddings = ops.cast(
ops.add(
embeddings,
(self.lora_alpha / self.lora_rank)
* ops.matmul(
self.lora_embeddings_a, self.lora_embeddings_b
),
),
dtype=self.compute_dtype,
)View on GitHub (pinned to 7a34a03db6)
Solutions
- Call the layer once on dummy input or call layer.build(input_shape) (or model.build(...)) before accessing .embeddings
- In tests, run layer(keras.ops.zeros((1,), dtype='int32')) first
- If you need pre-build weight access, construct weights yourself and assign them after build
Example fix
# before layer = keras.layers.Embedding(input_dim=100, output_dim=32) w = layer.embeddings # AttributeError # after layer = keras.layers.Embedding(input_dim=100, output_dim=32) layer.build((None,)) w = layer.embeddings
Defensive patterns
Strategy: validation
Validate before calling
if not layer.built:
layer.build((None,))
# or: layer(keras.ops.zeros((1,), dtype='int32')) Type guard
def embeddings_accessible(layer):
return getattr(layer, 'built', False) Try / catch
try:
w = layer.embeddings
except AttributeError:
layer.build((None,))
w = layer.embeddings Prevention
- Always build layers (or run one forward pass) before touching weights
- In tests, use a dummy input call before asserting on weights
When it happens
Trigger: Reading layer.embeddings on a freshly constructed, never-called Embedding layer; accessing embeddings after only setting input_dim/output_dim; a model loaded from config but not yet called with data before property access.
Common situations: Inspecting or initializing embeddings right after construction; serialization code touching weights before a forward pass; unit tests that read .embeddings without a dummy call.
Related errors
- `input_dim` must be a positive integer. Received: input_dim=
- `output_dim` must be a positive integer. Received: output_di
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/10330a4fe7bbbbd5.
Report an issue: GitHub.