MLPClassifier:warm_start仅可用于“ y”具有与上一次调用相同的类的情况。

时间:2018-08-06 05:13:19

标签: scikit-learn

我正在使用public static void main(String[] args)throws FileNotFoundException { XStream xstream = new XStream(); xstream.alias("Game", Game.class); xstream.alias("Games", GameList.class); xstream.addImplicitCollection(GameList.class, "Games", Game.class); FileReader reader = new FileReader("Games.xml"); GameList gamelist = new GameList(); gamelist.setGames((ArrayList<Game>) xstream.fromXML(reader)); } 逐步培训MLPClassifier进行多类分类。这是我的代码的一部分。 `

partial_fit

如果运行上面的代码,则错误是这样的:

from sklearn.datasets import load_svmlight_file
import numpy as np
from scipy.sparse import hstack
from sklearn.neural_network import MLPClassifier
import math

data_path = "/home/chandresh/ckm/data/multiclass data/"
tranfile= 'letter.scale.tr' # download this data from 
# https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass.html

def get_data():
    data = load_svmlight_file(data_path+tranfile)
    return data[0], data[1].astype(int)

X, y = get_data()

# add bias column

X = hstack([np.array([[1] for _ in range(X.shape[0])]), X])
X = X.toarray()

n, d = X.shape
no_classses =len( np.unique(y)) # 26 classes


clf = MLPClassifier(hidden_layer_sizes=(d), solver='sgd', warm_start=True, batch_size=1, max_iter=1, random_state=1)
# for online learning, I'm extracting at least one instance from each class and doing partial_fit
# xtr, ytr contain one instance from each class.
xtr=[]
ytr=[]
ind=[]

for i in range(1,no_classses+1):
    s, = np.where(y==i)
    ind.append(s[0])

xtr = X[ind,:]
ytr = y[ind]

X = np.delete(X,ind,0)
y = np.delete(y, ind,0)

n = X.shape[0]
classes  = np.unique(y)
clf.partial_fit(xtr,ytr, classes)
for t in range(0,n):
    xt = X[t,:]
    yt = int(y[t])-1
    p  = clf.predict_proba([xt])      
     # update the MLP
    clf = clf.partial_fit([xt],[yt], classes) 

根据sketch的有关partial_fit的文档,ValueError: warm_start can only be used where `y` has the same classes as in the previous call to fit. Previously got [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26], `y` has [21] 在下一次对partial_fit的调用中可以省略,但这无济于事。我不是在进行迷你批处理,而是一次训练MLP一个示例。因此,随后对partial_fit的调用将不具有所有类,这就是为什么会出错。有骇客吗?

谢谢。

0 个答案:

没有答案