题目
Log Softmax函数(Log Softmax)是一种常用的激活函数,是Softmax函数的对数形式,其计算公式为:
f
(
x
)
=
log
(
e
x
i
∑
j
=
1
n
e
x
j
)
f(x) = \log \left( \frac{e^{x_i}}{\sum_{j=1}^{n} e^{x_j}} \right)
f(x)=log(∑j=1nexjexi)
其中,
x
x
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)))