Tensor creation and dimensional manipulation
1. Tensor type (DType)
1.1 Conversion between types
T =torch.randn(3,3,dtype=torch.float) t1= t.teo (torch.float64)# t.teo () returns a new tensor without changing the value of toCopy the code
2. Storage device of tensors
Summary: The to() function converts both types and storage devices; Operations between two or more vectors can only be performed on the same device ·1;
3. Creation of tensors (from general to Special)
3.1 Created by torch. Tensor (
Torch. Tensor ([[1, 2], [3, 4]], dtype = torch. Float32); # receiving list type torch. Tensor (np) array ([[1, 2], [3, 4]]), dtype = torch. Int32); Accept numpy typeCopy the code
3.2 Create tensors from functions of pyTorch built-in functions
Torch. Zeros (2,2) torch. Ones (2,3) torch. Eye (3)# generate the 3x3 identity matrix torch Torch. Rand (3,3) # generates a 3x3 tensor with uniform distribution in the range 0-1. Torch. Randn (3,3) # generates a 3x3 tensor with normal distributionCopy the code
3.3 Creating tensors of the same shape from known tensors (remember with 2.2)
Ones_like (t) torch. Randint_like (t,0,10) torch. torch.randn_like(t)Copy the code
Summary: Essentially, you create new tensors by taking the shapes of known tensors
3.4 Create tensors with different shapes but the same data type from known tensors
T = torch. Randn (3, 3, dtype = torch. Float t.n ew_tensor ([1, 2, 3]) t.n ew_zeros (3, 3) t.n ew_ones (3, 3)Copy the code
Summary: There’s no new_randn,new_rand, there’s new_tensor(), which is generic; New_xxx is called by a tensor variable notice the difference; Essentially you take the dtype load of a known tensor and create a new tensor;