【发布时间】:2021-04-23 18:42:18
【问题描述】:
我知道softmax激活函数:输出层与softmax激活的总和总是等于1,也就是说:输出向量是归一化的,这也是必要的,因为最大累积概率不能超过1。好的,这很清楚。
但是我的问题是:当softmax用作分类器时,是使用argmax函数来获取类的索引。那么,如果重要参数是获得正确类别的指标,那么获得 1 或更高的累积概率有什么区别?
python 中的一个示例,我在其中创建了另一个 softmax(实际上不是 softmax 函数),但分类器的工作方式与使用真正的 softmax 函数的分类器相同:
import numpy as np
classes = 10
classes_list = ['dog', 'cat', 'monkey', 'butterfly', 'donkey',
'horse', 'human', 'car', 'table', 'bottle']
# This simulates and NN with her weights and the previous
# layer with a ReLU activation
a = np.random.normal(0, 0.5, (classes,512)) # Output from previous layer
w = np.random.normal(0, 0.5, (512,1)) # weights
b = np.random.normal(0, 0.5, (classes,1)) # bias
# correct solution:
def softmax(a, w, b):
a = np.maximum(a, 0) # ReLU simulation
x = np.matmul(a, w) + b
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0), np.argsort(e_x.flatten())[::-1]
# approx solution (probability is upper than one):
def softmax_app(a, w, b):
a = np.maximum(a, 0) # ReLU simulation
w_exp = np.exp(w)
coef = np.sum(w_exp)
matmul = np.exp(np.matmul(a,w) + b)
res = matmul / coef
return res, np.argsort(res.flatten())[::-1]
teor = softmax(a, w, b)
approx = softmax_app(a, w, b)
class_teor = classes_list[teor[-1][0]]
class_approx = classes_list[approx[-1][0]]
print(np.array_equal(teor[-1], approx[-1]))
print(class_teor == class_approx)
两种方法之间获得的类总是相同的(我说的是预测,而不是训练)。我问这个是因为我在 FPGA 设备中实现 softmax 并且使用第二种方法不需要 2 次运行来计算 softmax 函数:首先找到指数矩阵和它的总和,然后执行除法。
【问题讨论】:
标签: deep-learning neural-network classification softmax