torch Tensor initialization

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2025 Oct 29 12:37
Editor
Edited
Edited
2025 Oct 29 12:39
Refs
Refs

torch.empty() fastest

shape = (2, 3, 4) x = torch.empty(shape) x = torch.empty(2, 3, 4)

torch.zeros()

shape = (2, 3, 4) x = torch.zeros(shape) x = torch.zeros(2, 3, 4)

torch.ones()

shape = (2, 3, 4) x = torch.ones(shape) x = torch.ones(2, 3, 4)

Copy

b = torch.empty_like(a) # same shape/dtype c = torch.zeros_like(a) # same shape with 0
 
 
 
 
 
 

Recommendations