{"record":{"id":"1b255f8e00910995","repo":"keras-team/keras","slug":"you-must-build-the-layer-before-accessing-kernel-1b255f","errorCode":null,"errorMessage":"You must build the layer before accessing `kernel`.","messagePattern":"You must build the layer before accessing `kernel`\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/dense.py","lineNumber":168,"sourceCode":"                name=\"bias\",\n                shape=(self.units,),\n                initializer=self.bias_initializer,\n                regularizer=self.bias_regularizer,\n                constraint=self.bias_constraint,\n            )\n        else:\n            self.bias = None\n        self.input_spec = InputSpec(min_ndim=2, axes={-1: input_shape[-1]})\n        self.built = True\n        if self.lora_rank:\n            self.enable_lora(self.lora_rank)\n\n    @property\n    def kernel(self):\n        from keras.src.quantizers import gptq_core\n\n        if not self.built:\n            raise AttributeError(\n                \"You must build the layer before accessing `kernel`.\"\n            )\n\n        mode = self.quantization_mode\n        is_gptq = mode == \"gptq\"\n        is_awq = mode == \"awq\"\n        is_int4 = mode == \"int4\"\n        gptq_calibrated = bool(getattr(self, \"is_gptq_calibrated\", False))\n        awq_calibrated = bool(getattr(self, \"is_awq_calibrated\", False))\n        gptq_bits = (\n            gptq_core.get_weight_bits_for_layer(self, None) if is_gptq else None\n        )\n\n        # Decide the source tensor first (packed vs already-quantized vs plain\n        # kernel)\n        if mode == \"ternary\":\n            # Ternary: unpack to int8 {-1, 0, +1} float view.\n            return quantizers.unpack_ternary(","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/dense.py#L150-L186","documentation":"Dense.kernel is a property over the weight variable, which only exists after build() runs (weights are created lazily from the input shape). Accessing .kernel on an unbuilt layer raises AttributeError because no kernel variable exists yet.","triggerScenarios":"Accessing dense.kernel (or dense.kernel.shape) before calling the layer on data or before layer.build(input_shape) / model.build(input_shape).","commonSituations":"Inspecting or initializing kernel shape right after construction; custom weight-loading code reading .kernel before build; probing functional models before inputs are known.","solutions":["Build first: layer.build(input_shape) or call model(x) once, then access .kernel.","Use model.build(input_shape) for the whole model when no data is handy.","For shape logic, use layer.compute_output_shape() instead of reading the kernel."],"exampleFix":"# before\ndense = keras.layers.Dense(10)\nprint(dense.kernel.shape)  # AttributeError\n\n# after\ndense = keras.layers.Dense(10)\ndense.build(input_shape=(None, 32))\nprint(dense.kernel.shape)","handlingStrategy":"validation","validationCode":"if not dense.built:\n    dense.build(input_shape)\nk = dense.kernel","typeGuard":"def safe_kernel(layer):\n    return layer.kernel if layer.built else None","tryCatchPattern":null,"preventionTips":["Always build (or run a forward pass) before touching weights","Use model.build(input_shape) in weight-inspection utilities"],"tags":["keras","dense","lazy-build","attribute-error"],"backgroundTag":"keras-layer-not-built-yet","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}