Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

python - Pytorch: copy.deepcopy vs torch.tensor.contiguous()?

In python torch, it seems copy.deepcopy method is generally used to create deep-copies of torch tensors instead of creating views of existing tensors. Meanwhile, as far as I understood, the torch.tensor.contiguous() method turns a non-contiguous tensor into a contiguous tensor, or a view into a deeply copied tensor.

Then, do the two code lines below work equivalently if I want to deepcopy src_tensor into dst_tensor?

org_tensor = torch.rand(4)
src_tensor = org_tensor

dst_tensor = copy.deepcopy(src_tensor) # 1
dst_tensor = src_tensor.contiguous() # 2

If the two work equivalent, which method is better in deepcopying tensors?

question from:https://stackoverflow.com/questions/65894586/pytorch-copy-deepcopy-vs-torch-tensor-contiguous

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

torch.tensor.contiguous() and copy.deepcopy() methods are different. Here's illustration:

>>> x = torch.arange(6).view(2, 3)
>>> x
tensor([[0, 1, 2],
        [3, 4, 5]])
>>> x.stride()
(3, 1)
>>> x.is_contiguous()
True
>>> x = x.t()
>>> x.stride()
(1, 3)
>>> x.is_contiguous()
False
>>> y = x.contiguous()
>>> y.stride()
(2, 1)
>>> y.is_contiguous()
True
>>> z = copy.deepcopy(x)
>>> z.stride()
(1, 3)
>>> z.is_contiguous()
False
>>>

Here we can easily see that .contiguous() method created contiguous tensor from non-contiguous tensor while deepcopy method just copied the data without converting it to contiguous tensor.

One more thing contiguous creates new tensor only if old tensor is non-contiguous while deepcopy always creates new tensor.

>>> x = torch.arange(10).view(2, 5)
>>> x.is_contiguous()
True
>>> y = x.contiguous()
>>> z = copy.deepcopy(x)
>>> id(x)
2891710987432
>>> id(y)
2891710987432
>>> id(z)
2891710987720

contiguous()

Use this method to convert non-contiguous tensors to contiguous tensors.

deepcopy()

Use this to copy nn.Module i.e. mostly neural network objects not tensors.

clone()

Use this method to copy tensors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...