Answers.Body: <p>Aha, I understand now! In the paper, the diagram for SHA has its inputs in the order Q, K, V, while the diagram for MHS has its inputs in the order V, K, Q! In the grand diagram for the entire net, I thought the arrows for the inputs were in the order for SHA, but they are actually in the order for MHA. It is a bit confusing that when changing to the MHA diagram, they decided to swap the order of inputs in the paper, but it all makes sense now. output_seq x d_q multiplies d_q x input_seq to get output_seq x input_seq, which finally multiplies with input_seq x d_v to get output_seq x d_v, which then concatenates to output_seq x hd_v, so after the final linear layer it becomes output_seq x d_model, the standard in the decoder layer. Hopefully, anyone else who mixed up orders from the diagram will understand now.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-24T23:45:30.447Z
Answers.LastActivityDate: 2021-02-24T23:45:30.447Z
Answers.UserDisplayName: Alien
Body: <p>In <a href="https://www.tensorflow.org/tutorials/text/transformer" rel="nofollow noreferrer">this</a> tensorflow article, the comments in the code say that MHA should output with one of the dimensions being the sequence length of the query/key. However, that means that the second MHA in the decoder layer should output something with one of the dimensions being the input sequence length, but clearly it should actually be the output sequence length! From all that I have read on transformers, it seems that the output of the left side of the SHA should be a matrix with dimensions q_seq_length x q_seq_length, and the output of the right side of the SHA should be v_seq_length x d_model. These matrices can't even multiply when using the second MHA in the decoder to incorporate the encoder output! Please help. I would appreciate a clear-cut explanation. Thanks</p>
  
Tags: <transformer><attention><encoder-decoder>
Title: How is the transformers' output matrix size arrived at?
UserDisplayName: Alien
ThreadText: # How is the transformers' output matrix size arrived at?
  
  (Asked by: username_0 on 2021-02-24)
  
  In [this](https://www.tensorflow.org/tutorials/text/transformer) tensorflow article, the comments in the code say that MHA should output with one of the dimensions being the sequence length of the query/key. However, that means that the second MHA in the decoder layer should output something with one of the dimensions being the input sequence length, but clearly it should actually be the output sequence length! From all that I have read on transformers, it seems that the output of the left side of the SHA should be a matrix with dimensions q\_seq\_length x q\_seq\_length, and the output of the right side of the SHA should be v\_seq\_length x d\_model. These matrices can't even multiply when using the second MHA in the decoder to incorporate the encoder output! Please help. I would appreciate a clear-cut explanation. Thanks
  
  **Tags:** transformer, attention, encoder-decoder
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-02-24. Score: 0 | Accepted answer
  
  Aha, I understand now! In the paper, the diagram for SHA has its inputs in the order Q, K, V, while the diagram for MHS has its inputs in the order V, K, Q! In the grand diagram for the entire net, I thought the arrows for the inputs were in the order for SHA, but they are actually in the order for MHA. It is a bit confusing that when changing to the MHA diagram, they decided to swap the order of inputs in the paper, but it all makes sense now. output\_seq x d\_q multiplies d\_q x input\_seq to get output\_seq x input\_seq, which finally multiplies with input\_seq x d\_v to get output\_seq x d\_v, which then concatenates to output\_seq x hd\_v, so after the final linear layer it becomes output\_seq x d\_model, the standard in the decoder layer. Hopefully, anyone else who mixed up orders from the diagram will understand now.
  
  
  ---
  
  