keras-team/keras · error · AttributeError
You must build the layer before accessing `kernel`.
Error message
You must build the layer before accessing `kernel`.
What it means
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.
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
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/layers/convolutional/base_conv.py:230 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/ca5bfcaf09a69a10.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/convolutional/base_conv.py:230
self.bias = self.add_weight(
name="bias",
shape=(self.filters,),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
trainable=True,
dtype=self.dtype,
)
else:
self.bias = None
self.built = True
if self.lora_rank:
self.enable_lora(self.lora_rank, lora_alpha=self.lora_alpha)
@property
def kernel(self):
if not self.built:
raise AttributeError(
"You must build the layer before accessing `kernel`."
)
kernel = self._kernel
if self.lora_enabled:
kernel = ops.cast(
ops.add(
kernel,
(self.lora_alpha / self.lora_rank)
* ops.matmul(self.lora_kernel_a, self.lora_kernel_b),
),
dtype=self.compute_dtype,
)
return kernel
def convolution_op(self, inputs, kernel):
return ops.conv(
inputs,
kernel,View on GitHub (pinned to 7a34a03db6)