How to know if two torch.storage is the same?

I am confused.

Python 3.6.5 and PyTorch 0.4.0

>>> a = torch.Tensor([[1,3],[5,7]])
>>> a
tensor([[ 1.,  3.],
        [ 5.,  7.]])
>>> b = torch.Tensor(a)
>>> a_storage = a.storage()
>>> b_storage = b.storage()
>>> id(a.storage()) == id(b.storage())
True
>>> id(a_storage) == id(b_storage)
False
>>>
>>> a[0,0] = 0
>>> a
tensor([[ 0.,  3.],
        [ 5.,  7.]])
>>> b
tensor([[ 0.,  3.],
        [ 5.,  7.]])

and

>>> a = torch.tensor([[1,3],[5,7]])
>>> a
tensor([[ 1.,  3.],
        [ 5.,  7.]])
>>> b = torch.tensor(a)
>>> a_storage = a.storage()
>>> b_storage = b.storage()
>>> id(a.storage()) == id(b.storage())
True
>>> id(a_storage) == id(b_storage)
False
>>>
>>> a[0,0] = 0
>>> a
tensor([[ 0.,  3.],
        [ 5.,  7.]])
>>> b
tensor([[ 1.,  3.],
        [ 5.,  7.]])

???

1 Like

The lowercase torch.tensor constructor, as you’ve noticed, copies the underlying tensor data.

One way to tell is to use .data_ptr:

a.data_ptr() == b.data_ptr()