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 ...
随机推荐
- Java常用类、集合框架类1
A 时间日期格式转换(SDUT 2246)(SimpleDateFormat用法) 转换的df2里面时间是US,上面的df1可以是CHINA或者US. package test; import j ...
- [报错解决] "MySQL server has gone away" 解决思路
大概的4个思路 1.超时,超时的阀值有wait_timeout这个参数控制 2.连接被人为的kill 3.发送的SQL语句过大超过max_allowed_packet的大小. (操作的sql语句太长了 ...
- 判断List<E>内是否有重复对象
主要用到Java 8的Stream类 long distinctedSize = list.stream().distinct().count(); boolean hasRepeat = list. ...
- 激活 phpstorm2019.1 win10
首先添加以下内容到c:\windows\system32\drivers\etc\hosts 文件 0.0.0.0 account.jetbrains.com 0.0.0.0 www.jetbrain ...
- PHP开发高可用高安全App后端☆
第1章 本章先讲解课程所含技术点,并演示相关的项目,让小伙伴对课程有个初步的认知,然后再带领小伙伴进行功能的分析,表的ER总关系图 第2章本章主要讲解课程的一些准备工作知识.包括工具.环境.模板等. ...
- excel中在某一列上的所有单元格的前后增加
excel中在某一列上的所有单元格的前后增加数字汉字字符等东西的函数这样写 “东西”&哪一列&“东西” 例如 “1111”&E1&“3333”
- meshing-做类似ICEM的Y型剖分
原视频下载地址:https://yunpan.cn/cqjeKkrhwwN3x 访问密码 c724
- C# ASP.NET 控制windows服务的 开启和关闭 以及重启
用ASP.NET控制Windows服务的开启与关闭效果如图 代码 首页页面需要添加引用 页面的pageload中 实例化windows服务 protected void Page_Load(objec ...
- Mac地址转换成long长整型
Mac地址转换成long长整型 using System;using System.Collections.Generic;using System.IO;using System.Text;usin ...
- python 了解一点属性的延迟计算
写在前面 本以为百度搜索这类知识的文章应该有很多, 然后我看了前面几篇后,基本上都是类似的内容,我想找些与众不同的博客看下,来拖宽这方面的广度,我就随机点到了第10页,结果第10页的内容基本跟属性的延 ...