How to concatenate 3 tensors with different sizes as tensor

deneme: <p>x -&gt; torch.Size([1, 512, 80, 80])<br>
y -&gt; torch.Size([1, 1024, 40, 40])<br>
z -&gt; torch.Size([1, 2048, 20, 20])</p>
<p>t = concatenate(x,y,z) # for example</p>

Ajay_Babu: <p>Solved here !</p><aside class="quote" data-post="2" data-topic="1350">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fmassa/48/58_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-concatenate-list-of-pytorch-tensors/1350/2">How to concatenate list of pytorch tensors?</a> 
  </div>
  <blockquote>
    You can use <a href="http://pytorch.org/docs/torch.html#torch.cat" rel="noopener nofollow ugc">torch.cat</a> or <a href="http://pytorch.org/docs/torch.html#torch.stack" rel="noopener nofollow ugc">torch.stack</a>
  </blockquote>
</aside>


deneme: <p>thank you ,i try this but not solved</p>
<p>RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 2048 and 512 in dimension 2 at C:/w/1/s/tmp_conda_3.6_095855/conda/conda-bld/pytorch_1579082406639/work/aten/src\THC/gene<br>
ric/THCTensorMath.cu:71</p>

Ajay_Babu: <pre><code class="lang-auto">x = torch.Size([1, 512, 80, 80])
y = torch.Size([1, 1024, 40, 40])
z = torch.Size([1, 2048, 20, 20])

print(torch.cat((torch.tensor(x),torch.tensor(y),torch.tensor(z))))
print(torch.stack((torch.tensor(x),torch.tensor(y),torch.tensor(z))))

Outputs
tensor([   1,  512,   80,   80,    1, 1024,   40,   40,    1, 2048,   20,   20])
tensor([[   1,  512,   80,   80],
        [   1, 1024,   40,   40],
        [   1, 2048,   20,   20]])
</code></pre>

deneme: <p>Thanks,When I say torch size, I say the size of the array.</p>

Nikronic: <p>Hi,</p>
<p>It depends what axis you want to use for concatenation. Literally, you cannot do this as the tensors do not have the same size.</p>
<aside class="quote no-group" data-username="deneme" data-post="3" data-topic="84744">
<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/d/7cd45c/48.png" class="avatar"> deneme:</div>
<blockquote>
<p>RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 2048 and 512 in dimension 2</p>
</blockquote>
</aside>
<p>You have to have same shape for all dims except the one you one to use as the concatenation dimension. You can achieve it using padding by repetition, zeros, etc. For repetition you can use <code>torch.expand(size)</code> but for other methods such as interpolation, you need to use <code>torch.nn.functional.interpolation</code>.<br>
Personally, first I would make the dim=2 and dim=3 (last two dims) same size using <code>F.interpolate</code> then expand smaller tensors <code>x</code> and <code>y</code> by repetition using <code>torch.expand</code>.</p>
<p>Expand: <a href="https://discuss.pytorch.org/t/concat-two-tensors-with-different-dimensions/61977" class="inline-onebox">Concat two tensors with different dimensions</a><br>
Interpolation: <a href="https://discuss.pytorch.org/t/resize-tensor-without-converting-to-pil-image/52401/2" class="inline-onebox">Resize tensor without converting to PIL image? - #2 by Nikronic</a></p>
<p>Edit1: replace wrongly used <code>pad</code> instead of <code>interpolate</code><br>
Bests</p>