[Learning Series 2] Pytorch

Table of contents

Pytorch uses

1. Tensor

2. Create tensor in Pytorch

3. Common methods of tensor in Pytorch

4. The data type of tensor

5. Other operations of tensor


Pytorch uses

1. Tensor

 Tensor is a general term, which includes many types:
        1.0-order tensor: scalar, constant, 0-D Tensor
        2.1-order tensor: vector, 1-D Tensor
        3.2-order tensor: matrix, 2-D Tensor
        4.3-order tensor
        5. ..
        6. N order tensor

2. Create tensor in Pytorch

1. Create a tensor using a list or sequence in python

a = torch.Tensor([[1, 2, 3], [4, 5, 6]])
print(a)
'''
tensor([[1., 2., 3.],
        [4., 5., 6.]])
'''

2. Create a tensor using an array in numpy

array = np.arange(12).reshape(3, 4)
print(array)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
'''

3. Use torch's api to create tensor
        1.torch.empty(3,4) creates an empty tensor with 3 rows and 4 columns, which will be filled with useless data

        2.torch.ones([3,4]) Create a tensor with 3 rows and 4 columns that is all 1

        3.torch.zeros([3,4]) creates a tensor with 3 rows and 4 columns that is all 0

        4.torth.rand([3,4]) Create a tensor with 3 rows and 4 columns of random values, the interval of random values ​​is [0, 1)

rand = torch.rand(2, 3)
print(rand)
'''
tensor([[0.0311, 0.8508, 0.8894],
        [0.5359, 0.6662, 0.5128]])
'''

        5. torch, randint(1ow=0, high=10, size=[3,4]) creates a tensor of random integers with 3 rows and 4 columns, and the interval of random values ​​is [low, high)

randint = torch.randint(3, 10, (2, 2))
print(randint)
'''
tensor([[3, 3],
        [5, 6]])
'''

        6.torch.randn([3,4]) creates a tensor of random numbers with 3 rows and 4 columns, the distribution mean of random values ​​is 0, and the variance is 1

3. Common methods of tensor in Pytorch

1. Get the data in tensor (when only one element is available in tensor): tensor.item()

t1 = torch.Tensor([[10]])
print(t1)
print(t1.item())
'''
10
'''

2. Convert to numpy array

t2 = t1.numpy()
print(t2)
'''
[[10.]]
'''

3. Get shape: tensor.size()

t3 = torch.rand(3, 4)
print(t3.size())
'''
torch.Size([3, 4])
'''

4. Shape change: tensor.view((3,4)). Similar to reshape in numpy, it is a shallow copy, only the shape changes

t4 = t3.view(2, 6)
print(t3)
print(t4)
'''
tensor([[0.5064, 0.2271, 0.7398, 0.1612],
        [0.9324, 0.7501, 0.8088, 0.4055],
        [0.7744, 0.1162, 0.8764, 0.2311]])
tensor([[0.5064, 0.2271, 0.7398, 0.1612, 0.9324, 0.7501],
        [0.8088, 0.4055, 0.7744, 0.1162, 0.8764, 0.2311]])
'''

5. Get the dimension (order): tensor.dim()

print(t4.dim())
'''
2
'''

6. Get the maximum value: tensor.max()

print(t4)
print(t4.max())
'''
tensor([[0.6645, 0.9753, 0.1691, 0.9781, 0.9510, 0.5923],
        [0.7698, 0.4558, 0.5393, 0.7076, 0.2258, 0.8842]])
tensor(0.9781)
'''

7. Transpose: tensor.t() (T in numpy)

print(t4)
print(t4.t())
'''
tensor([[0.8161, 0.3213, 0.7988, 0.9154, 0.0466, 0.0426],
        [0.3853, 0.7590, 0.3691, 0.5878, 0.1044, 0.6844]])
tensor([[0.8161, 0.3853],
        [0.3213, 0.7590],
        [0.7988, 0.3691],
        [0.9154, 0.5878],
        [0.0466, 0.1044],
        [0.0426, 0.6844]])
'''
t4 = torch.rand(2, 3, 4)
print(t4)
print(t4.transpose(0, 1))
print(t4.permute(1, 0, 2))
'''
tensor([[[0.5269, 0.0171, 0.0110, 0.3139],
         [0.9625, 0.0424, 0.1559, 0.9512],
         [0.2839, 0.9033, 0.7233, 0.7983]],

        [[0.4119, 0.1300, 0.7744, 0.7593],
         [0.7209, 0.8706, 0.0809, 0.3257],
         [0.7994, 0.2252, 0.3721, 0.4984]]])
tensor([[[0.5269, 0.0171, 0.0110, 0.3139],
         [0.4119, 0.1300, 0.7744, 0.7593]],

        [[0.9625, 0.0424, 0.1559, 0.9512],
         [0.7209, 0.8706, 0.0809, 0.3257]],

        [[0.2839, 0.9033, 0.7233, 0.7983],
         [0.7994, 0.2252, 0.3721, 0.4984]]])
tensor([[[0.5269, 0.0171, 0.0110, 0.3139],
         [0.4119, 0.1300, 0.7744, 0.7593]],

        [[0.9625, 0.0424, 0.1559, 0.9512],
         [0.7209, 0.8706, 0.0809, 0.3257]],

        [[0.2839, 0.9033, 0.7233, 0.7983],
         [0.7994, 0.2252, 0.3721, 0.4984]]])
'''

8.tensor[1,3] Get the value of the first row and third column in tensor

t4 = torch.rand(2, 3, 2)
print(t4)
print(t4[1, 2])
'''
tensor([[[0.0768, 0.1384],
         [0.9043, 0.0226],
         [0.0334, 0.2946]],

        [[0.9075, 0.4634],
         [0.8371, 0.1950],
         [0.3811, 0.9326]]])
tensor([0.3811, 0.9326])
'''

9.tensor[1,2]=100 Assign the position of the first row and the third column in the tensor to 100

t4 = torch.rand(2, 3, 2)
print(t4)
print(t4[1, 2])
t4[1, 2] = 100
print(t4)
'''
tensor([[[0.2413, 0.9267],
         [0.7468, 0.1347],
         [0.8241, 0.6128]],

        [[0.1088, 0.0445],
         [0.5081, 0.5531],
         [0.4178, 0.0222]]])
tensor([0.4178, 0.0222])
tensor([[[2.4131e-01, 9.2668e-01],
         [7.4677e-01, 1.3468e-01],
         [8.2412e-01, 6.1279e-01]],

        [[1.0878e-01, 4.4516e-02],
         [5.0811e-01, 5.5308e-01],
         [1.0000e+02, 1.0000e+02]]])
'''

10. Slicing of tensor

4. The data type of tensor

There are many data types in tensor, and the common types are as follows:

The Tensor types in the above figure indicate that this type of tensor is an instance

1. Get the data type of tensor: tensor.dtype

print(t4)
print(t4.dtype)
'''
tensor([[[  0.9967,   0.8391],
         [  0.9651,   0.7411],
         [  0.4698,   0.5371]],

        [[  0.9868,   0.1172],
         [  0.3568,   0.8628],
         [100.0000, 100.0000]]])
torch.float32
'''

2. Specify the type when creating data

t5 = torch.ones([2, 3], dtype=torch.float32)
print(t5)
print(t5.dtype)
'''
tensor([[1., 1., 1.],
        [1., 1., 1.]])
torch.float32
'''

3. Modification of type

t5 = torch.ones([2, 3], dtype=torch.int32)
print(t5.dtype)
t5 = t5.type(torch.float)
print(t5.dtype)
t5 = t5.double()
print(t5.dtype)
'''
torch.int32
torch.float32
torch.float64
'''

5. Other operations of tensor

1. Add tensor and tensor

x = torch.rand(3, 4)
x = x.new_ones((2, 6), dtype=torch.float32)
y = torch.rand(2, 6)
print(x)
print(y)
print(x + y)
'''
tensor([[1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.]])
tensor([[0.0763, 0.2841, 0.7078, 0.7183, 0.9554, 0.5018],
        [0.0874, 0.9510, 0.0774, 0.1421, 0.1593, 0.8375]])
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''

print(torch.add(x, y))
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''
print(x.add(y))
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''

2. Method of in-place modification 

print(x.add_(y))  # 带下划线的方法会对x进行就地修改
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''
print(x)
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''

3. The use of tensor in Gpu

  • Instantiate device: torch.device("cuda:0" iftorch.cuda.is available() else "cpu")
  • tensor.to(device) #Convert tensor to tensor supported by CUDA, or tensor supported by cpu

Guess you like

Origin blog.csdn.net/WakingStone/article/details/129459297