1
$\begingroup$

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.

$\endgroup$

2 Answers 2

1
$\begingroup$

The code does produce an exception indeed, but it is catched by the try-except. To avoid the exception, you could modify your local copy of the sklearn_crfsuite library and add the missing attributes (keep_tempfiles and model_filename).

  1. Edit sklearn_crfsuite/estimator.py and add this to the __init__() method of the CRF class.
self.keep_tempfiles = keep_tempfiles
self.model_filename = model_filename
  1. Remove the try-except from your code.
grid_search.fit(X_train, Y_train)

It is running fine for me with a toy dataset.

$\endgroup$
0
$\begingroup$

I don't know how could we resolve it in sci-kit version 0.24 or later but when I downgraded it to 0.23.2 version, same piece of code seems to be working fine.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.