How to judge a tensor is view?

Like

import torch
x = torch.rand(2,3,4,5)
y = x.view(4,6,5)

If I want to know y is a view of x, which method can I use?

According to the documentation you can know this by doing :
x.storage().data_ptr() == y.storage().data_ptr().

1 Like

In my models it is used like x = torch.rand(2,3,4,5).view(4,6,5) so I can’t ger origin tensor’s storage data_ptr

I understand, but in this case you have no choice but to do the following.

y = torch.rand(2,3,4,5)
x = y.view(4,6,5)

torch.rand(2,3,4,5) is not useful here if it is not carried by a known variable.

Moreover, x and y here are conceptually indistinguishable; strides differ, but there are no “primary” strides.