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. python 装饰器函数基础知识

    1.装饰器的本质--一个闭包函数 2.装饰器的功能--在不改变原函数及其调用方式情况下对原函数功能进行拓展 3.带参数和返回值的装饰器 def timer(func): @wraps(func) #使 ...

  2. Kafka 都有哪些特点?

    高吞吐量.低延迟:kafka每秒可以处理几十万条消息,它的延迟最低只有几毫秒,每个topic可以分多个partition, consumer group 对partition进行consume操作. ...

  3. consumer提交offset原理

    1 数据结构 消费者的消费状态是保存在SubscriptionState类中的,而SubscriptionState有个重要的属性那就是assignment保存了消费者消费的partition及其pa ...

  4. 顺利通过EMC实验(12)

  5. 告别尬聊,解锁秀场+社交新玩法(内含源码+Demo)

    直播已成为用户的生活习惯之一 艾媒咨询数据显示:2021年直播用户规模达到6.35亿人,在线直播用户以年轻群体为主,24岁及以下用户占比49%,30岁以下用户接近8成. 众所周知,Z世代用户是一个社交 ...

  6. JavaScript正则进阶之路——活学妙用奇淫正则表达式

    原文收录在我的 GitHub博客 (https://github.com/jawil/blog) ,喜欢的可以关注最新动态,大家一起多交流学习,共同进步,以学习者的身份写博客,记录点滴. 有些童鞋肯定 ...

  7. iview table表中使用render函数props传值出现问题

    使用iview中的table表格时避免不了使用render函数渲染自定义内容,或者渲染组件.但是在正常使用时出现了props传值无法识别, 按照官网介绍使用props如下: render: (h, p ...

  8. 前端每日实战:134# 视频演示如何用 CSS 和 GSAP 创作一个树枝发芽的 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/LJmpXZ 可交互视频 此视频是可 ...

  9. DOM节点的使用(常用方法+代码)

    DOM节点的应用 学习总结 1. 什么是 DOM 2. HTMLDOM 3. 元素获取 元素获取方式 元素节点的属性操作 4. Node 对象的属性和方法 常用属性 常用方法 5. 事件处理 事件驱动 ...

  10. add jars、add external jars、add library、add class folder的区别

    add external jars = 增加工程外部的包add jars = 增加工程内包add library = 增加一个库add class folder = 增加一个类文件夹add jar是表 ...