# What is the difference between view() and unsqueeze()?

**URL:** <https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155>\
**Category:** Uncategorized\
**Created:** [March 17, 2017, 8:36pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155 "2017-03-17T20:36:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![wasiahmad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wasiahmad/32/248_2.png) [@wasiahmad](https://discuss.pytorch.org/u/wasiahmad)\
**Post date:** [March 17, 2017, 8:36pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/1 "2017-03-17T20:36:23Z")

</div>

Use of `unsqueeze()`:

```
input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
print(input.unsqueeze(0).size()) # prints - torch.size([1, 2, 4, 3])

```

Use of `view()`:

```
input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
print(input.view(1, -1, -1, -1).size()) # prints - torch.size([1, 2, 4, 3])

```

According to documentation, `unsqueeze()` inserts singleton dim at position given as parameter and view() creates a view with different dimensions of the storage associated with `tensor`.

What `view()` does is clear to me but I am unable to distinguish it from `unsqueeze()`. Moreover, I am not understanding when to use `view()` and when to use `unsqueeze()`?

Any help with good explanation would be appreciated!

---

<div class="post-metadata">

**Author:** ![pranav](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pranav/32/185_2.png) [@pranav](https://discuss.pytorch.org/u/pranav)\
**Post date:** [March 18, 2017, 4:40am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/2 "2017-03-18T04:40:46Z")

</div>

You cannot use the `view` function as you have written it - only one of the missing dimensions can be inferred (not more than one as you have written). This means that when adding a new axis to a tensor using `view` you have to specify all the other dimensions manually (except maybe 1). `squeeze` & `unsqueeze` pair of functions are utilities that make this very convenient, wherein we just specify where we want add or remove an axis.

Also, in the latest versions of PyTorch you can add a new axis by indexing with None as:

```python
>>> input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
>>> print(input[None].size())
torch.Size([1, 2, 4, 3])
>>> print(input[:, None].size())
torch.Size([2, 1, 4, 3])

```

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [March 18, 2017, 9:45pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/3 "2017-03-18T21:45:53Z")

</div>

Or you could use `expand`

---

<div class="post-metadata">

**Author:** ![ecolss](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ecolss/32/5045_2.png) [@ecolss](https://discuss.pytorch.org/u/ecolss)\
**Post date:** [April 9, 2017, 1:49am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/4 "2017-04-09T01:49:07Z")

</div>

`expand` seems more like repeating the elements, not adding a new dim/axis?

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [April 9, 2017, 10:18am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/5 "2017-04-09T10:18:24Z")

</div>

Yeah, but it works too. You can repeat a tensor along new dimensions as well.

---

<div class="post-metadata">

**Author:** ![nicoliKim](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nicolikim/32/3814_2.png) [@nicoliKim](https://discuss.pytorch.org/u/nicoliKim)\
**Post date:** [March 6, 2018, 10:44pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/6 "2018-03-06T22:44:16Z")

</div>

What about using `resize_` or `unsqueeze`?  
Is there a difference between those two?

If a have a tensor s=(300) and I want it to be (1,1,300)  
Shall I use s.resize\_(1,1,300) instead of using unsqueeze multiple times?  
Does this give the same result?

---

<div class="post-metadata">

**Author:** ![alexis-jacq](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alexis-jacq/32/180_2.png) [@alexis-jacq](https://discuss.pytorch.org/u/alexis-jacq)\
**Post date:** [March 7, 2018, 2:37pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/7 "2018-03-07T14:37:05Z")

</div>

> [@nicoliKim](#):
>
> If a have a tensor s=(300) and I want it to be (1,1,300)

`s.view(1,1,-1)` is the best way in that case.

---

<div class="post-metadata">

**Author:** ![nicoliKim](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nicolikim/32/3814_2.png) [@nicoliKim](https://discuss.pytorch.org/u/nicoliKim)\
**Post date:** [March 7, 2018, 5:34pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/8 "2018-03-07T17:34:53Z")

</div>

and what is the difference between s.view and s.resize\_ in this case?

_Kim Nicoli_

---

<div class="post-metadata">

**Author:** ![Wei](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@Wei](https://discuss.pytorch.org/u/Wei)\
**Post date:** [August 12, 2018, 3:25am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/9 "2018-08-12T03:25:22Z")

</div>

In pytorch 0.4, I get the same result for the input

input = torch.Tensor(2, 4, 3)

with  
print(input.view(1, 2, 4, 3).size())

and

print(input.unsqueeze(0).size())

There are no difference between unsequeeze() and view(), if the both are used correctly.  
They do not change the data storage in tensor (.storage());  
they have the same id as well ( print(id(input.unsqueeze(0).storage)) ).

It seems to me that the two (unsqueeze, view) change only the representation of a tensor.

---

<div class="post-metadata">

**Author:** ![mimoralea](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mimoralea/32/18990_2.png) [@mimoralea](https://discuss.pytorch.org/u/mimoralea)\
**Post date:** [December 19, 2018, 3:30pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/10 "2018-12-19T15:30:21Z")

</div>

How can you `unsqueeze` a 2+d tensor at once?

```auto
In [1]: import torch                                                                                                                                                                                                                           

In [2]: im = torch.Tensor(40,40)                                                                                                                                                                                                               

In [3]: im.size()                                                                                                                                                                                                                              
Out[3]: torch.Size([40, 40])

In [4]: im.view(1,1,-1).size()                                                                                                                                                                                                                 
Out[4]: torch.Size([1, 1, 1600])

```

`im.view(1,1,-1,-1)` throws an error. Best I came up with is `im.view((1,1) + im.size()).size()`, but that just looks ugly.

---

<div class="post-metadata">

**Author:** ![iacob](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/8dc957/32.png) [@iacob](https://discuss.pytorch.org/u/iacob)\
**Post date:** [March 10, 2021, 1:40pm UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155/11 "2021-03-10T13:40:01Z")

</div>

**What is `view()`?**

`view()` takes a tensor and reshapes it. A requirement being that the product of the lengths of each dimension in the new shape equals that of the original. Hence a tensor with shape (4,3) can be reshaped with `view` to one of shape:

- (1,12), (2,6), (3,4), (6,2), (12,1)

but also, any number of superficial dimensions of length 1 can be removed (i.e. `view(12)`), or added (e.g. (2,6,1), (3,1,1,4), (1,4,1,3,1) etc).

`squeeze` and `unsqueeze` are convenient synonyms for these latter two special cases where that is the only change in shape.

Likewise `flatten` is a [convenient synonym](https://discuss.pytorch.org/t/what-is-the-difference-of-flatten-and-view-1-in-pytorch/51790/4) for `view(-1)`.

**When to use `unsqueeze()`?**

Use `view` (or `reshape`) when you want to generically reshape a tensor.

If you want to specifically add a superficial dimension (e.g. for treating a single element like a batch, or to concatenate with another tensor), `unsqueeze` is a more convenient (and explicit) synonym, but the underlying operation is the same.

* * *

**Note:** `view(1, -1, -1, -1)` will not work (-1 can only be used once to infer one dimension’s size). What you want to do can be achieved with `view(1, *input.shape)`.
