当 a 和 b 引用相同的数据时,为什么 a.storage() 是 b.storage() 返回 false?

编程入门 行业动态 更新时间:2024-10-27 22:30:31
本文介绍了当 a 和 b 引用相同的数据时,为什么 a.storage() 是 b.storage() 返回 false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号.. <预><代码>>>>a = 火炬.arange(12).reshape(2, 6)>>>一种张量([[ 0, 1, 2, 3, 4, 5],[ 6, 7, 8, 9, 10, 11]])>>>b = a[1:, :]>>>b.storage() 是 a.storage()错误的

但是

<预><代码>>>>b[0, 0] = 999>>>b, a # 两个张量都改变了(张量([[999, 7, 8, 9, 10, 11]]),张量([[ 0, 1, 2, 3, 4, 5],[999, 7, 8, 9, 10, 11]]))

存储张量数据的对象究竟是什么?如何检查 2 个张量是否共享内存?

解决方案

torch.Tensor.storage() 返回 torch.Storage 每次调用.你可以在下面看到这一点

a.storage() 是 a.storage()# 错误的

要比较指向底层数据的指针,您可以使用以下内容:

a.storage().data_ptr() == b.storage().data_ptr()# 真的

这个pytorch 论坛帖子.


注意a.data_ptr()a.storage().data_ptr() 之间的区别.第一个返回指向张量的第一个元素的指针,而第二个似乎指向底层数据的内存地址(不是切片视图),尽管 没有记录.

知道了上面的内容,我们就可以理解为什么a.data_ptr()b.data_ptr()是不同的了.考虑以下代码:

导入火炬a = torch.arange(4, dtype=torch.int64)b = a[1:]b.data_ptr() - a.data_ptr()# 8

b的第一个元素的地址比a的第一个元素多8个,因为我们切片去掉了第一个元素,每个元素是8个字节(dtype 是 64 位整数).

如果我们使用与上面相同的代码,但使用 8 位整数数据类型,则内存地址将相差 1.

>>> a = torch.arange(12).reshape(2, 6)
>>> a
tensor([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11]])
>>> b = a[1:, :]
>>> b.storage() is a.storage()
False

But

>>> b[0, 0] = 999
>>> b, a # both tensors are changed
(tensor([[999,   7,   8,   9,  10,  11]]),
 tensor([[  0,   1,   2,   3,   4,   5],
         [999,   7,   8,   9,  10,  11]]))

What is exactly the objects that stores tensor data? How can I make check if 2 tensors share memory?

解决方案

torch.Tensor.storage() returns a new instance of torch.Storage on every invocation. You can see this in the following

a.storage() is a.storage()
# False

To compare the pointers to the underlying data, you can use the following:

a.storage().data_ptr() == b.storage().data_ptr()
# True

There is a discussion of how to determine whether pytorch tensors share memory in this pytorch forum post.


Note the difference between a.data_ptr() and a.storage().data_ptr(). The first returns the pointer to the first element of the tensor, whereas the second seems to the point to the memory address of the underlying data (not the sliced view), though it is not documented.

Knowing the above, we can understand why a.data_ptr() is different from b.data_ptr(). Consider the following code:

import torch

a = torch.arange(4, dtype=torch.int64)
b = a[1:]
b.data_ptr() - a.data_ptr()
# 8

The address of the first element of b is 8 more than the first element of a because we sliced to remove the first element, and each element is 8 bytes (the dtype is 64-bit integer).

If we use the same code as above but use an 8-bit integer data type, the memory address will be different by one.

这篇关于当 a 和 b 引用相同的数据时,为什么 a.storage() 是 b.storage() 返回 false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 05:58:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   storage   false

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!