【发布时间】:2013-09-24 14:08:48
【问题描述】:
我正在尝试在线使用(out-of-core) MNIST 问题的学习算法使用SGDClassifier 但似乎准确性并不总是在提高。
在这种情况下我该怎么办?以最准确的方式保存分类器? SGDClassifier 是否收敛到某个最优解?
这是我的代码:
import numpy as np
from sklearn.linear_model.stochastic_gradient import SGDClassifier
from sklearn.datasets import fetch_mldata
from sklearn.utils import shuffle
#use all digits
mnist = fetch_mldata("MNIST original")
X_train, y_train = mnist.data[:70000] / 255., mnist.target[:70000]
X_train, y_train = shuffle(X_train, y_train)
X_test, y_test = X_train[60000:70000], y_train[60000:70000]
step =1000
batches= np.arange(0,60000,step)
all_classes = np.array([0,1,2,3,4,5,6,7,8,9])
classifier = SGDClassifier()
for curr in batches:
X_curr, y_curr = X_train[curr:curr+step], y_train[curr:curr+step]
classifier.partial_fit(X_curr, y_curr, classes=all_classes)
score= classifier.score(X_test, y_test)
print score
print "all done"
我在 MNIST 上测试了 linearSVM 与 SGD,使用 10k 样本进行训练和 10k 样本进行测试,得到 0.883 13,95 和 0.85 1,32,因此 SGD 更快但准确度较低。
#test linearSVM vs SGD
t0 = time.time()
clf = LinearSVC()
clf.fit(X_train, y_train)
score= clf.score(X_test, y_test)
print score
print (time.time()-t0)
t1 = time.time()
clf = SGDClassifier()
clf.fit(X_train, y_train)
score= clf.score(X_test, y_test)
print score
print (time.time()-t1)
我也在这里找到了一些信息 https://stats.stackexchange.com/a/14936/16843
更新: 超过 1 次(10 次)通过数据达到 90.8 % 的最佳准确度。因此它可以成为解决方案。 SGD 的另一个特点是数据在传递给分类器之前必须经过洗牌。
【问题讨论】:
-
能否请您编辑您的问题以添加脚本的输出,以便手边没有安装 sklearn 的人仍然可以关注该问题?您观察到了什么以及您期望会发生什么?
-
Score 的输出在开始时类似于 0.52 0.6 0.75 0.74 0.84 0.80 0.82,但随后会在 0.8+-some_value 上下波动,我希望它会一直增加到某个 max_score 值,然后停止。
-
您可以在此处找到已解码的 MNIST 数据集版本:mnist-decoded.000webhostapp.com
标签: python machine-learning scikit-learn mnist