{"record":{"id":"3a90328f0eb4f913","repo":"keras-team/keras","slug":"cannot-do-batch-dot-on-inputs-with-rank-2-recei","errorCode":null,"errorMessage":"Cannot do batch_dot on inputs with rank < 2. Received inputs with shapes {x_shape} and {y_shape}.","messagePattern":"Cannot do batch_dot on inputs with rank < 2\\. Received inputs with shapes (.+?) and (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/merging/dot.py","lineNumber":60,"sourceCode":"            integer. The sizes of `x.shape[axes[0]]` and `y.shape[axes[1]]`\n            should be equal.\n            Note that axis `0` (the batch axis) cannot be included.\n\n    Returns:\n        A tensor with shape equal to the concatenation of `x`'s shape\n        (less the dimension that was summed over) and `y`'s shape (less the\n        batch dimension and the dimension that was summed over). If the final\n        rank is 1, we reshape it to `(batch_size, 1)`.\n    \"\"\"\n\n    x_shape = x.shape\n    y_shape = y.shape\n\n    x_ndim = len(x_shape)\n    y_ndim = len(y_shape)\n\n    if x_ndim < 2 or y_ndim < 2:\n        raise ValueError(\n            f\"Cannot do batch_dot on inputs \"\n            f\"with rank < 2. \"\n            f\"Received inputs with shapes \"\n            f\"{x_shape} and {y_shape}.\"\n        )\n\n    x_batch_size = x_shape[0]\n    y_batch_size = y_shape[0]\n\n    if x_batch_size is not None and y_batch_size is not None:\n        if x_batch_size != y_batch_size:\n            raise ValueError(\n                f\"Cannot do batch_dot on inputs \"\n                f\"with different batch sizes. \"\n                f\"Received inputs with shapes \"\n                f\"{x_shape} and {y_shape}.\"\n            )\n    if isinstance(axes, int):","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/merging/dot.py#L42-L78","documentation":"The batch_dot operation (used by Keras's Dot merge layer) requires both operands to have rank >= 2 (a batch dim plus at least one feature dim). This error fires in Dot._merge_function -> batch_dot when either input is a 1-D vector.","triggerScenarios":"Calling layers.Dot(axes=1)([x, y]) where x or y is shape (None,) or (batch_size,); using batched products on plain vectors.","commonSituations":"Dotting raw embedding outputs without a time/channel axis; mixing up Dot with inner products on flattened vectors; inputs unexpectedly squeezed upstream.","solutions":["Expand vectors to rank 2 before the Dot: x = keras.ops.expand_dims(x, -1)","Use keras.ops.sum(x * y, axis=-1) or a Dense(1) for vector inner products instead of Dot","Check upstream layers (squeeze, reduce) that might drop a dimension"],"exampleFix":"# before\nout = layers.Dot(axes=-1)([x, y])  # x shape (None,) -> ValueError\n\n# after\nx = layers.Reshape((1,))(x)  # or expand_dims to rank 2\nout = layers.Dot(axes=-1)([x, y])","handlingStrategy":"validation","validationCode":"assert len(x.shape) >= 2 and len(y.shape) >= 2, 'batch_dot needs rank >= 2'","typeGuard":"def batch_dot_ok(x, y) -> bool:\n    return len(x.shape) >= 2 and len(y.shape) >= 2","tryCatchPattern":null,"preventionTips":["Check ndim / len(shape) before Dot layers","Reshape or expand_dims vectors to rank 2 first","Use element-wise multiply plus sum for vector inner products"],"tags":["keras","dot","batch-dot","rank-mismatch"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}