Reputation: 31
I'm working on a project that looks to classify tweets and am using sklearn
's neural network model. Is it possible to retrain this using sklearn
and if so please guide me in the right direction. Also, is it worth it to retrain a model or should I just adjust values when constructing a network.
Upvotes: 0
Views: 1314
Reputation: 4264
You could very well do that using the partial_fit
method that MLPClasifier
offers. I have written a sample code for doing it. You could very well retrain your saved model if you get data in batches and training is a costy operation for you so can't afford to train on the entire dataset each and every time you get a new batch of data.
import pickle
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_classes=4, n_features=11,
n_informative=4, weights=[0.25,0.25,0.25,0.25],
random_state=0)
x_batch1 = X[0:500]
y_batch1 = y[0:500]
x_batch2 = X[500:999]
y_batch2 = y[500:999]
clf = MLPClassifier()
clf.partial_fit(x_batch1, y_batch1, classes = np.unique(y)) # you need to pass the classes when you fit for the first time
pickle.dump(clf, open("MLP_classifier", 'wb'))
restored_clf = pickle.load(open("MLP_classifier", 'rb'))
restored_clf.partial_fit(x_batch2, y_batch2)
Upvotes: 2
Reputation: 2128
You may try the following.
from sklearn.externals import joblib
##Suppose your trained model is named MyTrainedModel
##This is how you save it in a file called MyTrainedModelFile.txt.
joblib.dump(MyTrainedModel, 'MyTrainedModelFile.txt')
##Later you can recall the model and use it
Loaded_model = joblib.load('MyTrainedModelFile.txt')
The tutorial is here.
Please let me know if this is what you wanted.
Upvotes: 0