pytorch中tf.nn.functional.softmax(x,dim = -1)对参数dim的理解

本文深入解析PyTorch中softmax函数的dim参数作用,通过实例演示不同维度下softmax运算的具体效果,帮助读者掌握softmax在神经网络中的应用。

torch.nn.functional.Softmax(input,dim=None)

tf.nn.functional.softmax(x,dim = -1)中的参数dim是指维度的意思,设置这个参数时会遇到0,1,2,-1等情况,特别是对2和-1不熟悉,细究了一下这个问题
查了一下API手册,是指最后一行的意思。原文:
dim (python:int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).

softmax的公式为:
Softmax(xi)=exp(xi)∑jxiSoftmax(x_i)=\frac{exp(x_i)}{\sum_{j}x_i}Softmax(xi)=jxiexp(xi)

一般会有设置成dim=0,1,2,-1的情况

准备工作:先随机生成一个(2,2,3)的矩阵,两个维度的(2,3)矩阵。

import torch
import torch.nn.functional as F
input = torch.randn(2,2,3)
print(input)

输出为:

tensor([[[-3.9332,  0.7909,  0.8927],
         [-1.7991,  0.2505,  0.7695]],

        [[ 0.1946,  0.1878,  1.2713],
         [ 0.9536,  1.0525, -0.7081]]])
  • 要注意的是当dim=0时, 是对每一维度相同位置的数值进行softmax运算,举个栗子:
m = F.softmax(input,</
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值