1 Softmax

Softmax 函数是一种将任意实数值的向量转换为概率分布的数学函数。它将一个 K 维实值向量转换为另一个 K 维实值向量,其中每个元素的值都在 (0,1) 区间内,且所有元素的和为 1。

oftmax的作用

Softmax函数的主要作用是将一组输入值(可以是任何实数)映射到(0,1)区间内,并输出为概率分布。Softmax的输出可以被理解为属于每个类别的概率,且所有类别的概率之和为1。

代码实现:

# 导入相关库
import numpy as np

x = np.array([1, 2, 3, 4])
t1 = np.exp(x[0]) / np.sum(np.exp(x))
t2 = np.exp(x[1]) / np.sum(np.exp(x))
t3 = np.exp(x[2]) / np.sum(np.exp(x))
t4 = np.exp(x[3]) / np.sum(np.exp(x))
print(t1)  # 0.03205860328008499
print(t2)  # 0.08714431874203257
print(t3)  # 0.23688281808991013
print(t4)  # 0.6439142598879722

print(t1 + t2 + t3 + t4)  # 1.0

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐