torch.Tensor.new_tensor — PyTorch 2.7 documentation (original) (raw)

Tensor.new_tensor(data, *, dtype=None, device=None, requires_grad=False, layout=torch.strided, pin_memory=False) → Tensor

Returns a new Tensor with data as the tensor data. By default, the returned Tensor has the same torch.dtype andtorch.device as this tensor.

Warning

When data is a tensor x, new_tensor() reads out ‘the data’ from whatever it is passed, and constructs a leaf variable. Therefore tensor.new_tensor(x) is equivalent to x.detach().clone()and tensor.new_tensor(x, requires_grad=True) is equivalent to x.detach().clone().requires_grad_(True). The equivalents using detach() and clone() are recommended.

Parameters

data (array_like) – The returned Tensor copies data.

Keyword Arguments

Example:

tensor = torch.ones((2,), dtype=torch.int8) data = [[0, 1], [2, 3]] tensor.new_tensor(data) tensor([[ 0, 1], [ 2, 3]], dtype=torch.int8)