一、代码举例说明

总结

  • 一维张量:dim=0和-1结果一样
  • 二维张量:dim=1和-1一样
# -*- coding: utf-8 -*-
"""
Created on Mon May 27 11:09:52 2019@author: jiangshan
"""import torch
import numpy
import torch.nn.functional as Fx1= torch.Tensor( [[1,2,3,4],[1,3,4,5],[3,4,5,6]])
print(x1)
>>
tensor([[1., 2., 3., 4.],[1., 3., 4., 5.],[3., 4., 5., 6.]])import math
#将torch.Tensor转换成numpy
x1_num = x1.numpy()
print(x1_num)
>>
[[1. 2. 3. 4.][1. 3. 4. 5.][3. 4. 5. 6.]]r,c = x1_num.shape
Row_softmax = numpy.ndarray((r, c), dtype=numpy.float64)
Clo_softmax = numpy.ndarray((r, c), dtype=numpy.float64)
#对每一行进行softmax
for i in range(r):sum_j = 0for j in range(c):ins = x1_num[i,j]sum_j += math.exp(ins)for j in range(c):out_j = math.exp(x1_num[i,j])/sum_jRow_softmax[i,j] = out_jprint(out_j)print('=====row-%d-end====='%(i+1))
print(Row_softmax)
>>
0.03205860328008499
0.08714431874203257
0.23688281808991013
0.6439142598879722
=====row-1-end=====
0.01203764271193945
0.0889468172974043
0.24178251715880075
0.6572330228318555
=====row-2-end=====
0.03205860328008499
0.08714431874203256
0.23688281808991013
0.6439142598879724
=====row-3-end=====
[[0.0320586  0.08714432 0.23688282 0.64391426][0.01203764 0.08894682 0.24178252 0.65723302][0.0320586  0.08714432 0.23688282 0.64391426]]y12 = F.softmax(x1,dim = 1) #对每一行进行softmax --- dim = 1轴
print(y12)
y120 = F.softmax(x1,dim = -1) #对每一行进行softmax --- dim = -1
print(y120)
>>
tensor([[0.0321, 0.0871, 0.2369, 0.6439],[0.0120, 0.0889, 0.2418, 0.6572],[0.0321, 0.0871, 0.2369, 0.6439]])
tensor([[0.0321, 0.0871, 0.2369, 0.6439],[0.0120, 0.0889, 0.2418, 0.6572],[0.0321, 0.0871, 0.2369, 0.6439]])#对每一列进行softmax
for j in range(c):sum_i = 0for i in range(r):ins = x1_num[i,j]sum_i += math.exp(ins)for i in range(r): out_i = math.exp(x1_num[i,j])/sum_i Clo_softmax[i,j] = out_iprint(out_i)print('=====col-%d-end====='%(j+1))
print(Clo_softmax)
>>
0.10650697891920075
0.10650697891920075
0.7869860421615985
=====col-1-end=====
0.09003057317038046
0.24472847105479767
0.6652409557748219
=====col-2-end=====
0.09003057317038046
0.24472847105479764
0.6652409557748219
=====col-3-end=====
0.09003057317038045
0.24472847105479764
0.6652409557748219
=====col-4-end=====
[[0.10650698 0.09003057 0.09003057 0.09003057][0.10650698 0.24472847 0.24472847 0.24472847][0.78698604 0.66524096 0.66524096 0.66524096]]y11= F.softmax(x1, dim = 0) #对每一列进行softmax ---- dim = 0轴
print(y11)
print('=================================================')
>>
tensor([[0.1065, 0.0900, 0.0900, 0.0900],[0.1065, 0.2447, 0.2447, 0.2447],[0.7870, 0.6652, 0.6652, 0.6652]])
=================================================# 1 维张量
x2 = torch.Tensor([1,2,3,4])
print(x2)
y2 = F.softmax(x2,dim=0) #对每一列进行softmax ---- dim = 0轴
print(y2)
y20 = F.softmax(x2,dim=-1)
print(y20)
print('=================================================')
>>
tensor([1., 2., 3., 4.])
tensor([0.0321, 0.0871, 0.2369, 0.6439])
tensor([0.0321, 0.0871, 0.2369, 0.6439])
=================================================# 2 维张量
x3 = torch.Tensor([[1],[1],[3]])
print(x3)
>>
tensor([[1.],[1.],[3.]])y3 = F.softmax(x3,dim=1)#对每一行进行softmax --- dim = 1轴
print(y3)
y31 = F.softmax(x3,dim=0) #对每一列进行softmax ---- dim = 0轴
print(y31)
y30 = F.softmax(x3,dim=-1)
print(y30)
>>
tensor([[1.],[1.],[1.]])
tensor([[0.1065],[0.1065],[0.7870]])
tensor([[1.],[1.],[1.]])

二、参考

pytorch中F.softmax(x1,dim = -1) dim 取值测试及验证

Python-F.softmax(dim)中dim使用理解相关推荐

  1. python 生成器表达式_Python中的列表理解与生成器表达式

    python 生成器表达式 The list is a collection of different types of elements and there are many ways of cre ...

  2. python callable对象_Python中callable的理解?

    >Python has a more general concept of callable object, that is every object that can be called, w ...

  3. python f string slash_python-django中的APPEND_SLASH实现

    关于django中的APPEND_SLASH APPEND_SLASH 它是啥? 看变量名大概能知道做什么,就是添加斜线,用路由系统那里. 路由文件,只写了路由关系代码 ...... urlpatte ...

  4. 【python+ROS+路径规划】二、理解并处理地图数据

    目前打算使用python写出一个Astar的全局路径算法,总体分为三个部分:接收地图数据,设计路径(当然是顺过来的),发布路径. 文章目录 一.建立功能包 二.接受地图数据(处理上游) 查看地图发布的 ...

  5. F.softmax函数dim解读

    F.softmax(score, dim=1) dim=1就是对score矩阵中 所有第1维下标不同,其他维下标均相同的元素进行操作(softmax) 比如a[0][8][15]和a[7][8][15 ...

  6. Pytorch中tensor维度和torch.max()函数中dim参数的理解

    Pytorch中tensor维度和torch.max()函数中dim参数的理解 维度 参考了 https://blog.csdn.net/qq_41375609/article/details/106 ...

  7. python 类中定义列表_Python-从类定义中的列表理解访问类变量

    小编典典 类范围和列表,集合或字典的理解以及生成器表达式不混合. 为什么:或者,官方用词 在Python 3中,为列表理解赋予了它们自己的适当范围(本地名称空间),以防止其局部变量渗入周围的范围内(即 ...

  8. python中 r是什么意思_python中rb含义理解

    Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 文件使用方式标识 'r':默认值,表示从文件读取数据 'w':表示要向文件写入数据,并截断以前 ...

  9. python中result的用法_关于Python中的列表理解及用法

    在Python中,列表理解通常用于编写单行语句,这些语句通过可迭代对象进行迭代以创建新的列表或字典.本文首先介绍for循环如何在Python中工作,然后解释如何在Python中使用列表理解. Pyth ...

  10. python中raw是什么意思_对于Python中RawString的理解

    对于Python中RawString的理解 发布于 2016-01-11 06:56:27 | 100 次阅读 | 评论: 0 | 来源: PHPERZ Python编程语言Python 是一种面向对 ...

最新文章

  1. Python 常用 PEP8 编码规范和建议
  2. 密码学系列之:twofish对称密钥分组算法
  3. 雷军宣布红米 Redmi 品牌独立,这对小米意味着什么?
  4. 打破牢笼,展望更高层次的世界
  5. static--静态方法与静态成员
  6. 【python笔记】:python面向对象实现学生管理系统
  7. 【渝粤教育】国家开放大学2018年秋季 1323T内科护理学(本) 参考试题
  8. iOS 警告收录及科学快速的消除方法
  9. Android WorldWind的使用与添加Geoserver影像(一)
  10. 移动平台课程设计--日记本
  11. html中显示框框中对勾,word对号怎么打 word怎么设置在方框里打对号
  12. 提高情商,从这几方面做
  13. Simpson积分应用
  14. 查找并删除EXCEL文件中的重复行(整行重复)
  15. mac 卸载php版本,mac 系统下删除旧的php版本安装最新的php版本及Xdebug
  16. 拍照相册和裁剪保存图片集合
  17. 有服务器风扇声音对胎儿有影响吗,怀孕期间长时间噪音对胎儿的影响有哪些
  18. 工具条Toolstrip应用
  19. 互联网摸鱼日报(2022-12-26)
  20. WAS以及weblogic日志说明

热门文章

  1. c语言坐标三角形判断,C语言输入三角形边长判断其类型并输出面积实例代码
  2. 「云原生上云」后的聚石塔是如何应对 双11 下大规模应用挑战的
  3. macOS Sierra,Xcode 8配置openCV3
  4. Ubuntu 8.04中文智能拼音输入法
  5. 「经济/商学/理财」简说
  6. 搞副业被领导发现了,让我要么停止,要么滚蛋!
  7. 【夜读】自我提升的8个好习惯,迷茫时看一看
  8. 串口服务器调试助手使用教程,串口调试助手使用教程【操作方式】
  9. 配置Docker镜像加速器
  10. 基于jsp+mysql+Spring+mybatis的SSM健身房管理系统