The size of the vector representation of an inference with a transformer model after going through a transformer layer, before going into the next transformer layer, depends on the number of attention heads and the embedding dimension.
Assuming the transformer model has n attention heads and an embedding dimension of d, the size of the vector representation after going through a transformer layer can be calculated as follows:
- The input sequence of length
L is first embedded into a vector of size L \* d, where each element in the input sequence is represented by a fixed-size embedding.
- The embedded input is then passed through a multi-head self-attention mechanism, which applies attention to each element in the input sequence relative to every other element. The output of the self-attention mechanism is a vector of size
L \* (n \* d), where each element in the vector represents the attention weight for each element in the input sequence.
- The output of the self-attention mechanism is then passed through a feed-forward neural network (FFNN) of size
L \* (n \* d) \* k, where k is the number of layers in the FFNN. The FFNN applies a non-linear transformation to the output of the self-attention mechanism, which increases the dimensionality of the representation.
- The output of the FFNN is then passed through a final linear layer of size
L \* d, which produces the output representation of the transformer layer.
Therefore, the size of the vector representation of an inference with a transformer model after going through a transformer layer is L \* (n \* d) \* k, where n is the number of attention heads, d is the embedding dimension, and k is the number of layers in the FFNN.
After going through the final softmax to make a prediction, the size of the representation is reduced by the number of classes in the classification task. For example, if the model is trained for a binary classification task with two classes, the size of the representation after the final softmax is L \* (n \* d) \* k / 2, where n is the number of attention heads, d is the embedding dimension, and k is the number of layers in the FFNN.
It's worth noting that the size of the representation can vary depending on the specific transformer architecture and the input sequence. The size of the representation can also be adjusted by adjusting the number of attention heads, the embedding dimension, and the number of layers in the FFNN.