从Udacity的深度学习类中,y_i的softmax只是指数除以整个Y向量的指数之和:
哪里S(y_i)是的SOFTMAX功能y_i,并e为指数和j是否定的.输入向量Y中的列数.
我尝试过以下方法:
import numpy as np
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()
scores = [3.0, 1.0, 0.2]
print(softmax(scores))
Run Code Online (Sandbox Code Playgroud)
返回:
[ 0.8360188 0.11314284 0.05083836]
Run Code Online (Sandbox Code Playgroud)
但建议的解决方案是:
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
return np.exp(x) / np.sum(np.exp(x), axis=0)
Run Code Online (Sandbox Code Playgroud)
它产生与第一个实现相同的输出,即使第一个实现显式获取每列和最大值的差异,然后除以总和.
有人可以用数学方式显示原因吗?一个是正确的而另一个是错的吗?
实现在代码和时间复杂性方面是否相似?哪个更有效率?
我有2000个标记数据(7种不同标签)和大约100K未标记数据,我正在尝试使用sklearn.semi_supervised.LabelPropagation.数据有1024个维度.我的问题是分类器将所有内容标记为1.我的代码如下所示:
X_unlabeled = X_unlabeled[:10000, :]
X_both = np.vstack((X_train, X_unlabeled))
y_both = np.append(y_train, -np.ones((X_unlabeled.shape[0],)))
clf = LabelPropagation(max_iter=100).fit(X_both, y_both)
y_pred = clf.predict(X_test)
Run Code Online (Sandbox Code Playgroud)
y_pred是所有的.此外,X_train是2000x1024并且X_unlabeled是未标记数据的子集10000x1024.
在分类器上调用fit时,我也会收到此错误:
/usr/local/lib/python2.7/site-packages/sklearn/semi_supervised/label_propagation.py:255:RuntimeWarning:在self.label_distributions_/= normalizer中遇到无效值