一、Complete Pytorch Tensor Tutorial (Initializing Tensors, Math, Indexing, Reshaping)

import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
yy_tensor = torch.tensor([[1,2,3],[4,5,6]],dtype=torch.float32,device=device,requires_grad=True)
yy_tensor
"""
tensor([[1., 2., 3.],
        [4., 5., 6.]], requires_grad=True)
"""
yy_tensor.dtype
"""
torch.float32
"""
yy_tensor.device
"""
device(type='cpu')
"""
yy_tensor.shape
"""
torch.Size([2, 3])
"""
yy_tensor.requires_grad
"""
True
"""

Other common initialization methods

x = torch.empty(size=(3,3))
x
"""
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
"""
x = torch.zeros((3,3))
x
"""
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
"""
x = torch.rand((3,3))
x
"""
tensor([[0.4794, 0.7025, 0.0480],
        [0.4072, 0.1896, 0.0588],
        [0.2013, 0.2112, 0.0145]])
"""
x = torch.ones((3,3))
x
"""
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
"""
x = torch.eye(5,5)
x
"""
tensor([[1., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0.],
        [0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 1.]])
"""
x = torch.arange(start=0,end=5,step=1)#[0,5)每隔1个取个值
x
"""
tensor([0, 1, 2, 3, 4])
"""
x = torch.linspace(start=0.1,end=1,steps=10)#[0.1,1]取10个值
x
"""
tensor([0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 0.6000, 0.7000, 0.8000, 0.9000,
        1.0000])
"""
x = torch.empty(size=(1,5)).normal_(mean=0,std=1)#mean正态分布,std标准差
x
"""
tensor([[-0.7036,  1.3794, -0.1708,  0.5470, -0.5959]])
"""
x = torch.empty(size=(1,5)).uniform_(0,1)# [0~1]之间的均匀分布
x
"""
tensor([[0.3317, 0.2359, 0.3101, 0.8314, 0.6557]])
"""
x = torch.diag(torch.ones(3))# 对角阵
x
"""
tensor([[5., 0., 0.],
        [0., 5., 0.],
        [0., 0., 5.]])
"""

How to initialize and convert tensors to other types (int,float,double)

tensor = torch.arange(4)
tensor,tensor.bool()
"""
(tensor([0, 1, 2, 3]), tensor([False,  True,  True,  True]))
"""
tensor.short()
"""
tensor([0, 1, 2, 3], dtype=torch.int16)
"""
tensor.long()
"""
tensor([0, 1, 2, 3])
"""
tensor.half()
"""
tensor([0., 1., 2., 3.], dtype=torch.float16)
"""
tensor.float()
"""
tensor([0., 1., 2., 3.])
"""
tensor.double()
"""
tensor([0., 1., 2., 3.], dtype=torch.float64)
"""

Array to Tensor conversion and vice-versa

import numpy as np
np_array = np.zeros((5,5))
np_array
"""
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
"""
tensor_array = torch.from_numpy(np_array)
tensor_array
"""
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]], dtype=torch.float64)
"""
np_array_back = tensor_array.numpy()
np_array_back
"""
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
"""

Tensor Math & Comparison Operations

x = torch.tensor([1,2,3])
y = torch.tensor([9,8,7])
Addition
z1 = torch.empty(3)
torch.add(x,y,out=z1)
z1
"""
tensor([10., 10., 10.])
"""
z2 = torch.add(x,y)
z2
"""
tensor([10, 10, 10])
"""
z3 = x + y
z3
"""
tensor([10, 10, 10])
"""
Subtraction
z = x - y
z
"""
tensor([-8, -6, -4])
"""
Division
z = torch.true_divide(x,y)
z
"""
tensor([0.1111, 0.2500, 0.4286])
"""
inplace operations
x = torch.tensor([1,2,3])
t = torch.zeros(3)
t.add(x)
t += x
x, t
"""
(tensor([1, 2, 3]), tensor([1., 2., 3.]))
"""
x = torch.tensor([1,2,3])
t = torch.zeros(3)
t.add_(x)
t += x
x, t
"""
(tensor([1, 2, 3]), tensor([2., 4., 6.]))
"""

Exponentiation

x = torch.tensor([1,2,3])
z = x.pow(2)
x, z
"""
(tensor([1, 2, 3]), tensor([1, 4, 9]))
"""
z = x ** 2
z
"""
tensor([1, 4, 9])
"""

Simple comparison

x = torch.tensor([1,2,3])
z = x > 0
x, z
"""
(tensor([1, 2, 3]), tensor([True, True, True]))
"""
x = torch.tensor([1,2,3])
z = x <0
x, z
"""
(tensor([1, 2, 3]), tensor([False, False, False]))
"""

Matrix Multiplication

x1 = torch.rand((2,5))
x2 = torch.rand((5,3))
x3 = torch.mm(x1,x2)
x1.shape, x2.shape, x3.shape
"""
(torch.Size([2, 5]), torch.Size([5, 3]), torch.Size([2, 3]))
"""
x3 = x1.mm(x2)
x3, x3.shape
"""
(tensor([[1.8193, 1.0509, 1.8723],
         [1.1239, 0.7337, 0.7515]]),
 torch.Size([2, 3]))
"""

matrix exponentiation

matrix_exp = torch.rand(5,5)
matrix_exp.matrix_power(3), matrix_exp.matrix_power(3).shape
"""
(tensor([[2.2197, 1.9763, 2.0973, 2.0499, 1.4975],
         [2.7670, 2.4388, 2.8124, 2.9336, 2.0838],
         [3.2308, 2.8073, 2.6072, 3.1519, 2.2899],
         [3.6003, 3.1678, 2.8685, 3.2331, 2.3801],
         [3.2029, 2.8286, 2.8738, 3.1516, 2.2803]]),
 torch.Size([5, 5]))
"""

element wise mult

x = torch.tensor([1,2,3])
y = torch.tensor([9,8,7])
z = x * y
z
"""
tensor([ 9, 16, 21])
"""

dot product

x = torch.tensor([1,2,3])
y = torch.tensor([9,8,7])
z = torch.dot(x,y)
z
"""
tensor(46)
"""

Batch Matrix Multiplication

batch = 32
n = 10
m = 20
p = 30
tensor1 = torch.rand((batch,n,m))
tensor1.shape
"""
torch.Size([64, 10, 20])
"""
tensor2 = torch.rand((batch,m,p))
tensor2.shape
"""
torch.Size([64, 20, 30])
"""
out_bmm = torch.bmm(tensor1,tensor2)
out_bmm.shape
"""
torch.Size([64, 10, 30])
"""

Example of Broadcasting

x1 = torch.rand((5,5))
x2 = torch.rand((1,5))
z = x1 - x2
z.shape
"""
torch.Size([5, 5])
"""
z = x1 ** x2
z.shape
"""
torch.Size([5, 5])
"""

Other useful tensor operations

x = torch.tensor([[1,5,-3],[-4,2,6]])
y = torch.tensor([[1,-3,5],[4,-6,2]])
sum_0 = torch.sum(x,dim=0)
sum_1 = torch.sum(x,dim=1)
x, sum_0, sum_1
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([-3,  7,  3]),
 tensor([3, 4]))
"""
values_0, indices_0 = torch.max(x, dim=0)
x, values_0, indices_0
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([1, 5, 6]),
 tensor([0, 0, 1]))
"""
values_1, indices_1 = torch.max(x, dim=1)
x, values_1, indices_1
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([5, 6]),
 tensor([1, 2]))
"""
values_0, indices_0 = torch.min(x, dim=0)
x, values_0, indices_0
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([-4,  2, -3]),
 tensor([1, 1, 0]))
"""
values_1, indices_1 = torch.min(x, dim=1)
x, values_1, indices_1
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([-3, -4]),
 tensor([2, 0]))
"""
abs_x = torch.abs(x)
abs_x
"""
tensor([[1, 5, 3],
        [4, 2, 6]])
"""
z_0 = torch.argmax(x,dim=0)
z_1 = torch.argmax(x,dim=1)
x, z_0, z_1
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([0, 0, 1]),
 tensor([1, 2]))
"""
z_0 = torch.argmin(x,dim=0)
z_1 = torch.argmin(x,dim=1)
x, z_0, z_1
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([1, 1, 0]),
 tensor([2, 0]))
"""
mean_x_0 = torch.mean(x.float(),dim=0)
x, mean_x_0
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([-1.5000,  3.5000,  1.5000]))
"""
mean_x_1 = torch.mean(x.float(),dim=1)
x, mean_x_1
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([1.0000, 1.3333]))
"""
z = torch.eq(x,y)
x, y, z
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([[ 1, -3,  5],
         [ 4, -6,  2]]),
 tensor([[ True, False, False],
         [False, False, False]]))
"""
sorted_y0, indices_y0 = torch.sort(y,dim=0,descending=False)
y, sorted_y0, indices_y0
"""
(tensor([[ 1, -3,  5],
         [ 4, -6,  2]]),
 tensor([[ 1, -6,  2],
         [ 4, -3,  5]]),
 tensor([[0, 1, 1],
         [1, 0, 0]]))
"""
sorted_y1, indices_y1 = torch.sort(y,dim=1,descending=False)
y, sorted_y1, indices_y1
"""
(tensor([[ 1, -3,  5],
         [ 4, -6,  2]]),
 tensor([[-3,  1,  5],
         [-6,  2,  4]]),
 tensor([[1, 0, 2],
         [1, 2, 0]]))
"""
sorted_y0, indices_y0 = torch.sort(y,dim=0,descending=True)
y, sorted_y0, indices_y0
"""
(tensor([[ 1, -3,  5],
         [ 4, -6,  2]]),
 tensor([[ 4, -3,  5],
         [ 1, -6,  2]]),
 tensor([[1, 0, 0],
         [0, 1, 1]]))
"""
sorted_y1, indices_y1 = torch.sort(y,dim=1,descending=True)
y, sorted_y1, indices_y1
"""
(tensor([[ 1, -3,  5],
         [ 4, -6,  2]]),
 tensor([[ 5,  1, -3],
         [ 4,  2, -6]]),
 tensor([[2, 0, 1],
         [0, 2, 1]]))
"""
z = torch.clamp(x,min=0)
x, z
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([[1, 5, 0],
         [0, 2, 6]]))
"""
z = torch.clamp(x,max=3)
x, z
"""
(tensor([[ 1,  5, -3],
         [-4,  2,  6]]),
 tensor([[ 1,  3, -3],
         [-4,  2,  3]]))
"""

Tensor Indexing

batch_size = 5
features = 10
x = torch.rand(batch_size,features)
x, x[0].shape, x[0,:], x[:,0].shape, x[:,0]
"""
(tensor([[0.7380, 0.7100, 0.5412, 0.0149, 0.5732, 0.8709, 0.7453, 0.0804, 0.0916,
          0.8293],
         [0.3716, 0.8678, 0.3982, 0.6007, 0.5303, 0.7598, 0.8414, 0.8571, 0.8248,
          0.6621],
         [0.6012, 0.3657, 0.8806, 0.3800, 0.1936, 0.7487, 0.2563, 0.8969, 0.0763,
          0.8819],
         [0.2925, 0.0206, 0.5058, 0.0972, 0.5214, 0.5247, 0.3319, 0.0476, 0.6659,
          0.8648],
         [0.4704, 0.4940, 0.8135, 0.1278, 0.1322, 0.3146, 0.7224, 0.7119, 0.2662,
          0.6529]]),
 torch.Size([10]),
 tensor([0.7380, 0.7100, 0.5412, 0.0149, 0.5732, 0.8709, 0.7453, 0.0804, 0.0916,
         0.8293]),
 torch.Size([5]),
 tensor([0.7380, 0.3716, 0.6012, 0.2925, 0.4704]))
"""
x[2, 0:10]
"""
tensor([0.6012, 0.3657, 0.8806, 0.3800, 0.1936, 0.7487, 0.2563, 0.8969, 0.0763,
        0.8819])
"""
x[0,0] = 100
x[0,0:10]
"""
tensor([1.0000e+02, 7.1004e-01, 5.4120e-01, 1.4874e-02, 5.7325e-01, 8.7089e-01,
        7.4532e-01, 8.0429e-02, 9.1610e-02, 8.2926e-01])
"""

Fancy indexing

x = torch.arange(10)
indices = [2,5,8]
x, x[indices]
"""
(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), tensor([2, 5, 8]))
"""
x = torch.rand((3,5))
rows = torch.tensor([1,0])
cols = torch.tensor([4,0])
x, x[rows,cols], x[rows,cols].shape 
"""
(tensor([[0.4834, 0.7597, 0.8667, 0.5572, 0.7841],
         [0.3627, 0.4910, 0.3195, 0.8789, 0.2608],
         [0.1125, 0.6647, 0.2267, 0.3192, 0.7889]]),
 tensor([0.2608, 0.4834]),
 torch.Size([2]))
"""

More advanced indexing

x = torch.arange(10)
x[(x<1) | (x>8)], x[(x>1) & (x<5)], x[x.remainder(2) == 0] 
"""
(tensor([0, 9]), tensor([2, 3, 4]), tensor([0, 2, 4, 6, 8]))
"""

Userful operations

x = torch.arange(10)
x, torch.where(x>5, x ,x*2)
"""
(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
 tensor([ 0,  2,  4,  6,  8, 10,  6,  7,  8,  9]))
"""
torch.tensor([1,2,2,3,3,3,4,5,0,0,0]).unique()
"""
tensor([0, 1, 2, 3, 4, 5])
"""
x = torch.arange(10)
y = torch.tensor([[1,2,3],[3,2,1]])
x, y, x.ndimension(), y.ndimension() #维度
"""
(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
 tensor([[1, 2, 3],
         [3, 2, 1]]),
 1,
 2)
"""
x, x.numel()
"""
(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 10)
"""

Tensor Reshaping

x = torch.arange(9)
x1 = x.view(3,3)# 张量连续存储在内存中
x2 = x.reshape(3,3)# 更安全,但有性能损失
x, x1, x2
"""
(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8]),
 tensor([[0, 1, 2],
         [3, 4, 5],
         [6, 7, 8]]),
 tensor([[0, 1, 2],
         [3, 4, 5],
         [6, 7, 8]]))
"""
y = x2.t()
x2, y
"""
(tensor([[0, 1, 2],
         [3, 4, 5],
         [6, 7, 8]]),
 tensor([[0, 3, 6],
         [1, 4, 7],
         [2, 5, 8]]))
"""
y.contiguous().view(9)
"""
tensor([0, 3, 6, 1, 4, 7, 2, 5, 8])
"""
x1 = torch.rand((2,5))
x2 = torch.rand((2,5))
x1, x2, torch.cat((x1,x2),dim=0).shape, torch.cat((x1,x2),dim=1).shape
"""
(tensor([[0.7819, 0.1251, 0.3340, 0.9550, 0.7082],
         [0.5150, 0.7210, 0.0744, 0.0373, 0.8516]]),
 tensor([[0.0071, 0.6060, 0.3599, 0.4708, 0.7750],
         [0.4522, 0.7295, 0.5150, 0.6186, 0.7891]]),
 torch.Size([4, 5]),
 torch.Size([2, 10]))
"""
z = x1.view(-1)
z, z.shape
"""
(tensor([0.4683, 0.1770, 0.4593, 0.4702, 0.3166, 0.8451, 0.3698, 0.3247, 0.3582,
         0.1340]),
 torch.Size([10]))
"""
batch = 64
x = torch.rand((batch,2,5))
z = x.view(batch,-1)
z.shape
"""
torch.Size([64, 10])
"""
z = x.permute(0,2,1)
x.shape, z.shape
"""
(torch.Size([64, 2, 5]), torch.Size([64, 5, 2]))
"""
x = torch.arange(10)
x, x.unsqueeze(0), x.unsqueeze(1), x.unsqueeze(0).shape, x.unsqueeze(1).shape
"""
(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
 tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
 tensor([[0],
         [1],
         [2],
         [3],
         [4],
         [5],
         [6],
         [7],
         [8],
         [9]]),
 torch.Size([1, 10]),
 torch.Size([10, 1]))
"""
x = torch.arange(10).unsqueeze(0).unsqueeze(1)
"""
tensor([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]])
"""
x = torch.arange(10).unsqueeze(0).unsqueeze(1)
z = x.squeeze(1)
x, z
"""
(tensor([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]]),
 tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]))
"""

猜你喜欢

转载自blog.csdn.net/qq_41264055/article/details/126621202