Tensor size mismatch autoencoder

        self.encoder = nn.Sequential(nn.Conv1d(1, 16, kernel_size=51, stride=10, bias=False, padding=25),
                                     nn.PReLU(),
                                     nn.Conv1d(16, 32, kernel_size=11, stride=3,  bias=False, padding=5),
                                     nn.PReLU(),
                                     )
        self.decoder = nn.Sequential(
                                     nn.ConvTranspose1d(32, 16, 11, stride=3, bias=False, padding=5),
                                     nn.PReLU(),
                                     nn.ConvTranspose1d(16, 1, 51, stride=10, bias=False, padding=25),
                                     nn.PReLU())

I think this should match well, however I get this error:
RuntimeError: The size of tensor a (2971) must match the size of tensor b (3000) at non-singleton dimension 2

Any suggestions please?

Which line of code is throwing this issue?
Could you post an executable code snippet, so that we could have a look?

class Autoencoder2(nn.Module):
    def __init__(self):
        super(Autoencoder2, self).__init__()

        self.encoder = nn.Sequential(nn.Conv1d(1, 16, kernel_size=51, stride=10, bias=False, padding=25),
                                     nn.PReLU(),
                                     nn.BatchNorm1d(16),
                                     nn.Conv1d(16, 32, kernel_size=11, stride=3,  bias=False, padding=5),
                                     nn.PReLU(),
                                     )
        self.decoder = nn.Sequential(
                                     nn.ConvTranspose1d(32, 16, 11, stride=3, bias=False, padding=5),
                                     nn.PReLU(),
                                     nn.ConvTranspose1d(16, 1, 51, stride=10, bias=False, padding=25),
                                     nn.PReLU())
        self.lstm = nn.LSTM(100, 32, 1, batch_first=True, bidirectional=False)
        self.fc = nn.Sequential(nn.Linear(1024, 5))
    
    def forward(self, x):
        x_enc = self.encoder(x)
        x_dec = self.decoder(x_enc)
        x_lstm, (h_final, c_final) = self.lstm(x_enc)
        h_final = h_final.permute(1, 2, 0)

        x_lstm = x_lstm.contiguous().view(x_lstm.shape[0], -1)

        x_lstm = self.fc(x_lstm)
        return x_dec, x_lstm

    def _train_epoch(self, epoch):
        """
        Training logic for an epoch

        :param epoch: Integer, current training epoch.
        :return: A log that contains average loss and metric in this epoch.
        """
        self.model.train()
        self.train_metrics.reset()
        overall_outs = []
        overall_trgs = []
        for batch_idx, (data, target) in enumerate(self.data_loader):
            data, target = data.to(self.device), target.to(self.device)

            # adding noise:
            noise = np.random.normal(0, 10, data.shape)
            noise = torch.from_numpy(noise).type(torch.cuda.FloatTensor).to(self.device)
            noisy_data = data + noise

            self.optimizer.zero_grad()
            decoder_output, classifier_output = self.model(noisy_data)
            criterion_recon = self.criterion[0]
            criterion_class = self.criterion[1]
            loss1 = criterion_recon(decoder_output, data)
            loss2 = criterion_class(classifier_output, target)
            loss = loss1 * self.config["recon_loss_penalty"] + loss2
            loss.backward()
            self.optimizer.step()

The error is in the loss calcultion

File "/tmp/pycharm_project_53/pytorch_template_AE_test/trainer/trainer.py", line 55, in _train_epoch
    loss1 = criterion_recon(decoder_output, data)
ile "/tmp/pycharm_project_53/pytorch_template_AE_test/model/loss.py", line 14, in MSELoss
    return loss(output, target)
  File "/home/emad/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/emad/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 431, in forward
    return F.mse_loss(input, target, reduction=self.reduction)
  File "/home/emad/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 2215, in mse_loss
    expanded_input, expanded_target = torch.broadcast_tensors(input, target)
  File "/home/emad/.local/lib/python3.6/site-packages/torch/functional.py", line 52, in broadcast_tensors
    return torch._C._VariableFunctions.broadcast_tensors(tensors)
RuntimeError: The size of tensor a (2971) must match the size of tensor b (3000) at non-singleton dimension 2

It’s a dimension mismatch … I don’t know why although I’m adding padding to make it like “same” padding

Depending on the setup of the convolutions, you won’t be able to get exactly the same output shape from transposed convolution using the same setup.

Have a look at the Convolution Arithmetic for more information.

I’m not sure, what input shape you are using, but e.g. for an input of [batch_size, 1, 224], you would have to use an output_padding of 1 and 3 for the nn.ConvTranspose1d in your decoder to get the same output shape.

@ptrblck Thank you for your help.