< Back

Env

import torch

X = torch.arange(12, dtype=torch.float32).reshape((3,4))
X
tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.]])



clone

Y = X.clone()
Y
tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.]])

effect

Creates a new tensor that open new storage and requires gradients.


detach

Y = X.detach()
Y
tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.]])

effect

Creates a new tensor that shares storage with the original tensor but does not require gradients.


clone and detach

Y = X.clone().detach()
Y
tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.]])

effect

Creates a new tensor, open new storage, and does not require gradients.