Sum up tensor of different shape

Allaye: <p>So i am doing some new work on the Forward Forward algorithm using ConvNet, when performing prediction i will need to sum up the goodness score for each layer, the problem is each of this scores have different dimension since the Convolution operation has been performed on the data, here is my code sample.</p>
<pre><code class="lang-auto">  def predict(self, x):
        goodness_score_per_label = []
        for label in range(self.output_dim):
            # perform one hot encoding#
            print('label:', label, x.shape)
            encoded = overlay_y_on_x(x, label)
            goodness = []
            for idx, layer in enumerate(self.layers):
                encoded = layer(encoded)
                print('encoded:', encoded.shape)
                goodness += [encoded.pow(2).mean(1)]
                print('goodness:', len(goodness), goodness[idx].shape)
            goodness_score_per_label += [sum(goodness).unsqueeze(1)]
        goodness_score_per_label = torch.cat(goodness_score_per_label, 1)
        return goodness_score_per_label.argmax(1)
</code></pre>
<p>here is the tensor dimension,</p>
<pre><code class="lang-auto">encoded: torch.Size([50000, 6, 14, 14])
goodness: 1 torch.Size([50000, 14, 14])
encoded: torch.Size([50000, 16, 7, 7])
goodness: 2 torch.Size([50000, 7, 7])
encoded: torch.Size([50000, 120, 3, 3])
</code></pre>
<p>i am getting the error from this line <code>goodness_score_per_label += [sum(goodness).unsqueeze(1)]</code> what is the best way of handling this problem?</p>

ptrblck: <aside class="quote no-group" data-username="Allaye" data-post="1" data-topic="197967">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/f08c70/48.png" class="avatar"> Allaye:</div>
<blockquote>
<p>i am getting the error from this line <code>goodness_score_per_label += [sum(goodness).unsqueeze(1)]</code></p>
</blockquote>
</aside>
<p>This line shouldn’t raise an error since you are just appending a new tensor to a <code>list</code>, which does not care about the shapes of each tensor.<br>
However, <code>goodness_score_per_label = torch.cat(goodness_score_per_label, 1)</code> could fail as it expects all tensors to have the same shape in all dims besides <code>dim1</code>.<br>
If you are seeing a shape mismatch there you might want to reduce additional dimensions before creating a single tensor via <code>torch.cat</code>.</p>

Allaye: <p>i am appending and doing a summation, here is the traceback.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/c/dcb7824cd92d265a4c3fea4ce8f1a060814f3ce7.png" data-download-href="https://discuss.pytorch.org/uploads/default/dcb7824cd92d265a4c3fea4ce8f1a060814f3ce7" title="error"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/c/dcb7824cd92d265a4c3fea4ce8f1a060814f3ce7.png" alt="error" data-base62-sha1="vuyd024KsBou3VMSk40p9mWoZJJ" width="690" height="55" data-dominant-color="333333"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error</span><span class="informations">853×69 6.41 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>You might need to reduce the tensors first if this reduction also fails as PyTorch would try to broadcast the tensors and might fail.</p>

Allaye: <p>but this will cause loss of information, if i am correct.</p>