Tensor size mismatch autoencoder

username: emad
cooked: <pre><code class="lang-auto">        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())
</code></pre>
<p>I think this should match well, however I get this error:<br>
<code>RuntimeError: The size of tensor a (2971) must match the size of tensor b (3000) at non-singleton dimension 2</code></p>
<p>Any suggestions please?</p>

username: ptrblck
cooked: <p>Which line of code is throwing this issue?<br>
Could you post an executable code snippet, so that we could have a look?</p>

username: emad
cooked: <pre><code class="lang-auto">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

</code></pre>
<pre><code class="lang-auto">    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()
</code></pre>
<p>The error is in the loss calcultion</p>
<pre><code class="lang-auto">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
</code></pre>

username: emad
cooked: <p>It’s a dimension mismatch … I don’t know why although I’m adding padding to make it like “same” padding</p>

username: ptrblck
cooked: <p>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.</p>
<p>Have a look at the <a href="http://deeplearning.net/software/theano_versions/dev/tutorial/conv_arithmetic.html">Convolution Arithmetic</a> for more information.</p>
<p>I’m not sure, what input shape you are using, but e.g. for an input of <code>[batch_size, 1, 224]</code>, you would have to use an <code>output_padding</code> of <code>1</code> and <code>3</code> for the <code>nn.ConvTranspose1d</code> in your <code>decoder</code> to get the same output shape.</p>

username: emad
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thank you for your help.</p>