{"record":{"id":"cfb7b1d8c4edb0da","repo":"keras-team/keras","slug":"unexpected-bias-dimensions-len-bias-shape-expe","errorCode":null,"errorMessage":"Unexpected bias dimensions {len(bias_shape)}. Expected it to be 1 or {ndim(x) - 1} dimensions","messagePattern":"Unexpected bias dimensions (.+?)\\. Expected it to be 1 or (.+?) dimensions","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/backend.py","lineNumber":246,"sourceCode":"            x.assign(value)\n\n\n@keras_export(\"keras._legacy.backend.batch_normalization\")\ndef batch_normalization(x, mean, var, beta, gamma, axis=-1, epsilon=1e-3):\n    \"\"\"DEPRECATED.\"\"\"\n    return tf.nn.batch_normalization(x, mean, var, beta, gamma, epsilon)\n\n\n@keras_export(\"keras._legacy.backend.bias_add\")\ndef bias_add(x, bias, data_format=None):\n    \"\"\"DEPRECATED.\"\"\"\n    if data_format is None:\n        data_format = backend.image_data_format()\n    if data_format not in {\"channels_first\", \"channels_last\"}:\n        raise ValueError(f\"Unknown data_format: {data_format}\")\n    bias_shape = bias.shape\n    if len(bias_shape) != 1 and len(bias_shape) != ndim(x) - 1:\n        raise ValueError(\n            f\"Unexpected bias dimensions {len(bias_shape)}. \"\n            f\"Expected it to be 1 or {ndim(x) - 1} dimensions\"\n        )\n\n    if len(bias_shape) == 1:\n        if data_format == \"channels_first\":\n            return tf.nn.bias_add(x, bias, data_format=\"NCHW\")\n        return tf.nn.bias_add(x, bias, data_format=\"NHWC\")\n    if ndim(x) in (3, 4, 5):\n        if data_format == \"channels_first\":\n            bias_reshape_axis = (1, bias_shape[-1]) + bias_shape[:-1]\n            return x + reshape(bias, bias_reshape_axis)\n        return x + reshape(bias, (1,) + bias_shape)\n    return tf.nn.bias_add(x, bias)\n\n\n@keras_export(\"keras._legacy.backend.binary_crossentropy\")\ndef binary_crossentropy(target, output, from_logits=False):","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/backend.py#L228-L264","documentation":"bias_add() requires the bias tensor to be rank 1 (one scalar per channel) or exactly ndim(x)-1 (one bias per non-batch dimension, the Convolution2DFlipout-style full bias case). Any other rank is rejected because there is no unambiguous way to broadcast it against x under the chosen data_format.","triggerScenarios":"bias_add(x, bias) where bias.shape has rank >= 2 and != ndim(x)-1 — e.g. x of rank 4 (batch of images) with a rank-2 bias (16, 3) instead of rank-1 (3,) or rank-3 (h, w, 3).","commonSituations":"Flattening or reshaping a per-channel bias vector into 2D; passing a kernel's weights instead of the bias vector; custom conv layers where x gained/lost a batch axis before bias_add is called.","solutions":["Reshape the bias to rank 1: bias = keras.ops.reshape(bias, (-1,)) when it is one value per channel","Or reshape to rank ndim(x)-1 matching x's non-batch dims if you genuinely need spatial biases","Check that x actually has its batch dimension (expand_dims) and that you passed bias, not the kernel"],"exampleFix":"// before\nout = K.bias_add(x, bias)  # bias.shape=(1, 3), x rank 4 -> ValueError\n\n// after\nout = K.bias_add(x, keras.ops.reshape(bias, (-1,)))  # bias.shape=(3,)","handlingStrategy":"validation","validationCode":"assert len(bias.shape) == 1 or len(bias.shape) == len(x.shape) - 1, f'bias rank {len(bias.shape)} invalid for x rank {len(x.shape)}'","typeGuard":"def valid_bias(x, bias) -> bool:\n    r = len(bias.shape)\n    return r == 1 or r == len(x.shape) - 1","tryCatchPattern":"except ValueError as e:\n    if 'bias dimensions' in str(e):\n        out = K.bias_add(x, keras.ops.reshape(bias, (-1,)), data_format=fmt)\n    else:\n        raise","preventionTips":["Flatten biases to rank 1 at layer-construction time","Log bias.shape next to ndim(x) in failing layers","Guard against dropping/adding a batch axis before bias_add"],"tags":["keras","tensorflow","bias-add","rank-mismatch"],"backgroundTag":"invalid-tensor-shape","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}