2.3.1 头文件

import torch

 

2.3.2 标量张量的计算

# 定义两个标量
x = torch.tensor(3.0)
y = torch.tensor(2.0)
# 加法
print(x + y)
# 输出:
# tensor(5.)

# 减法
print(x - y)
# 输出:
# tensor(1.)

# 乘法
print(x * y)
# 输出:
# tensor(6.)

# 除法
print(x / y)
# 输出:
# tensor(1.5000)

# 求幂
print(x ** y)
# 输出:
# tensor(9.)

 

2.3.2 向量张量的计算

# 创建一个向量
x = torch.arange(4)
print(x)
# 输出:
# tensor([0, 1, 2, 3])

# 索引一个元素
print(x[3])
# 输出:
# tensor(3)

# 向量的长度:
print(len(x))
# 输出:
# 4

# 向量的形状
print(x.shape)
# 输出:
# torch.Size([4])

 

2.3.3 矩阵张量的计算

# 创建一个矩阵
A = torch.arange(20).reshape(5, 4)
print(A)
# 输出:
# tensor([[ 0,  1,  2,  3],
#         [ 4,  5,  6,  7],
#         [ 8,  9, 10, 11],
#         [12, 13, 14, 15],
#         [16, 17, 18, 19]])

# 矩阵转置:
print(A.T)
# 输出:
# tensor([[ 0,  4,  8, 12, 16],
#         [ 1,  5,  9, 13, 17],
#         [ 2,  6, 10, 14, 18],
#         [ 3,  7, 11, 15, 19]])

 

2.3.4 张量的计算

# 创建一个二位张量
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
B = A.clone()  # 通过分配新内存,将A的一个副本分配给B
print(A)
# 输出:
# tensor([[ 0.,  1.,  2.,  3.],
#         [ 4.,  5.,  6.,  7.],
#         [ 8.,  9., 10., 11.],
#         [12., 13., 14., 15.],
#         [16., 17., 18., 19.]])

# 张量加法
print(A+B)
# 输出:
# tensor([[ 0.,  2.,  4.,  6.],
#         [ 8., 10., 12., 14.],
#         [16., 18., 20., 22.],
#         [24., 26., 28., 30.],
#         [32., 34., 36., 38.]])

# 张量乘法
print(A * B)
# 输出:
# tensor([[  0.,   1.,   4.,   9.],
#         [ 16.,  25.,  36.,  49.],
#         [ 64.,  81., 100., 121.],
#         [144., 169., 196., 225.],
#         [256., 289., 324., 361.]])

# 张量的线性运算
a = 2
X = torch.arange(12).reshape(3, 4)
print(a * X + a)
# 输出:
# tensor([[ 2,  4,  6,  8],
#         [10, 12, 14, 16],
#         [18, 20, 22, 24]])

 

2.3.5 张量的求和

# 张量中所有元素的求和
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
print(A.sum())
# 输出:
# tensor(190.)

# 张量按列求和
print(A.sum(axis=0))
# 输出:
# tensor([40., 45., 50., 55.])

# 张量按行求和
print(A.sum(axis=1))
# 输出:
# tensor([ 6., 22., 38., 54., 70.])

 

2.3.6 张量的平均值

# 张量中所有元素的求平均值
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
print(A.mean())
# 输出:
# tensor(9.5000)

# 张量按列求平均值
print(A.mean(axis=0))
# 输出:
# tensor([ 8.,  9., 10., 11.])

# 张量按行求平均值
print(A.mean(axis=1))
# 输出:
# tensor([ 1.5000,  5.5000,  9.5000, 13.5000, 17.5000])

# 求张量中每个元素在所在行所占的比例
print(A/A.sum(axis=1, keepdims=True))
# 输出:
# tensor([[0.0000, 0.1667, 0.3333, 0.5000],
#         [0.1818, 0.2273, 0.2727, 0.3182],
#         [0.2105, 0.2368, 0.2632, 0.2895],
#         [0.2222, 0.2407, 0.2593, 0.2778],
#         [0.2286, 0.2429, 0.2571, 0.2714]])

# 求张量中每个元素在所在列所占的比例
print(A/A.sum(axis=0, keepdims=True))
# 输出:
# tensor([[0.0000, 0.0222, 0.0400, 0.0545],
#         [0.1000, 0.1111, 0.1200, 0.1273],
#         [0.2000, 0.2000, 0.2000, 0.2000],
#         [0.3000, 0.2889, 0.2800, 0.2727],
#         [0.4000, 0.3778, 0.3600, 0.3455]])

# 张量从上到下进行累加
print(A.cumsum(axis=0))
# 输出:
# tensor([[ 0.,  1.,  3.,  6.],
#         [ 4.,  9., 15., 22.],
#         [ 8., 17., 27., 38.],
#         [12., 25., 39., 54.],
#         [16., 33., 51., 70.]])

# 张量从左到右进行累加
print(A.cumsum(axis=1))
# 输出:
# tensor([[ 0.,  1.,  3.,  6.],
#         [ 4.,  9., 15., 22.],
#         [ 8., 17., 27., 38.],
#         [12., 25., 39., 54.],
#         [16., 33., 51., 70.]])

 

2.3.7 张量的点积

x = torch.arange(4, dtype=torch.float32)
y = torch.ones(4, dtype = torch.float32)
print(torch.dot(x, y))
# 输出:
# tensor(6.)    逐元素相乘再相加

 

本小节完整代码如下

import torch

# ------------------------------标量张量的计算------------------------------------
# 定义两个标量
x = torch.tensor(3.0)
y = torch.tensor(2.0)
# 加法
print(x + y)
# 输出:
# tensor(5.)

# 减法
print(x - y)
# 输出:
# tensor(1.)

# 乘法
print(x * y)
# 输出:
# tensor(6.)

# 除法
print(x / y)
# 输出:
# tensor(1.5000)

# 求幂
print(x ** y)
# 输出:
# tensor(9.)

# ------------------------------向量张量的计算------------------------------------
# 创建一个向量
x = torch.arange(4)
print(x)
# 输出:
# tensor([0, 1, 2, 3])

# 索引一个元素
print(x[3])
# 输出:
# tensor(3)

# 向量的长度:
print(len(x))
# 输出:
# 4

# 向量的形状
print(x.shape)
# 输出:
# torch.Size([4])

# ------------------------------矩阵张量的计算------------------------------------
# 创建一个矩阵
A = torch.arange(20).reshape(5, 4)
print(A)
# 输出:
# tensor([[ 0,  1,  2,  3],
#         [ 4,  5,  6,  7],
#         [ 8,  9, 10, 11],
#         [12, 13, 14, 15],
#         [16, 17, 18, 19]])

# 矩阵转置:
print(A.T)
# 输出:
# tensor([[ 0,  4,  8, 12, 16],
#         [ 1,  5,  9, 13, 17],
#         [ 2,  6, 10, 14, 18],
#         [ 3,  7, 11, 15, 19]])


# ------------------------------张量的计算------------------------------------
# 创建一个二位张量
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
B = A.clone()  # 通过分配新内存,将A的一个副本分配给B
print(A)
# 输出:
# tensor([[ 0.,  1.,  2.,  3.],
#         [ 4.,  5.,  6.,  7.],
#         [ 8.,  9., 10., 11.],
#         [12., 13., 14., 15.],
#         [16., 17., 18., 19.]])

# 张量加法
print(A+B)
# 输出:
# tensor([[ 0.,  2.,  4.,  6.],
#         [ 8., 10., 12., 14.],
#         [16., 18., 20., 22.],
#         [24., 26., 28., 30.],
#         [32., 34., 36., 38.]])

# 张量乘法
print(A * B)
# 输出:
# tensor([[  0.,   1.,   4.,   9.],
#         [ 16.,  25.,  36.,  49.],
#         [ 64.,  81., 100., 121.],
#         [144., 169., 196., 225.],
#         [256., 289., 324., 361.]])

# 张量的线性运算
a = 2
X = torch.arange(12).reshape(3, 4)
print(a * X + a)
# 输出:
# tensor([[ 2,  4,  6,  8],
#         [10, 12, 14, 16],
#         [18, 20, 22, 24]])

# ------------------------------张量的求和------------------------------------
# 张量中所有元素的求和
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
print(A.sum())
# 输出:
# tensor(190.)

# 张量按列求和
print(A.sum(axis=0))
# 输出:
# tensor([40., 45., 50., 55.])

# 张量按行求和
print(A.sum(axis=1))
# 输出:
# tensor([ 6., 22., 38., 54., 70.])

# ------------------------------张量的平均值------------------------------------
# 张量中所有元素的求平均值
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
print(A.mean())
# 输出:
# tensor(9.5000)

# 张量按列求平均值
print(A.mean(axis=0))
# 输出:
# tensor([ 8.,  9., 10., 11.])

# 张量按行求平均值
print(A.mean(axis=1))
# 输出:
# tensor([ 1.5000,  5.5000,  9.5000, 13.5000, 17.5000])

# 求张量中每个元素在所在行所占的比例
print(A/A.sum(axis=1, keepdims=True))
# 输出:
# tensor([[0.0000, 0.1667, 0.3333, 0.5000],
#         [0.1818, 0.2273, 0.2727, 0.3182],
#         [0.2105, 0.2368, 0.2632, 0.2895],
#         [0.2222, 0.2407, 0.2593, 0.2778],
#         [0.2286, 0.2429, 0.2571, 0.2714]])

# 求张量中每个元素在所在列所占的比例
print(A/A.sum(axis=0, keepdims=True))
# 输出:
# tensor([[0.0000, 0.0222, 0.0400, 0.0545],
#         [0.1000, 0.1111, 0.1200, 0.1273],
#         [0.2000, 0.2000, 0.2000, 0.2000],
#         [0.3000, 0.2889, 0.2800, 0.2727],
#         [0.4000, 0.3778, 0.3600, 0.3455]])

# 张量从上到下进行累加
print(A.cumsum(axis=0))
# 输出:
# tensor([[ 0.,  1.,  3.,  6.],
#         [ 4.,  9., 15., 22.],
#         [ 8., 17., 27., 38.],
#         [12., 25., 39., 54.],
#         [16., 33., 51., 70.]])

# 张量从左到右进行累加
print(A.cumsum(axis=1))
# 输出:
# tensor([[ 0.,  1.,  3.,  6.],
#         [ 4.,  9., 15., 22.],
#         [ 8., 17., 27., 38.],
#         [12., 25., 39., 54.],
#         [16., 33., 51., 70.]])

# ------------------------------张量的点积------------------------------------
x = torch.arange(4, dtype=torch.float32)
y = torch.ones(4, dtype = torch.float32)
print(torch.dot(x, y))
# 输出:
# tensor(6.)    逐元素相乘再相加

 

posted on 2022-11-01 14:34  yc-limitless  阅读(35)  评论(0编辑  收藏  举报