torch.sigmoid()、torch.softmax()、sum
1、torch.sigmoid()
对每个元素进行处理(函数为
)
举例:
A = torch.Tensor([1,2,3]) #一维
B = torch.sigmoid(A)
print(B)

A = torch.Tensor([[1,2,3],[1,2,3]]) #二维
B = torch.sigmoid(A)
print(B)

2、torch.softmax()
公式:
二维情况下,dim=1时,对行进行计算
A = torch.Tensor([[1,1],[1,1],[1,3]])
B = torch.softmax(A,dim=1) #对行 进行softmax
print(B)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GIDLbimg-1666254112726)(https://cdn.jsdelivr.net/gh/bean661/images@main/img/202210201552330.png)]
二维情况下,dim=0时,对列进行计算
A = torch.Tensor([[1,1],[1,1],[1,3]])
B = torch.softmax(A,dim=0) #对列 进行softmax
print(B)

3、sum
A = torch.Tensor([[1,2],[3,4],[5,6]])
B = A.sum(dim=0)
print()
([[1,2],[3,4],[5,6]])
B = A.sum(dim=0)
print()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LPNL8qgZ-1666254112727)(https://cdn.jsdelivr.net/gh/bean661/images@main/img/202210201620452.png)]
本文详细介绍了PyTorch中torch.sigmoid()函数和torch.softmax()函数的使用,包括一维和二维数据的处理,并展示了如何对张量进行求和操作。torch.sigmoid()用于对每个元素应用Sigmoid激活函数,而torch.softmax()则根据给定维度计算概率分布。此外,还演示了sum函数在不同维度上对张量求和的方法。
1万+

被折叠的 条评论
为什么被折叠?



