参考: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. 查询文章的上下篇Sql语句

    直接开入正题 文章内容页一般都会有上一篇和下一篇的功能: 那么查询上下篇的sql语句应该怎么写呢:示例数据表:zmd_article自增主键:id当前文章id:10 肯定有人说,这简单啊id+1和id ...

  2. iOS ---------- 获取设备的各种信息

    一.目录结构: 获取屏幕宽度与高度 获取设备版本号 获取iPhone名称 获取app版本号 获取电池电量 获取当前系统名称 获取当前系统版本号 获取通用的唯一识别码UUID 获取当前设备IP 获取总内 ...

  3. Python 文件复制&按目录树结构拷贝&批量删除目录及其子目录下的文件

    文件复制&按目录树结构拷贝&批量删除目录及其子目录下的文件 by:授客 QQ:1033553122 测试环境: Python版本:Python 3.3.2 Win7 64 代码实践 # ...

  4. 打包错误--Error:A problem was found with the configuration of task ':app:packageRelease'.

    解决办法: app目录下的build.gradle文件 将 shrinkResources 的值改为 false 或者直接去掉 shrinkResources true  表示 :打包的时候会去删除一 ...

  5. Android为TV端助力 转载自jguangyou的博客,XML基本属性大全

    android:layout_width 指定组件布局宽度 android:layout_height 指定组件布局高度 android:alpha 设置组件透明度 android:backgroun ...

  6. Multithreading C++ Out of Core Sotring for Massive Data|多线程C++的大规模数据外部排序

    先说一下,这个其实是我为实现PantaRay或者是类似Dreamworks的Out of Core点云GI的技术储备,为大规模点云光线跟踪所准备的第一步.在实际的应用中,int类型会被64bit的ui ...

  7. socket和http的区别

    1.HTTP连接 HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用 ...

  8. Apache Linux下Apache安装步骤

    Apache简介         Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广 ...

  9. pycharm 中按照文档引包方式,引包错误

    * python使用pycharm ide,如果电脑上有多个解释器的,在项目解释器配置的应该是当前使用的解释器: * 可以把当前使用的解释器目录添加到系统环境变量中,这样就不会报错了 另外,如果目录中 ...

  10. ALTER SYSTEM ARCHIVELOG CURRENT挂起案例

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