pythonscikit-learnhyperparametersfasttextgridsearchcv

How to use GridSearchCV (python) for maximizing or minimizing a function with parameters?


I would like to maximize a function: func(minCount, wordNgrams, lr, epoch, loss) with GridSearch on only these values:

`{'minCount': [2, 3],
'wordNgrams': [1, 2, 3, 4, 5],
'lr': [0.1, 0.01, 0.001, 0.0001],
'epoch': [5, 10, 15, 20, 25, 30],
'loss': [hs, ns, softmax]}`

I have read about sklearn.model_selection.GridSearchCV(estimator, param_grid, ...) But, I don't know, to where I should put my func(minCount, wordNgrams, lr, epoch, loss)

By the way, I've read about bayesian optimization (https://github.com/fmfn/BayesianOptimization), but don't have any understanding of how to use this with string and int parameters


Solution

  • According to the documentation , you have two solutions:

    def my_scoring_function(func_outputs):
    
      """
      process the outputs of func and return a score. 
    
      if func already reutrns the value you want to minimize, 
      my_scoring_function will be the identity function.
    
      score is the value to optimize
      """
    
      return score
    
    
    cv = GridSearchCV(estimator=func, param_grid=my_param_grid, scoring=my_scoring_function)
    



    Regarding bayesian optimisation, it is interesting if your problem meets the following criterions:

    For more details on bayesian optimization I would recommend this article that I find very comprehensive.