pytorch 向量计算

向量相关计算

在这里插入图片描述

向量与向量的计算

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())

在这里插入图片描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部