# -*- coding: utf-8 -*-
"""
Created on Mon May 27 11:09:52 2019 @author: jiangshan
""" import torch
import numpy
import torch.nn.functional as F x1= torch.Tensor( [[1,2,3,4],[1,3,4,5],[3,4,5,6]])
print(x1) import math
#将torch.Tensor转换成numpy
x1_num = x1.numpy()
print(x1_num)
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 = 0
for 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_j
Row_softmax[i,j] = out_j
print(out_j)
print('=====row-%d-end====='%(i+1))
print(Row_softmax)
y12 = F.softmax(x1,dim = 1) #对每一行进行softmax --- dim = 1轴
print(y12)
y120 = F.softmax(x1,dim = -1) #对每一行进行softmax --- dim = -1
print(y120) #对每一列进行softmax
for j in range(c):
sum_i = 0
for 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_i
print(out_i)
print('=====col-%d-end====='%(j+1))
print(Clo_softmax)
y11= F.softmax(x1, dim = 0) #对每一列进行softmax ---- dim = 0轴
print(y11)
print('=================================================') # 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('=================================================') # 2 维张量
x3 = torch.Tensor([[1],[1],[3]])
print(x3)
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)

  解读如下:

# -*- coding: utf-8 -*-
"""
Created on Mon May 27 11:09:52 2019 @author: jiangshan
""" import torch
import numpy
import torch.nn.functional as F x1= 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 = 0
for 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_j
Row_softmax[i,j] = out_j
print(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 = 0
for 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_i
print(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 取值测试及验证的更多相关文章

  1. 在LoadRunner中从数组类型的参数随机取值的方法

    在LoadRunner中从数组类型的参数随机取值的方法 使用web_reg_save_param做关联后,有时候会有多个匹配值. 为了模仿用户行为随机取一个值为后续transcation所用,可以使用 ...

  2. php的form中元素name属性相同时的取值问题

    php的form中元素name属性相同时的取值问题:修改元素的名称,在名称后面加上 '[]',然后取值时即可得array()数组. 一.以复选框为例: <html> <head> ...

  3. 理解pytorch中的softmax中的dim参数

    import torch import torch.nn.functional as F x1= torch.Tensor( [ [1,2,3,4],[1,3,4,5],[3,4,5,6]]) y11 ...

  4. Java 中日期的几种常见操作 —— 取值、转换、加减、比较

    Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿.当然,我只提 ...

  5. Gridview中DataKeyNames 设置多个主键 取值

    1.设置DataKeyNames a.F4  在属性面板中设置   多个值以逗号隔开  例如id,mane,sex b.通过后台代码 this.gridview.DataSource = Bind() ...

  6. Handlebars.js中集合(list)通过中括号的方式取值

    有这么一个需求,在一个table中,tr是通过each取值,取出的值要与table标题相对应,如何实现?例如: <table> <thead> <tr> {{#ea ...

  7. 聊聊 Java 中日期的几种常见操作 —— 取值、转换、加减、比较

    Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿.当然,我只提 ...

  8. jmeter ——JDBC Request中从数据库中读两个字段给接口取值

    前置条件数据库: 给接口传:tid和shopid这俩字段 直接从JDBC Request开始: Variable name:这里写入数据库连接池的名字(和JDBC Connection Configu ...

  9. PyQt(Python+Qt)学习随笔:Model/View中的枚举类 Qt.MatchFlag的取值及含义

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 枚举类 Qt.MatchFlag描述在模型中搜索项时可以使用的匹配类型,它可以在QStandardI ...

随机推荐

  1. 洛谷P3119草鉴定

    题目 草鉴定,tarjan可以用来缩点,优化spfa的时间, 缩点之后就是一个\(DAG\)了,因此完全可以用来跑spfa上的最长路,然后枚举每条边,查看是否这条边的两个节点分别可以到达起点所在的强连 ...

  2. windows下安装redis、celery,并启动测试

    Windows 环境下基于 Redis 的 Celery 任务调度模块的实现 搭建环境: Windows-x64 10 Celery 3.1.23 Celery-with-redis 3.0 Redi ...

  3. 一个Maven项目在eclipse中正常,但在IDEA中启动时报错

    这个项目十有八九最初是在ecplise创建的,框架上十有八九整合了Mybatis,报的错误十有八九是 org.apache.ibatis.binding.BindingException: Inval ...

  4. codeforces37C

    CF37C Old Berland Language   sol:直接暴力模拟下去,长度加了就补0,凑个数就+1,凑不好就puts(“no”) #include <bits/stdc++.h&g ...

  5. Python豆瓣书籍信息爬虫

    练习下BeautifulSoup,requests库,用python3.3 写了一个简易的豆瓣小爬虫,将爬取的信息在控制台输出并且写入文件中. 上源码: # coding = utf-8 '''my ...

  6. Java枚举抽象方法实战

    需求背景 需求已经确定了几个固定的常量值,并且每个常量值都有相同的行为,但是具体实现细节不同.建议使用枚举抽象方法,优点:结构清晰,便于扩展. 枚举类实现抽象方法 与常规抽象类一样,enum类允许我们 ...

  7. 请解释一下 JavaScript 的同源策略

    概念: 同源策略是客户端脚本(尤其是Netscape Navigator2.0,其目的是防止某个文档或脚本从多个不同源装载. 这里的同源策略指的是:协议,域名,端口相同,同源策略是一种安全协议. 指一 ...

  8. openrstry 限流 是否有清零逻辑

    openrstry  限流  是否有清零逻辑 https://github.com/openresty/lua-resty-limit-traffic

  9. Java并发包线程池之ForkJoinPool即ForkJoin框架(二)

    前言 前面介绍了ForkJoinPool相关的两个类ForkJoinTask.ForkJoinWorkerThread,现在开始了解ForkJoinPool.ForkJoinPool也是实现了Exec ...

  10. ps 快捷键大全

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)矩形.椭圆选框工具 [M]移动工具 [V]套索.多边形套索.磁性套索 [L]魔棒工具 [W]裁剪工具 [C]切片工具.切片选择 ...