loss of previous learning when change/update in the hyper parameters #710
-
Hi Team, In online learning methodology. When the user wants to update the model after certain learnings(Batches) using set_params, Do provide any solution on suggestions on this. Below is the sample code for this scenario. Import packagesfrom sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from river import metrics
from river import ensemble
from collections import Counter load Iris data set""" Load IRIS Data set and assign feature and Target to X and y """
data_iris = load_iris()
X = data_iris.data
y = data_iris.target
X_feature_names = data_iris.feature_names
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, \
random_state=42)
## River method to calcuate accuracy from predictions
model_accuracy = metrics.Accuracy()
def river_learn(X,Y,X_feature_names,model,model_accuracy):
""" This method,trains the river model on train dataset and calculates
training accuray """
y_predicted_all = []
for x,y in zip(X,Y):
X_sample_featurename_dict = dict(zip(X_feature_names,x))
y_predicted_one = model.predict_one(X_sample_featurename_dict)
y_predicted_all.append(y_predicted_one)
model.learn_one(X_sample_featurename_dict,y)
model_accuracy.update(y,y_predicted_one)
return model_accuracy,model,y_predicted_all
def river_predict(X,Y,X_feature_names,model,model_accuracy):
""" This method,recevies trained model as parameter and
predicts on test data set and calculate accuracy """
y_predicted_all = []
for x,y in zip(X,Y):
X_sample_featurename_dict = dict(zip(X_feature_names,x))
y_predicted_one = model.predict_one(X_sample_featurename_dict)
y_predicted_all.append(y_predicted_one)
model_accuracy.update(y,y_predicted_one)
return model_accuracy,y_predicted_all
Adaptive_random_forest = ensemble.AdaptiveRandomForestClassifier() Training the modelmodel_accuracy,Trained_model_RF,y_predicted_all = river_learn(X_train,y_train,X_feature_names,Adaptive_random_forest,model_accuracy)
print(Counter(y_predicted_all[1:]))
print(model_accuracy)
# Counter({1: 39, 0: 30, 2: 30})
# Accuracy: 87.00%
"""Testing the trained_model on test data set """
model_accuracy,y_predicted_all = river_predict(X_train,y_train,X_feature_names,Trained_model_RF,model_accuracy)
print(Counter(y_predicted_all))
print(model_accuracy)
# Counter({2: 35, 1: 34, 0: 31})
# Accuracy: 91.00%
Updated_param_Trained_model_RF_1 = Trained_model_RF._set_params({'n_models':25})
model_accuracy,y_predicted_all = river_predict(X_train,y_train,X_feature_names,Updated_param_Trained_model_RF_1,model_accuracy)
print(Counter(y_predicted_all)) Counter({None: 100}) Updated_param_Trained_model_RF_2 = Trained_model_RF._set_params({'max_features':'log2'})
model_accuracy,y_predicted_all = river_predict(X_train,y_train,X_feature_names,Updated_param_Trained_model_RF_2,model_accuracy)
print(Counter(y_predicted_all)) Counter({None: 100}) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hello. I've updated your post to make it readable. Could you please make a better effort next time? Thanks though for making it somewhat reproducible. I don't have any time at the moment but I'll dig in when I can. Cheers. |
Beta Was this translation helpful? Give feedback.
-
Ok I understand. Unfortunately, you can't modify parameters on the fly. At least, the May I ask, why do you want to change parameters on-the-fly? I agree that it's a nice thing to have, but it sounds overkill for most tasks. |
Beta Was this translation helpful? Give feedback.
Ok I understand. Unfortunately, you can't modify parameters on the fly. At least, the
_set_params
method "resets" the model. You could simply update the parameters manually. But in your case you're changingn_models
, so that's not going to work.May I ask, why do you want to change parameters on-the-fly? I agree that it's a nice thing to have, but it sounds overkill for most tasks.