torch.nn.functional中softmax的作用及其参数说明
参考: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的作用及其参数说明的更多相关文章
- 从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系
从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系 relu多种实现之间的关系 relu 函数在 pytorch 中总共有 3 次出现: torc ...
- PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx
PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 在写 PyTorch 代码时,我们会发现一些功能重复的操作,比如卷积.激活.池化等操作.这些操作分别可 ...
- [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 ...
- Pytorch本人疑问(1) torch.nn和torch.nn.functional之间的区别
在写代码时发现我们在定义Model时,有两种定义方法: torch.nn.Conv2d()和torch.nn.functional.conv2d() 那么这两种方法到底有什么区别呢,我们通过下述代码看 ...
- pytorch torch.nn.functional实现插值和上采样
interpolate torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', ali ...
- PyTorch官方中文文档:torch.nn
torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...
- Pytorch中pad函数toch.nn.functional.pad()的用法
padding操作是给图像外围加像素点. 为了实际说明操作过程,这里我们使用一张实际的图片来做一下处理. 这张图片是大小是(256,256),使用pad来给它加上一个黑色的边框.具体代码如下: 1 2 ...
- torch.nn 的本质
torch.nn 的本质 PyTorch 提供了各种优雅设计的 modules 和类 torch.nn,torch.optim,Dataset 和 DataLoader 来帮助你创建并训练神经网络.为 ...
- 到底什么是TORCH.NN?
该教程是在notebook上运行的,而不是脚本,下载notebook文件. PyTorch提供了设计优雅的模块和类:torch.nn, torch.optim, Dataset, DataLoader ...
随机推荐
- 【转录】原来Github上的README.md文件这么有意思——Markdown语言详解
之前一直在使用github,也在上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是默认的README.md文件,也不曾对这个文件有过什么了解.但是在看到别人写的项目的README.md里 ...
- Python 一键commit文件、目录到SVN服务器
一键commit文件.目录到SVN服务器 by:授客 QQ:1033553122 实现功能 1 测试环境 1 代码show 1 实现功能 一键提交文件.目录到svn 测试环境 Win7 64位 ...
- C# 利用ReportViewer生成报表
本文主要是利用微软自带的控件ReportViewer进行报表设计的小例子,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: ReportViewer :位于Microsoft.Reportin ...
- Apktool(3)——Apktool的使用
一.apktool的作用 安卓应用apk文件不仅仅是包含有resource和编译的java代码的zip文件,如果你尝试用解压工具(如好压)解压后,你将会获得classes.dex和resource.a ...
- gitlab runner安装与使用
今天来讲一下如何使用gitlab-runner 下载runner,根据自己对应服务器的型号自行选择下载: # Linux x86- sudo wget -O /usr/local/bin/gitlab ...
- 设计模式java----单例模式
一.懒汉式单例 在第一次调用的时候实例化自己,Singleton的唯一实例只能通过getInstance()方法访问.线程不安全 /** * Created by Admin on 2017/3/19 ...
- java防止double和float精度丢失的方法
在浮点数当中做运算时经常会出现精度丢失的情况,如果做项目不作处理的话会对商家造成很大的影响的.项目尤其是金融相关的项目对这些运算的精度要求较高. 问题原因:首先计算机进行的是二进制运算,我们输入的十进 ...
- Python输出和输入
一.input()函数 在 Python 中,使用内置函数 input()可以接收用户的键盘输入. input()函数的基本用法如 下: variable = input("提示文字") 其中,var ...
- 【转载】failed to initialize nvml driver/library version mismatch ubuntu
英伟达驱动版本是384.130 显示的NVRM version: NVIDIA UNIX x86_64 Kernel Module是:384.130. 若是旧的版本就会出现如下问题. 这个问题出现的原 ...
- June 3. 2018 Week 23rd Sunday
You only get one shot; do not miss your chance to blow. 机会只有一次,不要错过. From Eminem, "Lose Yoursel ...