Open shangwang99 opened 8 years ago
To save a classifier it's best if you just use pickle
so everything is saved correctly. There are tests for that.
If you don't use pickle
you may need to call _initialize
yourself with the right data. See if you can find the function in the code and let me know if it works!
@alexjc Thank you very much for your quick response.
I tried your method using the following code:
clf2 = Classifier( layers=[ Layer("Sigmoid", units=1000), Layer("Softmax",units=8)], learning_rate=0.009, n_iter=50)
clf2._initialize(newx[train])
Then clf2.is_initialized returns True. Great, it is initialized.
And then I set the parameters of this new classifier using clf2.set_parameters(param) and test using clf2.get_parameters(). I got the parameters!!
But when I use this to make prediction clf2.predict(newx[test]), I still got an assertion error
AssertionError Traceback (most recent call last)
You need to pass y
to _initialize
otherwise it can't know the classes. I think I'm going to remove this case, you should use pickle
for classifiers—there are too many things to restore.
@alexjc When I try to add y in the _initialize, I got the following error. y is a list of multi-class labels, for example [5, 5, 5, 5, 6, 6, 7, 7, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4].
IndexError Traceback (most recent call last)
Probably won't work unless it's fixed. Try partial_fit
with classes
and empty data?
Instructed by the document, I constructed a classifier and trained it using the following code:
clf = Classifier( layers=[ Layer("Sigmoid", units=1000), Layer("Softmax",units=8)], learning_rate=0.009, n_iter=50) clf.fit(x[train],y[train])
And made prediction using: pred = clf.predict(x[test])
Then I extracted and saved the parameters of the classifier using param = clf.get_parameters() np.save('./param.npy')
When I build a new classifier and try to initialise it with set_parameters using following code: clf2 = Classifier( layers=[ Layer("Sigmoid", units=1000), Layer("Softmax",units=8)], learning_rate=0.009, n_iter=50)
clf2.set_parameters(param) pred2 = clf2.predict(x[test])
I got the following error:
assert self.label_binarizers != [],\ --> 451 "Can't predict without fitting: output classes are unknown." 452 453 yp = self.predict_proba(X, collapse=False)
AssertionError: Can't predict without fitting: output classes are unknown.
I also tried clf2.set_parameters([(param[0].weights,param[0].biases), (param[1].weights,param[1].biases)]) and clf2 = Classifier( layers=[ Layer("Sigmoid", units=1000), Layer("Softmax",units=8)], parameters=[(param[0].weights,param[0].biases), (param[1].weights,param[1].biases)])
I still got the error. And I tested clf2.is_initialized, the result is False.
Could someone give any suggestions to deal with this or an example of using set_parameters() to initialize a classifier?
Thank you very much. :)