softmax里边的exp用拟合验证精度。

文章目录


要验证Softmax函数中的指数运算(exp函数)对精度的影响,可以通过拟合一个函数来近似Softmax函数,并比较两者的输出结果。

import numpy as np
import matplotlib.pyplot as plt

# Softmax函数
def softmax(x):
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

# 拟合函数
def approximate_softmax(x, k):
    return np.exp(k * x) / np.sum(np.exp(k * x))

# 生成输入数据
x = np.array([2.0, 1.0, 0.1])

# 计算Softmax输出
softmax_output = softmax(x)

# 拟合Softmax函数
k = 10
approx_output = approximate_softmax(x, k)

# 打印结果
print("Softmax Output:", softmax_output)
print("Approximate Output with k =", k, ":", approx_output)

# 绘制对比图
plt.figure()
plt.plot(softmax_output, label='Softmax Output')
plt.plot(approx_output, label='Approximate Output (k=' + str(k) + ')')
plt.legend()
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值