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 ...
随机推荐
- leetcode解题报告(15):Third Maximum Number
描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...
- 四川大学第二届SCUACM新生赛(同步赛)题解
周末没事干,就不要脸地去一边吃饭一边看学弟沈阳拿银一边水了个比赛,水都水了,简单写个题解. 比赛链接 A,丁姐姐喜欢Fibonacci.签到1,斐波那契%3 1 1 0 1 1 0 1 1 0..., ...
- 4、wordcount程序原理剖析及Spark架构原理
一.wordcount程序原理深度剖析 二.Spark架构原理 1.
- NodeJs的Event Loop
我们之前谈过浏览器的Event Loop:https://www.cnblogs.com/amiezhang/p/11349450.html 简单来说,就是每执行一个宏任务,就去执行微任务队列,直到清 ...
- [bzoj 4566][Haoi 2016]找相同字符
传送门 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. Solution 考虑用SAM,采用较为轻 ...
- HNOI2009有趣的数列
首先next_permutation打表,发现Cat规律. 其实考试的时候这么做没什么问题,而且可以节省异常多的时间,那么现在我们来想一下why. 首先我拿模型法解释一下,我们把2n个数看成2n个人, ...
- 【代码】python 绘图(以faster rcnn 的 loss 曲线为例)
# -*- coding=utf-8 -*-''' import matplotlib.pyplot as plt import re logs=open('loss').read() #print ...
- Kettle中ETL的效率优化
ETL效率优化 开启数据库日志记录及性能监控 如果我们想要优化一个ETL(KTR或者KJB)的性能,我们首先需要知道的就是它的瓶颈在哪里.而这些信息一般只能在ETL运行的步骤度量中看到,并且是不会持久 ...
- HTML中调用带有SoapHeader头的WebService的两种方法
第一种: function CallWebMethodWithHeader() { var soapXML = "<soap:Envelope xmlns:xsi='http://ww ...
- [IMX6DL] CPU频率调节模式以及降频方法
本文转自http://blog.csdn.net/kris_fei/article/details/51822435 Kernel branch: 3.0.35 CPU的频率调节模式:1. Perfo ...