NJU1healer的个人博客分享 http://blog.sciencenet.cn/u/NJU1healer

博文

torch. nn.Softmax(dim=1)或torch. nn.Softmax(dim=-1)

已有 9413 次阅读 2020-10-23 16:28 |个人分类:Pytorch|系统分类:科研笔记

softmax2 = nn.Softmax(dim=2) #三维数据的最后一维

y=torch.rand(3,2,4)

y

Out[190]: 

tensor([[[0.4634, 0.1223, 0.8533, 0.5247],

         [0.0603, 0.1866, 0.1680, 0.5770]],


        [[0.8840, 0.2867, 0.2902, 0.6421],

         [0.5726, 0.8274, 0.3731, 0.0680]],


        [[0.8073, 0.2921, 0.5206, 0.0273],

         [0.2502, 0.5895, 0.0978, 0.5977]]])


z=softmax2(y)

z

Out[192]: 

tensor([[[0.2352, 0.1673, 0.3474, 0.2501],

         [0.2031, 0.2304, 0.2261, 0.3404]],


        [[0.3463, 0.1906, 0.1912, 0.2719],

         [0.2693, 0.3475, 0.2206, 0.1626]],


        [[0.3563, 0.2129, 0.2675, 0.1633],

         [0.2138, 0.3001, 0.1835, 0.3026]]])


z.sum(0)

Out[193]: 

tensor([[0.9378, 0.5707, 0.8061, 0.6853],

        [0.6861, 0.8780, 0.6303, 0.8056]])


z.sum(1)

Out[194]: 

tensor([[0.4383, 0.3977, 0.5735, 0.5905],

        [0.6156, 0.5380, 0.4118, 0.4345],

        [0.5701, 0.5130, 0.4511, 0.4659]])


z.sum(2)

Out[195]: 

tensor([[1., 1.],

        [1., 1.],

        [1., 1.]])


softmax3 = nn.Softmax(dim=-1#三维数据的最后一维

zz = softmax3(y)

zz.sum(2)

Out[198]: 

tensor([[1., 1.],

        [1., 1.],

        [1., 1.]])

点滴分享,福泽你我!Add oil!



https://wap.sciencenet.cn/blog-3428464-1255499.html

上一篇:pytorch中与维度/变换相关的几个函数(torch.squeeze() / torch.unsqueeze())
下一篇:BatchNorm, LayerNorm, InstanceNorm和GroupNorm总结
收藏 IP: 103.149.249.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的其他最新博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-27 02:26

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部