pytorch中F.softmax(x1,dim = -1) dim 取值测试及验证
# -*- 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 取值测试及验证的更多相关文章
- 在LoadRunner中从数组类型的参数随机取值的方法
在LoadRunner中从数组类型的参数随机取值的方法 使用web_reg_save_param做关联后,有时候会有多个匹配值. 为了模仿用户行为随机取一个值为后续transcation所用,可以使用 ...
- php的form中元素name属性相同时的取值问题
php的form中元素name属性相同时的取值问题:修改元素的名称,在名称后面加上 '[]',然后取值时即可得array()数组. 一.以复选框为例: <html> <head> ...
- 理解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 ...
- Java 中日期的几种常见操作 —— 取值、转换、加减、比较
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿.当然,我只提 ...
- Gridview中DataKeyNames 设置多个主键 取值
1.设置DataKeyNames a.F4 在属性面板中设置 多个值以逗号隔开 例如id,mane,sex b.通过后台代码 this.gridview.DataSource = Bind() ...
- Handlebars.js中集合(list)通过中括号的方式取值
有这么一个需求,在一个table中,tr是通过each取值,取出的值要与table标题相对应,如何实现?例如: <table> <thead> <tr> {{#ea ...
- 聊聊 Java 中日期的几种常见操作 —— 取值、转换、加减、比较
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿.当然,我只提 ...
- jmeter ——JDBC Request中从数据库中读两个字段给接口取值
前置条件数据库: 给接口传:tid和shopid这俩字段 直接从JDBC Request开始: Variable name:这里写入数据库连接池的名字(和JDBC Connection Configu ...
- PyQt(Python+Qt)学习随笔:Model/View中的枚举类 Qt.MatchFlag的取值及含义
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 枚举类 Qt.MatchFlag描述在模型中搜索项时可以使用的匹配类型,它可以在QStandardI ...
随机推荐
- The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest
目录 Contest Info Solutions A. Attack B. Polynomial E. Interesting Trip F. Sequence G. Winner H. Anoth ...
- linux中清理旧内核
执行update的时候会自动升级内核,开机启动的时候会好多内核选项.所以我们要清理不需要内核. 查看当前系统使用的内核版本 uname -a Linux localhost.localdomain 3 ...
- python 最小二乘 leastsq 函数实现 法线式 解决与x轴垂直问题
当使用y=kx+b时,与x轴垂直的直线无法计算.因此使用法线式ysin(theta)+xcos(theta) = dist.貌似这么用有点复杂了,直接使用ax+by=1不知道能不能计算,未测试. # ...
- c++ ros 计算两点距离
#include <iostream> /* puts, printf */ #include <time.h> /* time_t, struct tm, time, loc ...
- 基于Python的GMSSL实现
基于Python的GMSSL实现 团队任务 一.小组讨论对课程设计任务的理解 基于Python的GMSSL实现,即GmSSL开源加密包的python实现,支持其SM2/SM3/SM4等国密(国家商用密 ...
- Spring boot 集成Solr
首先安装Solr 集成 ikanalyzer ,可以参考 https://www.cnblogs.com/lick468/p/10867492.html https://www.cnblogs.com ...
- js中的一些隐式转换和总结
js中的不同的数据类型之间的比较转换规则如下: 1. 对象和布尔值比较 对象和布尔值进行比较时,对象先转换为字符串,然后再转换为数字,布尔值直接转换为数字 [] == true; //false [] ...
- sklearn中的弹性网函数 ElasticNet
语法: ElasticNet(self, alpha=1.0, l1_ratio=0.5, fit_intercept=True, normalize=False, precompute=False ...
- PHP fopen/file_get_contents与curl性能比较
PHP中fopen,file_get_contents,curl 函数的区别: 1.fopen/file_get_contents 每次请求都会重新做 DNS 查询,并不对 DNS 信息进行缓存. 但 ...
- web前端之es6对象的扩展
1.属性的简洁表示法 2.属性名表达式 表达式作为对象的属性名 3.方法的 name 属性 例如:函数的name 属性,返回函数名. 4.Object.is() ES 比较两个值是否相等,只有两个运算 ...