# Which parameter is pass in view function?

**URL:** <https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655>\
**Category:** Uncategorized\
**Created:** [March 1, 2019, 3:48pm UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655 "2019-03-01T15:48:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MisBah\_Awan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MisBah\_Awan](https://discuss.pytorch.org/u/MisBah_Awan)\
**Post date:** [March 1, 2019, 3:48pm UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/1 "2019-03-01T15:48:55Z")

</div>

i want to ask u,pass hidden layer or input layer in view function?`(x.view(-1,64))`  
and i am randomly choose the hidden layer and input layers? where as i have 3064 images with 512x512 dimensions.  
i am asking about this:

```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)

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 2, 2019, 12:14am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/2 "2019-03-02T00:14:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MisBah\_Awan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MisBah\_Awan](https://discuss.pytorch.org/u/MisBah_Awan)\
**Post date:** [March 2, 2019, 5:48am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/3 "2019-03-02T05:48:05Z")

</div>

here is full code have a look:

```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

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 2, 2019, 11:32am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/4 "2019-03-02T11:32:07Z")

</div>

Currently the number of input features to `fc1` is set as `512*512=262144`, which won’t work in this architecture.  
Note that you are using pooling layers, which decrease the spatial size with a factor of 2 in your setup.  
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.  
Here is the working and annotated model:

```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)

```

---

<div class="post-metadata">

**Author:** ![MisBah\_Awan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MisBah\_Awan](https://discuss.pytorch.org/u/MisBah_Awan)\
**Post date:** [April 29, 2019, 4:55pm UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/5 "2019-04-29T16:55:04Z")

</div>

kindly tell me the formula. how to find the the input layers?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 30, 2019, 6:32am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/6 "2019-04-30T06:32:45Z")

</div>

The output sizes are beautifully explained in Stanford’s [CS231n](http://cs231n.github.io/convolutional-networks/).  
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.  
For an input size of `[batch_size, 3, 512, 512]` you will end up as:

```auto
conv1: 512 - 2 = 510
pool: 510 / 2 = 255
conv2: 255 - 2 = 253
pool: 253 / 2 = 126 (floor)

```

---

<div class="post-metadata">

**Author:** ![adarsh\_pandey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adarsh_pandey/32/14907_2.png) [@adarsh\_pandey](https://discuss.pytorch.org/u/adarsh_pandey)\
**Post date:** [December 24, 2019, 8:31am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/7 "2019-12-24T08:31:00Z")

</div>

@ptrblck what are the parameters taken by the view() function please explain…  
Thanking You,

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [December 24, 2019, 9:07am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/8 "2019-12-24T09:07:24Z")

</div>

`.view` takes the desired shape as the input arguments. E.g. `x.view(x.size(0), -1)` flattens the tensor while keeping the batch dimension intact.

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.

---

<div class="post-metadata">

**Author:** ![adarsh\_pandey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adarsh_pandey/32/14907_2.png) [@adarsh\_pandey](https://discuss.pytorch.org/u/adarsh_pandey)\
**Post date:** [December 24, 2019, 9:10am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/9 "2019-12-24T09:10:40Z")

</div>

@ptrblck can we innput anything other then then -1 as the second argument …

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [December 24, 2019, 9:14am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/10 "2019-12-24T09:14:34Z")

</div>

Sure, the `-1` just means that PyTorch should automatically fill up this dimension, so that the number of elements stay the same.  
You could of course specify is manually:

```python
x = torch.arange(10)
y = x.view(2, 1, 5)

```

---

<div class="post-metadata">

**Author:** ![adarsh\_pandey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adarsh_pandey/32/14907_2.png) [@adarsh\_pandey](https://discuss.pytorch.org/u/adarsh_pandey)\
**Post date:** [December 24, 2019, 9:17am UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/11 "2019-12-24T09:17:35Z")

</div>

@ptrblck that means the second argument means the size to which we want to reshape ???  
also how does PyTorch automatically fills up this dimension??

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [December 24, 2019, 9:55pm UTC](https://discuss.pytorch.org/t/which-parameter-is-pass-in-view-function/38655/12 "2019-12-24T21:55:04Z")

</div>

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