运算
(1)torch.nn.functional.softmax(input, dim)
dim:指明维度,dim=0表示按列计算;dim=1表示按行计算。默认dim的方法已经弃用了,最好声明dim
import torch
x = torch.randn(3, 4)
y=torch.nn.functional.softmax(x, dim=0)
print(x)
print(y)
(2)张量的点乘,即哈达马积,torch.mul()
# 张量的点乘,即哈达马积,torch.mul()
x=torch.tensor([[1,2],
[3,4]])
y=torch.tensor([[1,2],
[3,4]])
print(torch.mul(x,y))
(3)矩阵的乘法,torch.mm(),遵循矩阵乘的规则,不能用于3Dtensor和2D的tensor的矩阵乘
x=torch.tensor([[1,2],
[3,4]])
y=torch.tensor([[1,2],
[3,4]])
print(torch.mm(x,y))
(4)tensor.matmul(),tensor的矩阵乘,可用用于不同维度的tensot的矩阵乘
import torch
x=torch.tensor([[[1,2,3],
[2,3,1]],
[[1,2,3],
[2,3,1]]])
y=torch.tensor([[2],
[1],
[1]])
print(torch.matmul(x,y))
outpuy
tensor([[[7],
[8]],
[[7],
[8]]])
(5)torch.bmm(),3Dtensor的矩阵乘,两个操作的tensor必须是3Dtensor
import torch
x=torch.tensor([[[1,2,3],
[2,3,1]],
[[1,2,3],
[2,3,1]]])
y=torch.tensor([[[1,2,3],
[2,3,1],
[2,3,1]],
[[1,2,3],
[2,3,1],
[2,3,1]]])
print(torch.bmm(x,y))
output
tensor([[[11, 17, 8],
[10, 16, 10]],
[[11, 17, 8],
[10, 16, 10]]])