python - Some strange questions about pytorch copy a tensor
问题描述
I am a bit confused about pytorch's shared memory mechanism.
a = torch.tensor([[1,0,1,0],
[0,1,1,0]])
b = a
b[b == 1] = 0
It's easy to know that a
and b
will simutaneously become tensor([[0,0,0,0],[0,0,0,0]])
, cause a
and b
share the same memory. When I changed the code to
a = torch.tensor([[1,0,1,0],
[0,1,1,0]])
b = a
b = b - 1
b
became tensor([[0,-1,0,-1],[-1,0,0,-1]])
, but a
is still torch.tensor([[1,0,1,0],[0,1,1,0]])
.
a
and b
are sharing the same memory. Why did b
changed, while a
didn't change?
解决方案
In your second example a
and b
share the same reference but b = b - 1
is actually a copy. You are not affecting the underlying data of b
(and not of a
neither since it's the same).
You can look at it this way:
>>> a = torch.tensor([[1,0,1,0], [0,1,1,0]]) >>> b1 = a >>> b2 = b1 - 1
Comparing their pointer to the data buffers:
>>> a.data_ptr() == b1.data_ptr() True >>> b1.data_ptr() == b2.data_ptr() False
If in fact, you operate on
b
inplace, you will of course changea
as well:>>> a = torch.tensor([[1,0,1,0], [0,1,1,0]]) >>> b1 = a >>> b1.sub_(1)
Then you haven't made a copy:
>>> a.data_ptr() == b1.data_ptr() True
推荐阅读
- xsl-fo - 有分页符时,表格的行可以转到页脚吗?
- html - 如何更改otree中单选按钮的大小?
- firefox-addon - Firefox 扩展数据的查找位置/位置(profileName/browser-extension-data 除外)
- php - 应该需要一组复选框,而不是每个单独的复选框
- java - 使用 collect 在 Java 流中保留顺序
- python - 提取 Mustache 变量以在 Django 模板标签中使用
- c# - 如何在图像控件中显示图像的原始大小
- angular - 从 Angular 返回多个解析器 - 初学者
- r - R - 将字符向量转换为数据帧
- vba - 如何在 VBA 中复制和粘贴单元格