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
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.
Source
Thrown at keras/src/layers/core/dense.py:168
name="bias",
shape=(self.units,),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
)
else:
self.bias = None
self.input_spec = InputSpec(min_ndim=2, axes={-1: input_shape[-1]})
self.built = True
if self.lora_rank:
self.enable_lora(self.lora_rank)
@property
def kernel(self):
from keras.src.quantizers import gptq_core
if not self.built:
raise AttributeError(
"You must build the layer before accessing `kernel`."
)
mode = self.quantization_mode
is_gptq = mode == "gptq"
is_awq = mode == "awq"
is_int4 = mode == "int4"
gptq_calibrated = bool(getattr(self, "is_gptq_calibrated", False))
awq_calibrated = bool(getattr(self, "is_awq_calibrated", False))
gptq_bits = (
gptq_core.get_weight_bits_for_layer(self, None) if is_gptq else None
)
# Decide the source tensor first (packed vs already-quantized vs plain
# kernel)
if mode == "ternary":
# Ternary: unpack to int8 {-1, 0, +1} float view.
return quantizers.unpack_ternary(View on GitHub (pinned to 7a34a03db6)
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.
Example fix
# before dense = keras.layers.Dense(10) print(dense.kernel.shape) # AttributeError # after dense = keras.layers.Dense(10) dense.build(input_shape=(None, 32)) print(dense.kernel.shape)
Defensive patterns
Strategy: validation
Validate before calling
if not dense.built:
dense.build(input_shape)
k = dense.kernel Type guard
def safe_kernel(layer):
return layer.kernel if layer.built else None Prevention
- Always build (or run a forward pass) before touching weights
- Use model.build(input_shape) in weight-inspection utilities
When it happens
Trigger: Accessing dense.kernel (or dense.kernel.shape) before calling the layer on data or before layer.build(input_shape) / model.build(input_shape).
Common situations: Inspecting or initializing kernel shape right after construction; custom weight-loading code reading .kernel before build; probing functional models before inputs are known.
Related errors
- You must build the layer before accessing `kernel`.
- Received an invalid value for `units`, expected a positive i
- Cannot enable lora on a layer that isn't yet built.
- Sequential model '{self.name}' has no defined input shape ye
- Sequential model '{self.name}' has no defined output shape y
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/1b255f8e00910995.
Report an issue: GitHub.