参考:https://pytorch-cn.readthedocs.io/zh/latest/package_references/functional/#_1

class torch.nn.Softmax(input, dim)

或:

torch.nn.functional.softmax(input, dim)

对n维输入张量运用Softmax函数,将张量的每个元素缩放到(0,1)区间且和为1。Softmax函数定义如下:

参数:

  dim:指明维度,dim=0表示按列计算;dim=1表示按行计算。默认dim的方法已经弃用了,最好声明dim,否则会警告:

UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.

shape:

  • 输入:(N, L)
  • 输出:(N, L)

返回结果是一个与输入维度dim相同的张量,每个元素的取值范围在(0,1)区间。

例子:

import torch

from torch import nn
from torch import autograd m = nn.Softmax()
input = autograd.Variable(torch.randn(, ))
print(input)
print(m(input))

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
tensor([[ 0.2854, 0.1708, 0.4308],
[-0.1983, 2.0705, 0.1549]])
test.py:: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
print(m(input))
tensor([[0.3281, 0.2926, 0.3794],
[0.0827, 0.7996, 0.1177]])

可见默认按行计算,即dim=1

更明显的例子:

import torch

import torch.nn.functional as F

x= torch.Tensor( [ [,,,],[,,,],[,,,]])

y1= F.softmax(x, dim = ) #对每一列进行softmax
print(y1) y2 = F.softmax(x,dim =) #对每一行进行softmax
print(y2) x1 = torch.Tensor([,,,])
print(x1) y3 = F.softmax(x1,dim=) #一维时使用dim=,使用dim=1报错
print(y3)

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
tensor([[0.3333, 0.3333, 0.3333, 0.3333],
[0.3333, 0.3333, 0.3333, 0.3333],
[0.3333, 0.3333, 0.3333, 0.3333]])
tensor([[0.0321, 0.0871, 0.2369, 0.6439],
[0.0321, 0.0871, 0.2369, 0.6439],
[0.0321, 0.0871, 0.2369, 0.6439]])
tensor([., ., ., .])
tensor([0.0321, 0.0871, 0.2369, 0.6439])

因为列的值相同,所以按列计算时每一个所占的比重都是0.3333;行都是[1,2,3,4],所以按行计算,比重结果都为[0.0321, 0.0871, 0.2369, 0.6439]

一维使用dim=1报错:

RuntimeError: Dimension out of range (expected to be in range of [-, ], but got )

torch.nn.functional中softmax的作用及其参数说明的更多相关文章

  1. 从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系

    从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系 relu多种实现之间的关系 relu 函数在 pytorch 中总共有 3 次出现: torc ...

  2. PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx

    PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 在写 PyTorch 代码时,我们会发现一些功能重复的操作,比如卷积.激活.池化等操作.这些操作分别可 ...

  3. [pytorch笔记] torch.nn vs torch.nn.functional; model.eval() vs torch.no_grad(); nn.Sequential() vs nn.moduleList

    1. torch.nn与torch.nn.functional之间的区别和联系 https://blog.csdn.net/GZHermit/article/details/78730856 nn和n ...

  4. Pytorch本人疑问(1) torch.nn和torch.nn.functional之间的区别

    在写代码时发现我们在定义Model时,有两种定义方法: torch.nn.Conv2d()和torch.nn.functional.conv2d() 那么这两种方法到底有什么区别呢,我们通过下述代码看 ...

  5. pytorch torch.nn.functional实现插值和上采样

    interpolate torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', ali ...

  6. PyTorch官方中文文档:torch.nn

    torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...

  7. Pytorch中pad函数toch.nn.functional.pad()的用法

    padding操作是给图像外围加像素点. 为了实际说明操作过程,这里我们使用一张实际的图片来做一下处理. 这张图片是大小是(256,256),使用pad来给它加上一个黑色的边框.具体代码如下: 1 2 ...

  8. torch.nn 的本质

    torch.nn 的本质 PyTorch 提供了各种优雅设计的 modules 和类 torch.nn,torch.optim,Dataset 和 DataLoader 来帮助你创建并训练神经网络.为 ...

  9. 到底什么是TORCH.NN?

    该教程是在notebook上运行的,而不是脚本,下载notebook文件. PyTorch提供了设计优雅的模块和类:torch.nn, torch.optim, Dataset, DataLoader ...

随机推荐

  1. 关于 Socket 设置 setSoTimeout 误用的说明

    做网络开发的想必对setSoTimeout这个方法很熟悉,知道是设置的超时事件.但是很多人都认为这个是设置链路的超时时间,但是查看相关文档的此方法的说明: HttpConnectionParams: ...

  2. JAVA 多线程(3)

    再讲线程安全: 一.脏读 脏读:在于读字,意在在读取实例变量时,实例变量有可能被另外一个线程更改了,导致获取到的数据出现异常. 在非线程安全的情况下,如果线程A与线程B 共同使用对象实例C中的方法me ...

  3. F5负载的应用IIS日志记录的不是真实IP的处理方法

    如果没有这一项,在服务里添加上 将F5XForwardedFor.dll拷贝到应用目录下 添加筛选器: 名称:F5XForwardedFor 可执行文件:F5XForwardedFor.dll所在的目 ...

  4. Android--小游戏

    GitHub:https://github.com/vinieo/game 功能描述   “猜小球”是一个简单的愉悦身心的小游戏,它的功能结构如图a-1所示. 构建开发环境 在开发本游戏时,首先需要下 ...

  5. Flutter 布局(一)- Container详解

    本文主要介绍Flutter中非常常见的Container,列举了一些实际例子介绍如何使用. 1. 简介 A convenience widget that combines common painti ...

  6. PostgreSQL date_trunc() 和timestamp

    timestamp 01.SELECT now()::timestamp + '1 year'; 02.SELECT now()::timestamp + '1 month'; 03.SELECT n ...

  7. Python爬取网络图片

    代码: import urllib import urllib.request import re #打开网页,下载器 def open_html ( url): require=urllib.req ...

  8. ALTER SYSTEM ARCHIVELOG CURRENT挂起案例

    最近两天,一台ORACLE数据库的作业执行delete_ob_get_epps.sh脚本清理过期备份时,执行下面SQL语句就会被阻塞,在监控工具DPA里面部分截图如下(图片分开截断) sql 'alt ...

  9. XSS攻击介绍

    一.概念 XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安全漏洞, ...

  10. mssql sqlserver 获取指定汉字的笔画数的方法分享

    转自:http://www.maomao365.com/?p=6421 摘要: 下文讲述计算汉字笔画数的sql函数分享,如下所示: 例:建立汉字笔画数sql函数 )) returns int as b ...