Trouble with TensorFlow 2.12 Multi-Head Attention Layer: Unexpected Output Shapes
I'm following best practices but I'm working on a sequence-to-sequence model using TensorFlow 2.12, and I'm working with issues with the output shapes when using the Multi-Head Attention layer... My model is structured to take input sequences of shape `(batch_size, seq_length, embedding_dim)`, where `embedding_dim` is 64. However, when I implement the attention layer, the output shape seems to be incorrect after the layer processes the input. Here's a snippet of my code: ```python import tensorflow as tf from tensorflow.keras.layers import MultiHeadAttention class MyModel(tf.keras.Model): def __init__(self): super(MyModel, self).__init__() self.attention = MultiHeadAttention(num_heads=4, key_dim=64) def call(self, inputs): output = self.attention(inputs, inputs) return output model = MyModel() input_data = tf.random.normal((32, 10, 64)) # 32 samples, 10 timesteps, 64 embedding dim output_data = model(input_data) print(output_data.shape) ``` I expected the output shape to be `(32, 10, 64)` as well, but instead, I'm getting `(32, 10, 256)`. I know this is because the attention layer concatenates the outputs of the heads, but I'm not sure how to adjust the model to get the desired output shape without modifying the heads. Iβve tried setting the `output_shape` parameter in the layer, but it doesnβt seem to have any effect. Is there a way to adjust the Multi-Head Attention layer or process the output to achieve the expected shape? Any help would be appreciated! Any pointers in the right direction? Thanks, I really appreciate it!