How to perform Grid Search on NLP CRF model
I am trying to perform hyperparameter tuning on sklearn_crfsuite.CRF model. When I try to execute below code, it doesn't give any exception but it probably fails to perform fit. And due to which, if I try to get best estimator from grid search, it doesn't work.
%%time
# define fixed parameters and parameters to search
crf = sklearn_crfsuite.CRF(
algorithm='lbfgs',
max_iterations=100,
all_possible_transitions=True
)
params_space = {
c1: [0,0.05,0.1, 0.25,0.5,1],
c2: [0,0.05,0.1, 0.25,0.5,1]
}
# use the same metric for evaluation
f1_scorer = make_scorer(metrics.flat_f1_score,
average='weighted', labels=labels)
# search
grid_search = GridSearchCV(estimator=crf,
param_grid=params_space,
cv=3,
n_jobs=-1, verbose=1,scoring=f1_scorer)
#grid_search.fit(X_train, Y_train)
#above code throws exception, which seems to be a open bug in latest version of scikit-learn 0.24.0 or later.
#github link for bug: https://github.com/TeamHG-Memex/sklearn-crfsuite/issues/60
try:
grid_search.fit(X_train, Y_train)
except AttributeError as e:
if 'CRF' object has no attribute 'keep_tempfiles' not in str(e):
raise
Any help would be appreciated, how can I perform hyperparameter tuning here?
I took reference using this tutorial, but stuck in same situation.
Topic machine-learning-model gridsearchcv nlp
Category Data Science