{"record":{"id":"10330a4fe7bbbbd5","repo":"keras-team/keras","slug":"you-must-build-the-layer-before-accessing-embeddi","errorCode":null,"errorMessage":"You must build the layer before accessing `embeddings`.","messagePattern":"You must build the layer before accessing `embeddings`\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/embedding.py","lineNumber":172,"sourceCode":"                config=self.quantization_config,\n            )\n        if self.quantization_mode not in (\"int8\", \"int4\"):\n            self._embeddings = self.add_weight(\n                shape=embeddings_shape,\n                initializer=self.embeddings_initializer,\n                name=\"embeddings\",\n                regularizer=self.embeddings_regularizer,\n                constraint=self.embeddings_constraint,\n                trainable=True,\n            )\n        self.built = True\n        if self.lora_rank:\n            self.enable_lora(self.lora_rank)\n\n    @property\n    def embeddings(self):\n        if not self.built:\n            raise AttributeError(\n                \"You must build the layer before accessing `embeddings`.\"\n            )\n        embeddings = self._embeddings\n        if self.quantization_mode == \"int4\":\n            embeddings = quantizers.unpack_int4(\n                embeddings, self._orig_output_dim, axis=-1\n            )\n        if self.lora_enabled:\n            embeddings = ops.cast(\n                ops.add(\n                    embeddings,\n                    (self.lora_alpha / self.lora_rank)\n                    * ops.matmul(\n                        self.lora_embeddings_a, self.lora_embeddings_b\n                    ),\n                ),\n                dtype=self.compute_dtype,\n            )","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/embedding.py#L154-L190","documentation":"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.","triggerScenarios":"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.","commonSituations":"Inspecting or initializing embeddings right after construction; serialization code touching weights before a forward pass; unit tests that read .embeddings without a dummy call.","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"],"exampleFix":"# before\nlayer = keras.layers.Embedding(input_dim=100, output_dim=32)\nw = layer.embeddings  # AttributeError\n# after\nlayer = keras.layers.Embedding(input_dim=100, output_dim=32)\nlayer.build((None,))\nw = layer.embeddings","handlingStrategy":"validation","validationCode":"if not layer.built:\n    layer.build((None,))\n# or: layer(keras.ops.zeros((1,), dtype='int32'))","typeGuard":"def embeddings_accessible(layer):\n    return getattr(layer, 'built', False)","tryCatchPattern":"try:\n    w = layer.embeddings\nexcept AttributeError:\n    layer.build((None,))\n    w = layer.embeddings","preventionTips":["Always build layers (or run one forward pass) before touching weights","In tests, use a dummy input call before asserting on weights"],"tags":["keras","embedding","build-state","attribute-access"],"backgroundTag":"layer-not-built","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}