牛客题解 | Log Softmax函数的实现

题目

题目链接

Log Softmax函数(Log Softmax)是一种常用的激活函数,是Softmax函数的对数形式,其计算公式为:

\[f(x) = \log \left( \frac{e^{x_i}}{\sum_{j=1}^{n} e^{x_j}} \right) \]

其中,\(x\)是输入。
该算法是深度学习中常用的激活函数之一。

标准代码如下

def log_softmax(scores: list) -> np.ndarray:
    # Subtract the maximum value for numerical stability
    scores = scores - np.max(scores)
    return scores - np.log(np.sum(np.exp(scores)))
posted @ 2025-03-12 12:35  wangxiaoxiao  阅读(18)  评论(0)    收藏  举报