Which parameter is pass in view function?

MisBah_Awan: <p>i  want to ask u,pass hidden layer or input layer in view function?<code>(x.view(-1,64))</code><br>
and i am randomly choose the hidden layer  and input layers? where as  i have 3064 images with 512x512 dimensions.<br>
i am asking about this:</p>
<pre><code class="lang-auto">self.fc1 = nn.Linear(262144, 64)
        self.fc2 = nn.Linear(64, 3)
        #self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1,64)
</code></pre>

ptrblck: <p>Usually you use a line of code like <code>x = x.view(-1, 64)</code> to create a new view of the tensor <code>x</code>, so that for example you can feed the output of a conv layer into a linear layer.<br>
What exactly would you like to know?<br>
Currently the code snippet seems to be missing some important parts.<br>
Do you have trouble implementing some architecture?</p>

MisBah_Awan: <p>here is full code have a look:</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 3)
        self.fc1 = nn.Linear(262144,1500)
        self.fc2 = nn.Linear(1500, 3)
        #self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        
        #x = x.view(x.size(0), -1)
        #output = Net(x)
        x = x.view(-1,262144)
        x = F.relu(self.fc1(x))
        #x = F.relu(self.fc2(x))
        x = self.fc2(x)
        return x
</code></pre>

ptrblck: <p>Currently the number of input features to <code>fc1</code> is set as <code>512*512=262144</code>, which won’t work in this architecture.<br>
Note that you are using pooling layers, which decrease the spatial size with a factor of 2 in your setup.<br>
Also you are not using any padding in the conv layers, which will reduce the spatial size by 2 pixels using a kernel size of 3.<br>
Here is the working and annotated model:</p>
<pre><code class="lang-python">class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 3)
        self.fc1 = nn.Linear(64*126*126,1500)
        self.fc2 = nn.Linear(1500, 3)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  # output size: [batch_size, 32, 255, 255]
        x = self.pool(F.relu(self.conv2(x)))  # output size: [batch_size, 64, 126, 126]
        
        x = x.view(x.size(0), -1)  # output size: [batch_size, 64*126*126]
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x


x = torch.randn(1, 3, 512, 512)
model = Net()
output = model(x)
</code></pre>

MisBah_Awan: <p>kindly tell me the formula. how to find the the input layers?</p>

ptrblck: <p>The output sizes are beautifully explained in Stanford’s <a href="http://cs231n.github.io/convolutional-networks/" rel="nofollow noopener">CS231n</a>.<br>
In my architecture each conv layer will reduce the spatial size by two in both dimensions, while the pooling layers will divide them by two.<br>
For an input size of <code>[batch_size, 3, 512, 512]</code> you will end up as:</p>
<pre><code class="lang-auto">conv1: 512 - 2 = 510
pool: 510 / 2 = 255
conv2: 255 - 2 = 253
pool: 253 / 2 = 126 (floor)
</code></pre>

adarsh_pandey: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> what are the parameters taken by the view() function please explain…<br>
Thanking You,</p>

ptrblck: <p><code>.view</code> takes the desired shape as the input arguments. E.g. <code>x.view(x.size(0), -1)</code> flattens the tensor while keeping the batch dimension intact.</p>
<p>As the name suggests, you will get a view of the tensor, so that no copy will be performed, but rather just the strides and shape will be charged.</p>

adarsh_pandey: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> can we innput anything other then then -1 as the second argument …</p>

ptrblck: <p>Sure, the <code>-1</code> just means that PyTorch should automatically fill up this dimension, so that the number of elements stay the same.<br>
You could of course specify is manually:</p>
<pre><code class="lang-python">x = torch.arange(10)
y = x.view(2, 1, 5)
</code></pre>

adarsh_pandey: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> that means the second argument means the size to which we want to reshape ???<br>
also how does PyTorch automatically fills up this dimension??</p>

ptrblck: <p>No, each passed value results in the desired shape of the new view on this tensor.<br>
Have a look at <a href="https://discuss.pytorch.org/t/contigious-vs-non-contigious-tensor/30107/2">this post</a> for some additional information. It’s more focused on the <code>contiguous</code> call, however it might also give you a better idea about the <code>view</code> operation.</p>