文章目录

  • softmax 将值的向量转换为概率分布
  • 创建数组
  • 计算公式
  • 计算过程
  • tf.keras.activations.softmax实现
  • numpy实现

softmax 将值的向量转换为概率分布

import tensorflow as tf
import numpy as np

创建数组

a1 = np.arange(0,6).reshape(2,3).astype(float)
print(a1)
[[0. 1. 2.][3. 4. 5.]]

计算公式

softmax=eai−max∑eai−maxsoftmax = \frac{e^{a_{i}-max}}{\sum e^{a_{i}-max}}softmax=∑eai​−maxeai​−max​​

aia_{i}ai​​ : 数组中的元素

e : 自然数

max : 数组中的最大数

计算过程

[[e0−5e0−5+e1−5+e2−5,e1−5e0−5+e1−5+e2−5,e2−5e0−5+e1−5+e2−5][e3−5e3−5+e4−5+e5−5,e4−5e3−5+e4−5+e5−5,e5−5e3−5+e4−5+e5−5]]=[[0.09003057,0.24472847,0.66524096][0.09003057,0.24472847,0.66524096]][[\frac{e^{0-5}}{e^{0-5}+e^{1-5}+e^{2-5}} ,\frac{e^{1-5}}{e^{0-5}+e^{1-5}+e^{2-5}},\frac{e^{2-5}}{e^{0-5}+e^{1-5}+e^{2-5}}][\frac{e^{3-5}}{e^{3-5}+e^{4-5}+e^{5-5}} ,\frac{e^{4-5}}{e^{3-5}+e^{4-5}+e^{5-5}},\frac{e^{5-5}}{e^{3-5}+e^{4-5}+e^{5-5}}]]=[[0.09003057 ,0.24472847 ,0.66524096] [0.09003057 ,0.24472847 ,0.66524096]][[e0−5+e1−5+e2−5e0−5​,e0−5+e1−5+e2−5e1−5​,e0−5+e1−5+e2−5e2−5​][e3−5+e4−5+e5−5e3−5​,e3−5+e4−5+e5−5e4−5​,e3−5+e4−5+e5−5e5−5​]]=[[0.09003057,0.24472847,0.66524096][0.09003057,0.24472847,0.66524096]]​​

tf.keras.activations.softmax实现

t2 = tf.convert_to_tensor(a1)
print(t2)
t2_softmax = tf.keras.activations.softmax(t2)
print(t2_softmax)
tf.Tensor(
[[0. 1. 2.][3. 4. 5.]], shape=(2, 3), dtype=float64)
tf.Tensor(
[[0.09003057 0.24472847 0.66524096][0.09003057 0.24472847 0.66524096]], shape=(2, 3), dtype=float64)
print(tf.reduce_sum(t2_softmax[0, :]))
tf.Tensor(0.9999999999999999, shape=(), dtype=float64)

numpy实现

a1_max = np.max(a1)
print(a1_max)
5.0
a1_reduce_max = a1-a1_max
print(a1_reduce_max)
[[-5. -4. -3.][-2. -1.  0.]]
a1_exp = np.exp(a1_reduce_max)
print(a1_exp)
[[0.00673795 0.01831564 0.04978707][0.13533528 0.36787944 1.        ]]
a1_exp_sum = np.sum(a1_exp,axis = 1)
print(a1_exp_sum)
[0.07484065 1.50321472]
a1_exp_sum = a1_exp_sum.reshape(-1, 1)
a1_exp_sum_pad = np.pad(a1_exp_sum,  # 数据((0, 0),(1, 1),),  # 最后一维,左边、右边'reflect'  # 填充边缘对称值
)
print(a1_exp_sum_pad)
[[0.07484065 0.07484065 0.07484065][1.50321472 1.50321472 1.50321472]]
# a1_softmax = a1_exp/a1_exp_sum
a1_softmax = np.divide(a1_exp,a1_exp_sum)
print(a1_softmax)
[[0.09003057 0.24472847 0.66524096][0.09003057 0.24472847 0.66524096]]
print(np.sum(a1_softmax,axis=-1))
[1. 1.]

tf.keras.activations.softmax 激活函数 示例相关推荐

  1. tf.keras.activations.sigmoid 激活函数 示例

    import tensorflow as tf Sigmoid 等价于 2 元素 Softmax,其中第二个元素假定为零.sigmoid 函数始终返回一个介于 0 和 1 之间的值.‎, 用于隐层神经 ...

  2. tf.keras.activations.relu 激活函数 示例

    import tensorflow as tf 取 0 和 x 中 最大的值 foo = tf.constant([-10, -5, 0.0, 5, 10], dtype=tf.float32) tf ...

  3. tf.keras.activations.gelu tensorflow1.15.0

    鉴于tensorflow1.15.0没有tf.keras.activations.gelu函数,所以需要添加gelu函数的定义. 以下代码为激活函数gelu的定义: def gelu_(X):retu ...

  4. tf.keras.activations.elu(五)

    tf.keras.activations.elu(x,alpha=1.0 ) 源码 Defined in tensorflow/python/keras/activations.py. @tf_exp ...

  5. 深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-softmax多分类-06

    softmax分类 Fashion MNIST数据集 import tensorflow as tf import pandas as pd import numpy as np import mat ...

  6. tf.keras.losses.SquaredHinge 损失函数 示例

    平方铰链 loss=(Max(1−正确值×预测值,0))2loss=(Max(1-正确值\times 预测值,0))^{2}loss=(Max(1−正确值×预测值,0))2 正确值应为 -1 或 1. ...

  7. tf.keras.losses.Poisson 损失函数 示例

    泊松损失函数 预测值为为 PPP ,真实值 TTT . poisson(P,T)=1n∑i=1n(Pi−Tilog(Pi))poisson(P,T)=\frac{1}{n}\sum_{i=1}^{n} ...

  8. tf.keras.losses.MeanAbsolutePercentageError 损失函数 示例

    平均绝对百分比误差 MAPE MAPE 是 MAD 的变形,它是一个百分比值,因此比其他统计量更容易理解.例如,如果 MAPE 为 5,则表示预测结果较真实结果平均偏离 5 %.MAPE 的计算公式如 ...

  9. tf.keras.losses.MeanAbsoluteError 损失函数 示例

    平均绝对值误差 计算标签和预测之间的绝对差值的平均值.‎ import tensorflow as tf y_true = [[0., 1.], [0., 0.]] y_pred = [[1., 1. ...

最新文章

  1. 机器视觉-相机内参数和外参数
  2. 计算机网络第七版(谢希仁著)课后习题答案
  3. MobaXterm 设置在使用export DISPLAY=xx.xx.xx.xx:0.0后调用图形化界面不弹出提示方法
  4. Tomcat配置问题解决方法
  5. sublime支持虚拟环境python2.7
  6. Win7搭建http文件共享
  7. 开窗函数的意义与用法
  8. iOS开发拓展篇—蓝牙之mutipeerConnectivity的使用
  9. 计算机应用 课件 .doc,《计算机应用基础》课件36915.doc
  10. win10环境下matlab2017b编译运行c++文件步骤
  11. cat3速度 rj45_综合布线当中,CAT8网线开始渐入佳境
  12. 计算机会议论文和sci,ei会议论文集算不算期刊_ieee会议论文集属于istp_会议论文集算发表么...
  13. AutoLisp从入门到放弃(十三)
  14. 经典单片机c语言教程 pdf下载,51单片机经典教程.pdf
  15. 去银行当程序员是一种什么体验
  16. 二进制、八进制、十六进制的写法
  17. AE制作文字模糊特效
  18. 用计算机画函数图象的软件,信息技术应用 用计算机画函数图象优秀教案
  19. [操作系统]关于平均周转时间的一些题目
  20. 冶化工业计算机模拟仿真工作,工业仿真,工业三维虚拟仿真技术平台 - 【中视典数字科技】...

热门文章

  1. ECCV2020图像分割开源论文合集
  2. OpenCV源代码分析——SGBM
  3. FusionNet:基于稀疏雷达点云和RGB图像的深度图补全
  4. unknown error 1130,unknown error 1045
  5. Nat. Mach. Intell. | FFPred-GAN:“以假乱真“—基于GAN创建合成特征样本改进蛋白质功能预测...
  6. KerGNNs:结合Graph kernels的可解释GNN
  7. 目前的Android恶意软件分类
  8. mysql哪些数据库不能删除吗_为什么我不能删除MySQL数据库?
  9. java 数据聚合_Java数据聚合问题请教?
  10. MPB:生态环境中心韩丽丽等-土壤病毒组富集及DNA提取