Softmax函数公式

Softmax的作用简单的说就计算一组数值中每个值的占比

import torch
import torch.nn.functional as F # 原始数据tensor
y = torch.rand(size=[2, 3, 4])
print(y, '\n')
tensor([[[0.6898, 0.0193, 0.0913, 0.9597],
[0.2965, 0.6402, 0.3175, 0.2141],
[0.6842, 0.6477, 0.1265, 0.2181]], [[0.7287, 0.9654, 0.8608, 0.1618],
[0.4583, 0.4862, 0.3352, 0.1108],
[0.1539, 0.0863, 0.1511, 0.6078]]])
dim0 = F.softmax(y, dim=0)
print('dim=0 softmax:\n', dim0)
print('dim=0, tensor:')
for i in range(2):
print(y[i, :, :].reshape(-1))
# dim = 0指第一个维度,例子中第一个维度的size是2
dim=0 softmax:
tensor([[[0.4903, 0.2797, 0.3166, 0.6895],
[0.4596, 0.5384, 0.4956, 0.5258],
[0.6296, 0.6368, 0.4938, 0.4038]], [[0.5097, 0.7203, 0.6834, 0.3105],
[0.5404, 0.4616, 0.5044, 0.4742],
[0.3704, 0.3632, 0.5062, 0.5962]]])
dim=0, tensor:
tensor([0.6898, 0.0193, 0.0913, 0.9597, 0.2965, 0.6402, 0.3175, 0.2141, 0.6842,
0.6477, 0.1265, 0.2181])
tensor([0.7287, 0.9654, 0.8608, 0.1618, 0.4583, 0.4862, 0.3352, 0.1108, 0.1539,
0.0863, 0.1511, 0.6078])
dim1 = F.softmax(y, dim=1)
print('dim=1 softmax:\n', dim1)
print('dim=1, tensor:')
for i in range(3):
print(y[:, i, :].reshape(-1))
# dim = 1指第二个维度,例子中第一个维度的size是3
dim=1 softmax:
tensor([[[0.3746, 0.2112, 0.3040, 0.5126],
[0.2528, 0.3929, 0.3811, 0.2432],
[0.3725, 0.3959, 0.3149, 0.2442]], [[0.4299, 0.4915, 0.4801, 0.2847],
[0.3281, 0.3044, 0.2838, 0.2705],
[0.2420, 0.2041, 0.2361, 0.4447]]])
dim=1, tensor:
tensor([0.6898, 0.0193, 0.0913, 0.9597, 0.7287, 0.9654, 0.8608, 0.1618])
tensor([0.2965, 0.6402, 0.3175, 0.2141, 0.4583, 0.4862, 0.3352, 0.1108])
tensor([0.6842, 0.6477, 0.1265, 0.2181, 0.1539, 0.0863, 0.1511, 0.6078])
dim2 = F.softmax(y, dim=2)
print('dim=2 softmax:\n', dim2)
print('dim=2, tensor:')
for i in range(4):
print(y[:, :, i].reshape(-1))
# dim = 2指第三个维度,例子中第一个维度的size是4
dim=2 softmax:
tensor([[[0.2967, 0.1517, 0.1631, 0.3886],
[0.2298, 0.3240, 0.2346, 0.2116],
[0.3161, 0.3047, 0.1809, 0.1983]], [[0.2515, 0.3187, 0.2870, 0.1427],
[0.2763, 0.2841, 0.2443, 0.1952],
[0.2219, 0.2074, 0.2213, 0.3494]]])
dim=2, tensor:
tensor([0.6898, 0.2965, 0.6842, 0.7287, 0.4583, 0.1539])
tensor([0.0193, 0.6402, 0.6477, 0.9654, 0.4862, 0.0863])
tensor([0.0913, 0.3175, 0.1265, 0.8608, 0.3352, 0.1511])
tensor([0.9597, 0.2141, 0.2181, 0.1618, 0.1108, 0.6078])

python3 Softmax函数的更多相关文章

  1. [Machine Learning] logistic函数和softmax函数

    简单总结一下机器学习最常见的两个函数,一个是logistic函数,另一个是softmax函数,若有不足之处,希望大家可以帮忙指正.本文首先分别介绍logistic函数和softmax函数的定义和应用, ...

  2. softmax函数详解

    答案来自专栏:机器学习算法与自然语言处理 详解softmax函数以及相关求导过程 这几天学习了一下softmax激活函数,以及它的梯度求导过程,整理一下便于分享和交流. softmax函数 softm ...

  3. tensorflow 使用 5 mnist 数据集, softmax 函数

    用于分类  softmax 函数 手写数据识别:

  4. Softmax函数详解与推导

    一.softmax函数 softmax用于多分类过程中,它将多个神经元的输出,映射到(0,1)区间内,可以看成概率来理解,从而来进行多分类! 假设我们有一个数组,V,Vi表示V中的第i个元素,那么这个 ...

  5. [机器学习入门篇]-Logistic函数与Softmax函数

    1.Logistic函数 在维基百科中,对logistic函数这样介绍道: A logistic function or logistic curve is a common "S" ...

  6. 层次softmax函数(hierarchical softmax)

    一.h-softmax 在面对label众多的分类问题时,fastText设计了一种hierarchical softmax函数.使其具有以下优势: (1)适合大型数据+高效的训练速度:能够训练模型“ ...

  7. Python3 isinstance() 函数

    Python3 isinstance() 函数 描述 isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). isinstance() 与 type() 区别: typ ...

  8. Python3 join函数和os.path.join用法

    Python3  join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...

  9. Python3 round() 函数

    Python3 round() 函数  Python3 数字 描述 round() 方法返回浮点数x的四舍五入值. 语法 以下是 round() 方法的语法: round( x [, n] ) 参数 ...

随机推荐

  1. springboot实现热部署的几种方式

    原理:使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在 ...

  2. Kafka 的高可靠性是怎么实现的?

    可以参见我这篇文章:Kafka 是如何保证数据可靠性和一致性

  3. Spring @Configuration继承

    Bean定义继承 Bean定义可以包含许多配置信息,包括构造函数参数,属性值和特定于容器的信息,例如初始化方法,静态工厂方法名称等.子bean定义从父定义继承配置数据.子定义可以覆盖某些值或根据需要添 ...

  4. 上传文件到阿里云linux服务器(windows到Linux的文件上传)

    在"运行"中输入cmd,打开控制台,切换到刚才Putty的安装目录下,我的是E:\Putty,然后输入pscp命令,我们需要这个命令来实现文件的上传.如下命令格式: F:\PuTT ...

  5. Ubuntu 18.04 磁盘根目录在线扩容 & 修改分区 inode 数量

    Ubuntu 18.04 磁盘根目录在线扩容 & 修改分区 inode 数量   Ubuntu 作为服务器系统使用的时候,系统盘的空间可能并不是很充裕,apt apt 着,根目录就满了.诚然, ...

  6. scanf()函数的原理

    最近使用scanf发现了自己对scanf函数还是不太了解,主要出现在无意中出现的一个错误: scanf正确的写法是,scanf中以什么格式输入变量,则变量的类型就应该是什么格式,如下面scanf输入到 ...

  7. 顺利通过EMC试验(2)

    限制值 电磁波照射,静电放电敏感性

  8. 关于小程序websocket全套解决方案,Nginx代理wss

    需求对话 提问 我在本地web能够使用ws协议去链接websocket,但是小程序不能使用. 回答 由于小程序使用的是SSL加密协议,所以需要使用wss.这里wss与ws的关系就相当于https于ht ...

  9. java的内存泄露是如何发生的,如何避免和发现

    java的垃圾回收与内存泄露的关系:[新手可忽略不影响继续学习] 马克-to-win:上一节讲了,(i)对象被置成null.(ii)局部对象(无需置成null)当程序运行到右大括号.(iii)匿名对象 ...

  10. 安卓xml布局中 android:paddingBottom="@dimen/activity_vertical_margin"是什么意思?

    @dimen/activity_vertical_margin 是什么意思? @dimen/activity_vertical_margin这个的意思就是在你的values文件夹下面的dimens文件 ...