二、Softmax函数

Softmax函数

代码:

import torch
input = torch.randn(2,2,3,3) # 生成随机张量
print("input",input)
print("input.size",input.size())
output = torch.nn.Softmax(dim=1)(input) # 沿着维度1进行Softmax计算
print("output", output)
print("output.size", output.size())

结果:

input tensor([[[[-1.0284,  0.3340,  0.8394],
                [ 2.0868, -0.4440,  1.1711],
                [-0.4021, -1.5332,  1.3740]],

               [[-1.7498, -2.3110, -0.9069],
                [ 1.3210, -2.1890,  0.8349],
                [-0.0612,  0.3885, -1.3221]]],

              [[[ 0.9202,  0.4145,  0.5510],
                [-1.2614,  1.1752, -0.7427],
                [ 0.1773, -0.1683, -0.4466]],

               [[ 1.7270,  0.7876, -0.0378],
                [ 1.1749,  0.1121, -0.0742],
                [-1.4793, -0.5104,  1.9719]]]])
                
input.size torch.Size([2, 2, 3, 3])

output tensor([[[[0.6729, 0.9337, 0.8515],
                 [0.6826, 0.8513, 0.5833],
                 [0.4156, 0.1277, 0.9368]],

                [[0.3271, 0.0663, 0.1485],
                 [0.3174, 0.1487, 0.4167],
                 [0.5844, 0.8723, 0.0632]]],


               [[[0.3086, 0.4078, 0.6431],
                 [0.0804, 0.7433, 0.3388],
                 [0.8398, 0.5847, 0.0818]],

                [[0.6914, 0.5922, 0.3569],
                 [0.9196, 0.2567, 0.6612],
                 [0.1602, 0.4153, 0.9182]]]])
                 
output.size torch.Size([2, 2, 3, 3]) # 维度1之和为1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值