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] ) 参数 ...
随机推荐
- 怎样在方法里面得到Request,或者Session?
直接在方法的形参中声明request,SpringMvc就自动把request对象传入.
- Redis 相比 Memcached 有哪些优势?
1.Memcached 所有的值均是简单的字符串,redis 作为其替代者,支持更为丰 富的数据类 2.Redis 的速度比 Memcached 快很 3.Redis 可以持久化其数据
- poj_1852_Ants(复杂问题简单化)
原题传送门 描述 一群蚂蚁走在长度为l cm的水平细杆上,以1cm/s的匀速.当一只行走的蚂蚁到达杆的一端,它就会掉下去.当两只蚂蚁相遇,它们会掉头像反方向走去.我们知道一只蚂蚁在杆上的初始位置,然而 ...
- hbase增删查
代码: package cn.idcast.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.h ...
- 【Android开发】Android 颜色透明度换算
透明度 透明度分为256阶(0-255),计算机上用16进制表示为(00-ff). 透明就是0阶,不透明就是255阶,如果50%透明就是127阶(256的一半当然是128,但因为是从0开始,所以实际上 ...
- IE zoom
zoom是IE浏览器特有的属性,它可以设置或检索对象的缩放比例(它的中文解释是:放大),它的作用通常可以概括为三个方面: 1.hasLayout 2.清除浮动 3.清除div的垂直外边距合并问题 什么 ...
- CommonsCollection6反序列化链学习
CommonsCollection6 1.前置知识 1.1.HashSet HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合.继承了序列化和集合 构造函数参数为空的话创建一 ...
- python---用顺序表实现栈
class Stack(object): """栈, 存放数据的一种容器, 后进先出""" def __init__(self): self ...
- Mybatis注解开发(一对一)
其他代码访问:Mybatis注解开发基础操作 1.添加OrderMapper接口 public interface OrderMapper { // @Select("select *,o. ...
- Spring原始注解开发-02
使用@Repository.@Service.@Controller注解配置,使其更加清晰属于哪一层,因为我是模拟的web层,所有没有使用@Controller注解,后面结合web开发会使用到 1.创 ...