- 一、作用
- 二、常用
- 三、特殊
- 四、代码
1-1. 将原tensor重构成新的tensor。先将原tensor展成dim=1的tensor,再根据parm a, parm b, …, …重构为a * b*… * …的tensor,且重构前后tensor的元素相同。见In[13]。
二、常用2-1. 见代码In[5] In[6]。
三、特殊3-1. 若某个参数为-1,则该维度上的元素个数自动调整,但必须保证元素总数不变。
3-2. 每次使用view()之前,该tensor只要用了transpose()和permute()转置函数,则一定要在使用view()函数前用一下contiguous()函数。见:In[14] In[15]
原因:transpose()和permute()函数会导致生成的tensor占用的不是一整块内存,而使用view()函数必须满足tensor占用一整块连续内存的要求。
3-3. 如果重构前的shape和重构后的shape相同,则两个tensor的元素相同,但不是同一个tensor。见In[9] In[10]。
In[3]: a = torch.rand(4, 1, 28, 28) #生成元素服从0-1均匀分布的Tensor。
In[4]: a.shape
Out[4]: torch.Size([4, 1, 28, 28])
In[5]: a.view(4, 1 * 28 * 28) #view()重构
Out[5]:
tensor([[0.2660, 0.7497, 0.5505, ..., 0.2350, 0.5027, 0.0898],
[0.8779, 0.3542, 0.1804, ..., 0.6070, 0.3469, 0.0923],
[0.2596, 0.3707, 0.1245, ..., 0.2191, 0.2887, 0.1363],
[0.2262, 0.8925, 0.1345, ..., 0.1647, 0.7298, 0.9179]])
In[6]: a.view(4, 1 * 28 * 28).shape
Out[6]: torch.Size([4, 784])
In[7]: b = a.view(4,784)
In[8]: b1 = b.view(4, 1, 28, 28)
In[9]: b1 == a
Out[9]:
tensor([[[[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
...,
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True]]],
...
...
[[[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
...,
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True]]]])
In[10]: b1 is a
Out[10]: False
In[11]: b2 = b.view(4, 28, 28, 1)
In[12]: b2 == a
Out[12]:
tensor([[
...
[[[ True, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]],
...
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, True]]]])
In[13]: a.view(4, 783)
Traceback (most recent call last):
File "E:minicondainstall-filelibsite-packagesIPythoncoreinteractiveshell.py", line 3397, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
a.view(4, 783)
RuntimeError: shape '[4, 783]' is invalid for input of size 3136
In[14]: a.transpose(1, 3).view(4, 1*28*28)
Traceback (most recent call last):
File "E:minicondainstall-filelibsite-packagesIPythoncoreinteractiveshell.py", line 3397, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in |
a.transpose(1, 3).view(4, 1*28*28)
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
In[15]: a.transpose(1, 3).contiguous().view(4, 1*28*28)
Out[15]:
tensor([[0.2660, 0.2308, 0.9646, ..., 0.3541, 0.0460, 0.0898],
[0.8779, 0.6723, 0.4005, ..., 0.0378, 0.7965, 0.0923],
[0.2596, 0.0018, 0.7991, ..., 0.2613, 0.3324, 0.1363],
[0.2262, 0.2708, 0.7940, ..., 0.6500, 0.8061, 0.9179]])
| | 


