What is reshape layer in pytorch?

username: melody-rain
cooked: <p>Hi all,<br>
What is the reshape layer in pytorch?</p>
<p>In torch7 it seems to be nn.View, but what it is in pytorch?</p>
<p>What I want is to add a reshpe layer in nn.Sequential.</p>
<p>Thanks.</p>

username: apaszke
cooked: <p>We don’t recommend that. Use <code>nn.Sequential</code> only for trivial sequences, if you need to insert some reshaping or views, wrap it in the container. You can see how <a href="https://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py">torchvision models</a> are implemented.</p>

username: melody-rain
cooked: <aside class="quote no-group" data-username="apaszke" data-post="2" data-topic="1110">
<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/apaszke/48/21_2.png" class="avatar"> apaszke:</div>
<blockquote>
<p>recommend</p>
</blockquote>
</aside>
<p>Hi, good example. Thanks.</p>
<p>what is not recommended? My network is a bit complex so I use nn.Sequential.<br>
BTW, if  I do not use nn.Sequential, what is the reshape layer in pytorch?</p>
<p>Thank you.</p>

username: apaszke
cooked: <p>There’s no reshape layer. You just call <code>.view</code> on the output you want to reshape in the <code>forward</code> function of your custom model.</p>

username: ypxie
cooked: <p>I didn’t find how reshape is wrapped in a container in that example. Could you elabrate a little more?<br>
Thanks!</p>

username: smth
cooked: <p>you can see this line here <a href="https://github.com/pytorch/examples/blob/master/dcgan/main.py#L178">https://github.com/pytorch/examples/blob/master/dcgan/main.py#L178</a></p>

username: ypxie
cooked: <p>Thanks for your reply. But it is still in forward function.<br>
How could I do something like <code>self.node = nn.sequential(*layers)</code>, layers contain <code>reshape</code>, and later I only need to call <code>self.node(input)</code></p>

username: allenye0119
cooked: <p>If you really want a reshape layer, maybe you can wrap it into a <code>nn.Module</code> like this:</p>
<pre><code class="lang-auto">import torch.nn as nn

class Reshape(nn.Module):
    def __init__(self, *args):
        super(Reshape, self).__init__()
        self.shape = args

    def forward(self, x):
        return x.view(self.shape)
</code></pre>

username: ypxie
cooked: <p>Thanks~ but it is still so many codes, a lambda layer like the one used in keras would be very helpful.</p>

username: smth
cooked: <p>We are not big on layers, in fact you can avoid entire sequential and put a for loop <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Shisho_Sama
cooked: <p>Knowing that <code>Flatten()</code> layer was recently added, how about adding the <code>Reshape</code> layer as well for the very same reason Flatten() was added.<br>
It just makes life easier, specially for new comers from Keras. and Also, it can come in handy in normal sequential models as well.</p>

username: Noam_Salomonski
cooked: <p>I have to ask why reshaping does not count as “trivial”?</p>
<p>Current way of work forces me to separate logic of data flow to two separate places - definition of the nn.Sequential, and forward()</p>

username: dsantiago
cooked: <p>I think in Pytorch the way of thinking, differently from TF/Keras, is that layers are generally used on some process that requires some gradients, <code>Flatten()</code>, <code>Reshape()</code>, <code>Add()</code>, etc… are just formal process, no gradients involved, so you can just use helper functions like the ones in <code>torch.nn.functional.*</code>…</p>
<p>There’s some use cases where a <code>Reshape()</code> layer can come in handy, like in embedded systems where you add to your model firstly a reshape, so that all the model is compacted to be flashed in the device, and the reshape can adjust incoming data from sensors…</p>
<p>For high level DL, those layers are more confusing than beneficial…</p>

username: alexp
cooked: <p>I think the layer <a href="https://pytorch.org/docs/stable/generated/torch.nn.Unflatten.html" rel="noopener nofollow ugc">nn.Unflatten()</a> may do the job. It can be inserted into a Sequential model.</p>