Solved by DogeWatch in post #9 
              
              
                my usage is wrong, I should let src = torch.randn(4,1,16), because the nn.TransformerEncoder’s input shape is (seq_len, batch_size, embedding_dim)