Dimension error for transformer model

chesscoder: <p>I’m trying to use a transformer model (based heavily on the pytorch tutorial code, which I hope to adapt to my own problem) on the same chess data I was using before (which I was able to debug). However I am getting a dimension error.</p>
<p>class TransformerModel(nn.Module):</p>
<pre><code>def __init__(self, ntoken: int, d_model: int, nhead: int, d_hid: int,
             nlayers: int, dropout: float = 0.5):
    super().__init__()
    self.model_type = 'Transformer'
    self.pos_encoder = PositionalEncoding(d_model, dropout)
    encoder_layers = TransformerEncoderLayer(d_model, nhead, d_hid, dropout)
    self.transformer_encoder = TransformerEncoder(encoder_layers, nlayers)
    self.encoder = nn.Embedding(ntoken, d_model)
    self.d_model = d_model
    self.decoder = nn.Linear(d_model, 1)

    self.init_weights()

def init_weights(self) -&gt; None:
    initrange = 0.1
    self.encoder.weight.data.uniform_(-initrange, initrange)
    self.decoder.bias.data.zero_()
    self.decoder.weight.data.uniform_(-initrange, initrange)

def forward(self, src: Tensor, src_mask: Tensor) -&gt; Tensor:
    """
    Args:
        src: Tensor, shape [seq_len, batch_size]
        src_mask: Tensor, shape [seq_len, seq_len]

    Returns:
        output Tensor of shape [seq_len, batch_size, ntoken]
    """
    src = self.encoder(src) * math.sqrt(self.d_model)
    src = self.pos_encoder(src)
    output = self.transformer_encoder(src, src_mask)
    output = self.decoder(output)
    return output
</code></pre>
<p>def generate_square_subsequent_mask(sz: int) → Tensor:<br>
“”“Generates an upper-triangular matrix of -inf, with zeros on diag.”“”<br>
return torch.triu(torch.ones(sz, sz) * float(‘-inf’), diagonal=1)</p>
<p>import torch.optim as optim</p>
<h1>
<a name="instantiate-the-adam-optimizer-and-cross-entropy-loss-function-1" class="anchor" href="#instantiate-the-adam-optimizer-and-cross-entropy-loss-function-1"></a>Instantiate the Adam optimizer and Cross-Entropy loss function</h1>
<p>model = TransformerModel(ntoken= 64*12+6, d_model= 200, nhead= 2, d_hid=200,<br>
nlayers= 6, dropout= 0.2).cuda()</p>
<p>Here is the error<br>
/home/ec2-user/.local/lib/python3.7/site-packages/torch/nn/modules/loss.py:536: UserWarning: Using a target size (torch.Size([32, 1])) that is different to the input size (torch.Size([32, 774, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.<br>
return F.mse_loss(input, target, reduction=self.reduction)<br>
Traceback (most recent call last):<br>
File “transformernneval.py”, line 112, in <br>
loss = criterion(output, target)<br>
File “/home/ec2-user/.local/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 1194, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “/home/ec2-user/.local/lib/python3.7/site-packages/torch/nn/modules/loss.py”, line 536, in forward<br>
return F.mse_loss(input, target, reduction=self.reduction)<br>
File “/home/ec2-user/.local/lib/python3.7/site-packages/torch/nn/functional.py”, line 3291, in mse_loss<br>
expanded_input, expanded_target = torch.broadcast_tensors(input, target)<br>
File “/home/ec2-user/.local/lib/python3.7/site-packages/torch/functional.py”, line 74, in broadcast_tensors<br>
return _VF.broadcast_tensors(tensors)  # type: ignore[attr-defined]<br>
RuntimeError: The size of tensor a (774) must match the size of tensor b (32) at non-singleton dimension 1</p>