pytorch reshape view clone的区别

reshape view作为pytorch中torch的常用操作,有一些小细节需要注意一下。

x = torch.arange(12)
print('x')
print(x)
print(id(x))

y = x.reshape(3,4)
print('y')
print(y)
print(id(y))

x[:] = 2
print('x')
print(x)

print('y')
print(y)

以上代码输出结果为

x
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
140575189363840
y
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
140575189361680
x
tensor([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
y
tensor([[2, 2, 2, 2],
        [2, 2, 2, 2],
        [2, 2, 2, 2]])

可以看到,x进行reshape操作后生存的y,在地址上是不一样的。但是当x的元素值修改时,y的元素值也跟着修改,这表明reshape操作可以理解为仅仅创建了一个新的指向数据的指针,并没有去开辟新的内存空间,复制新的一份数据。也就是说,reshape操作后,数据存储还是一份,但是指向該数据的指针增多一个。

同时,与reshape操作类似,view也是同种效果.

这表明,reshape操作和view操作都是创建一个新的指针变量,指向原有的数据。尽管数据的shape不一样,但是值是一样的。

那么如何复制原有数据,并且完全创建新的一份数据呢,使得两份数据之间互不干扰?

可以使用clone函数。

x = torch.arange(12)
print('x')
print(x)
print(id(x))
y = torch.clone(x).reshape(3,4)
print('y')
print(y)
print(id(y))
x[:] = 2
print('x')
print(x)
print('y')
print(y)

结果为

x
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
140575196239024
y
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
140575189994624
x
tensor([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
y
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])

总结

1.使用reshape或者view操作后,数据的指针变量修改,但是数据并没有改。因此修改数据数值,会同时影响多个变量。

2.要想完全复制新的一份数据,包括开辟内存空间,赋予新的指针变量,使用clone函数。

 

这里有一个博客写得相当不错

https://blog.csdn.net/Flag_ing/article/details/109129752?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值