栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch中的view(parm a, parm b, ..., ...) 函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch中的view(parm a, parm b, ..., ...) 函数

文章目录
    • 一、作用
    • 二、常用
    • 三、特殊
    • 四、代码

一、作用

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]])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950281.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号