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

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.]])

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

## 全部精选博文导读

GMT+8, 2024-2-25 04:33