{"record":{"id":"6bcc2e546b0dc7c3","repo":"tensorflow/models","slug":"transformerlayer-expects-a-three-dimensional-input-6bcc2e","errorCode":null,"errorMessage":"TransformerLayer expects a three-dimensional input of shape [batch, sequence, width].","messagePattern":"TransformerLayer expects a three-dimensional input of shape \\[batch, sequence, width\\]\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/nlp/modeling/layers/transformer.py","lineNumber":257,"sourceCode":"          self._kernel_initializer)\n\n    self._self_attention_cls = self_attention_cls or attention.CachedAttention\n\n    if cross_attention_cls is not None:\n      self._cross_attention_cls = cross_attention_cls\n      if self.multi_channel_cross_attention:\n        logging.warning(\n            \"%s will be used for cross attention\", cross_attention_cls\n        )\n    elif self.multi_channel_cross_attention:\n      self._cross_attention_cls = multi_channel_attention.MultiChannelAttention\n    else:\n      self._cross_attention_cls = attention.MultiHeadAttention\n\n  def build(self, input_shape):\n    target_tensor_shape = tf.TensorShape(input_shape[0])\n    if len(target_tensor_shape.as_list()) != 3:\n      raise ValueError(\"TransformerLayer expects a three-dimensional input of \"\n                       \"shape [batch, sequence, width].\")\n    hidden_size = target_tensor_shape[2]\n    if hidden_size % self.num_attention_heads != 0:\n      raise ValueError(\n          \"The hidden size (%d) is not a multiple of the number of attention \"\n          \"heads (%d)\" % (hidden_size, self.num_attention_heads))\n    self.attention_head_size = int(hidden_size) // self.num_attention_heads\n    common_kwargs = dict(\n        kernel_regularizer=self._kernel_regularizer,\n        bias_regularizer=self._bias_regularizer,\n        activity_regularizer=self._activity_regularizer,\n        kernel_constraint=self._kernel_constraint,\n        bias_constraint=self._bias_constraint)\n    # Self attention.\n    self.self_attention = self._self_attention_cls(\n        num_heads=self.num_attention_heads,\n        key_dim=self.attention_head_size,\n        dropout=self.attention_dropout_rate,","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/nlp/modeling/layers/transformer.py#L239-L275","documentation":"Error \"TransformerLayer expects a three-dimensional input of shape [batch, sequence, width].\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/nlp/modeling/layers/transformer.py:257 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}