RuntimeError: mat1 and mat2 shapes cannot be multiplied (984x1 and 4x512)

def forward(self,src:Tensor,tgt:Tensor,src_mask:Tensor=None,tgt_mask:Tensor=None) -> Tensor:
        src = self.encoder_input_layer(src)
        src = self.positional_encoding_layer(src)
        src = self.encoder(src)
        decoder_output = self.decoder_input_layer(tgt)
        decoder_output = self.decoder(
            tgt = decoder_output,
            memory = src,
            tgt_mask = tgt_mask,
            memory_mask = src_mask
        decoder_output = self.linear_mapping_layer(decoder_output)
        return decoder_output

I have been getting dimensionality error from quite a long time and this is my forward method for the model.

It’s unclear which layer raises the shape mismatch error but based on the error message it should come from a linear layer. You would have to check the stacktrace and isolate which layer fails. Once this is done make sure the input activation has the expected number of features or adapt the linear layer and change its in_features attribute to the feature dimension of the input activation.