pytorch 向量计算

向量相关计算 向量与向量的计算 x = torch.arange(4, dtype=torch.float32)y = torch.ones(4, dtyp

向量相关计算

在这里插入图片描述

向量与向量的计算

x = torch.arange(4, dtype=torch.float32)
y = torch.ones(4, dtype=torch.float32) * 2
print(x)
print(y)

在这里插入图片描述

# 向量与向量计算
print(x + y)
print(x * y)
print(x / y)
print(x ** y)

在这里插入图片描述

# 向量点乘
print(y.dot(x))
print(torch.sum(x * y))

在这里插入图片描述

向量与矩阵

# 向量与矩阵
M = torch.arange(12, dtype=torch.float32).reshape(3, 4)
V = torch.arange(4, dtype=torch.float32) + 1print(M)
print(V)print(M + V)
print(M * V)
print(M / V)
print(M ** V)mv = torch.mv(M, V)
print(mv)

在这里插入图片描述

向量的范数

# 范数
# L2范数
u = torch.tensor([3.0, -4.0])
print(torch.norm(u))# L1范数
print(torch.abs(u).sum())

在这里插入图片描述