Reshape selected axis (with torch.reshape)?

username: peepeepoopoo
cooked: <p>Hi! So kind of a stupid question but say if I have a tensor dimension of T x B x C x H x W and I flattened it to TB x C x H x W, would torch reshape using the original dimension values gave back the same order of data? Is there a way that I can reshape only the selected axis to expand the dimension? and would the order also be maintained?</p>

username: ptrblck
cooked: <p>Yes, as long as you don’t permute the dimensions, <code>view</code> will yield the original tensor:</p>
<pre><code class="lang-python">T, B, C, H, W = 2, 3, 4, 5, 6
x = torch.randn(T, B, C, H, W)
y = x.view(-1, C, H, W)
z = y.view(T, B, C, H, W)
print((z == x).all())
&gt; tensor(True)
</code></pre>

username: peepeepoopoo
cooked: <p>Gotcha, thank you! I was able to verify that is the case here.</p>

username: Jaideep_Valani
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
My tensor shape is  4*H,W<br>
if arrange this using loop to bigger H,W that is factor of above i get correct Image.<br>
But when i simply do view(BigH,BigW)  i get incorrect image  ,there is repetition of patches</p>

username: ptrblck
cooked: <p>Could you post a code snippet showing these unexpected results, please?</p>

username: Jaideep_Valani
cooked: <p>Pred= model (x)<br>
<span class="hashtag">#pred</span> shape is 4 * 4* 272,352<br>
pred= pred.argmax(1).contiguous()<br>
<span class="hashtag">#pred</span> shape is 4 <em>272</em>352<br>
pred= pred.view(2,2,272,356).view(544,704) <span class="hashtag">#gives</span> incorrect image<br>
<span class="hashtag">#expected</span> ordering of index is[ [0,1][2,3]]</p>
<p>When  use for loop i get right image<br>
for i in rows<br>
for j in columns<br>
img[i:i+1 *272,j:j+1*352]= pred[index]<br>
Index=index+1</p>

username: ptrblck
cooked: <p>In your code you are interleaving dimensions, which I tried to mention in:</p>
<blockquote>
<p>as long as you don’t permute the dimensions, <code>view</code> will yield the original tensor</p>
</blockquote>
<p>I’m also unsure how the <code>for</code> loop approach would work and if you have a typo or of you want to replace the values in <code>img</code>. Should the indexing be <code>[i:(i+1)*272, j:(j+1)*352]</code>?<br>
The reshape operation is also failing, as it seems you want to flatten it to <code>(2, 2, 272, 352)</code>.</p>

username: Jaideep_Valani
cooked: <p>Yes it was typo… in for loop<br>
What do you mean by interleaving?<br>
So there is no straight way ?<br>
I even flattened whole tensor , then tried to reshape but it gave  same results .<br>
<a class="mention" href="/u/ptrblck">@ptrblck</a><br>
I thought pytorch does sequential reshaping</p>

username: ptrblck
cooked: <p>You are trying to flatten <code>dim0</code> into two different dimensions, which won’t work without a permutation.<br>
Since your for loop approach wasn’t updated, I assume this code snippet creates the desired output and also shows how to permute the original tensor before reshaping:</p>
<pre><code class="lang-python">pred = torch.randn(4, 4, 272, 352)
pred= pred.argmax(1).contiguous()
pred_ref = pred.view(2, 2, 272, 352).permute(0, 2, 1, 3).reshape(544, 704)
pred_ = pred.view(-1, 272, 352)

img = torch.zeros(544, 704)
index = 0
for i in range(2):
    for j in range(2):
        img[i*272:(i+1)*272,j*352:(j+1)*352]= pred_[index]
        index = index+1

print((img == pred_ref).all())
&gt; tensor(True)
</code></pre>