python3 Softmax函数
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函数的更多相关文章
- [Machine Learning] logistic函数和softmax函数
简单总结一下机器学习最常见的两个函数,一个是logistic函数,另一个是softmax函数,若有不足之处,希望大家可以帮忙指正.本文首先分别介绍logistic函数和softmax函数的定义和应用, ...
- softmax函数详解
答案来自专栏:机器学习算法与自然语言处理 详解softmax函数以及相关求导过程 这几天学习了一下softmax激活函数,以及它的梯度求导过程,整理一下便于分享和交流. softmax函数 softm ...
- tensorflow 使用 5 mnist 数据集, softmax 函数
用于分类 softmax 函数 手写数据识别:
- Softmax函数详解与推导
一.softmax函数 softmax用于多分类过程中,它将多个神经元的输出,映射到(0,1)区间内,可以看成概率来理解,从而来进行多分类! 假设我们有一个数组,V,Vi表示V中的第i个元素,那么这个 ...
- [机器学习入门篇]-Logistic函数与Softmax函数
1.Logistic函数 在维基百科中,对logistic函数这样介绍道: A logistic function or logistic curve is a common "S" ...
- 层次softmax函数(hierarchical softmax)
一.h-softmax 在面对label众多的分类问题时,fastText设计了一种hierarchical softmax函数.使其具有以下优势: (1)适合大型数据+高效的训练速度:能够训练模型“ ...
- Python3 isinstance() 函数
Python3 isinstance() 函数 描述 isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). isinstance() 与 type() 区别: typ ...
- Python3 join函数和os.path.join用法
Python3 join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...
- Python3 round() 函数
Python3 round() 函数 Python3 数字 描述 round() 方法返回浮点数x的四舍五入值. 语法 以下是 round() 方法的语法: round( x [, n] ) 参数 ...
随机推荐
- 什么是B+树??
上一篇中,我们了解了B树,辣么..B+树又是什么呢?? 一:定义:B+树是基于B树的,是B树的变形,也是一种多路搜索树.查询性能更加出色. 1.每个父节点元素出现在子节点中,是子节点的最大或最小元素. ...
- python 面向对象的一些魔法方法和反射
1.with和__enter__,__exit__,__init__配合使用class A: def __init__(self): print('init') def __enter__(self) ...
- Java中如何声明方法?JavaScript中如何声明函数?
public void method(){ } //实例方法 Function Declaration 可以定义命名的函数变量,而无需给变量赋值.Function Declaration 是一种独立的 ...
- 区分 BeanFactory 和 ApplicationContext?
BeanFactory ApplicationContext 它使用懒加载 它使用即时加载 它使用语法显式提供资源对象 它自己创建和管理资源对象 不支持国际化 支持国际化 不支持基于依赖的注解 支持基 ...
- ThreadLocal是什么?使用场景有哪些?
什么是ThreadLocal? ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本. 测试代码: package ...
- 请用c++ 实现stl中的string类,实现构造,拷贝构造,析构,赋值,比较,字符串相加,获取长度及子串等功能。
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 class String{ 5 public ...
- ElasticSearch-学习笔记01-docker安装
安装ElasticSearch docker 安装请参考: https://www.cnblogs.com/youxiu326/p/docker-01.html docker run -d --nam ...
- 前馈控制+PID
参考来源: 北京交通大学 硕士学位论文 基于脉冲串控制的含位置反馈和前馈补偿的位置控制算法的研究 赵旺升
- VueJs项目笔记
======================知识点总结=========================== 一.keep-alive(实现页面的缓存) 二. 移动端固定定位的解决方案 三. Vue表 ...
- 使用Canvas和JavaScript做一个画板
本文同步于个人博客:https://zhoushuo.me/blog/2018/03/11/drawing-borad/ 前些天学习了HTML5的<canvas>元素,今天就来实践一下,用 ...