{"record":{"id":"ca5bfcaf09a69a10","repo":"keras-team/keras","slug":"you-must-build-the-layer-before-accessing-kernel","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/convolutional/base_conv.py","lineNumber":230,"sourceCode":"            self.bias = self.add_weight(\n                name=\"bias\",\n                shape=(self.filters,),\n                initializer=self.bias_initializer,\n                regularizer=self.bias_regularizer,\n                constraint=self.bias_constraint,\n                trainable=True,\n                dtype=self.dtype,\n            )\n        else:\n            self.bias = None\n        self.built = True\n        if self.lora_rank:\n            self.enable_lora(self.lora_rank, lora_alpha=self.lora_alpha)\n\n    @property\n    def kernel(self):\n        if not self.built:\n            raise AttributeError(\n                \"You must build the layer before accessing `kernel`.\"\n            )\n        kernel = self._kernel\n        if self.lora_enabled:\n            kernel = ops.cast(\n                ops.add(\n                    kernel,\n                    (self.lora_alpha / self.lora_rank)\n                    * ops.matmul(self.lora_kernel_a, self.lora_kernel_b),\n                ),\n                dtype=self.compute_dtype,\n            )\n        return kernel\n\n    def convolution_op(self, inputs, kernel):\n        return ops.conv(\n            inputs,\n            kernel,","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/convolutional/base_conv.py#L212-L248","documentation":"Attribute guard on the `kernel` property of a base conv layer: weights (including the kernel) only exist after build(), which happens on the first call with a known input shape or an explicit build(input_shape); accessing layer.kernel before that has no tensor to return.","triggerScenarios":"Thrown at keras/src/layers/convolutional/base_conv.py:230 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call the layer on an input or layer.build(input_shape) before reading layer.kernel","Retrieve weights via layer.get_weights() only after building","In functional/subclass models, move kernel access into call() or after the first forward pass"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}