2

tflearn モデルのハイパーパラメータに対してグリッド検索を実行するつもりです。によって生成されたモデルtflearn.DNNは、sklearn の GridSearchCV の期待と互換性がないようです:

from sklearn.grid_search import GridSearchCV
import tflearn
import tflearn.datasets.mnist as mnist
import numpy as np

X, Y, testX, testY = mnist.load_data(one_hot=True)

encoder = tflearn.input_data(shape=[None, 784])
encoder = tflearn.fully_connected(encoder, 256)
encoder = tflearn.fully_connected(encoder, 64)

# Building the decoder
decoder = tflearn.fully_connected(encoder, 256)
decoder = tflearn.fully_connected(decoder, 784)

# Regression, with mean square error
net = tflearn.regression(decoder, optimizer='adam', learning_rate=0.01,
                         loss='mean_square', metric=None)

model = tflearn.DNN(net, tensorboard_verbose=0)

grid_hyperparams = {'optimizer': ['adam', 'sgd', 'rmsprop'], 'learning_rate': np.logspace(-4, -1, 4)}
grid = GridSearchCV(model, param_grid=grid_hyperparams, scoring='mean_squared_error', cv=2)
grid.fit(X, X)

エラーが発生します:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-fd63245cd0a3> in <module>()
     22 grid_hyperparams = {'optimizer': ['adam', 'sgd', 'rmsprop'], 'learning_rate': np.logspace(-4, -1, 4)}
     23 grid = GridSearchCV(model, param_grid=grid_hyperparams, scoring='mean_squared_error', cv=2)
---> 24 grid.fit(X, X)
     25 
     26 

/home/deeplearning/anaconda3/lib/python3.5/site-packages/sklearn/grid_search.py in fit(self, X, y)
    802 
    803         """
--> 804         return self._fit(X, y, ParameterGrid(self.param_grid))
    805 
    806 

/home/deeplearning/anaconda3/lib/python3.5/site-packages/sklearn/grid_search.py in _fit(self, X, y, parameter_iterable)
    539                                          n_candidates * len(cv)))
    540 
--> 541         base_estimator = clone(self.estimator)
    542 
    543         pre_dispatch = self.pre_dispatch

/home/deeplearning/anaconda3/lib/python3.5/site-packages/sklearn/base.py in clone(estimator, safe)
     45                             "it does not seem to be a scikit-learn estimator "
     46                             "as it does not implement a 'get_params' methods."
---> 47                             % (repr(estimator), type(estimator)))
     48     klass = estimator.__class__
     49     new_object_params = estimator.get_params(deep=False)

TypeError: Cannot clone object '<tflearn.models.dnn.DNN object at 0x7fead09948d0>' (type <class 'tflearn.models.dnn.DNN'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

GridSearchCV に適したオブジェクトを取得する方法を教えてください。

4

1 に答える 1