2

tflearn 모델의 하이퍼 매개 변수를 통해 그리드 검색을 수행하려고합니다. tflearn.DNN에 의해 생성 된 모델은 sklearn의 GridSearchCV 기대와 호환되지 않습니다 보인다 :sklearn의 GridSearchCV를 사용하여 tflearn을 실행할 수 없습니다.

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에 적합한 개체를 얻을 수있는 방법 :

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) 

내가 오류가?

답변

0

저는 tflearn에 대한 경험이 없지만 파이썬과 sklearn에서 기본적인 배경 지식을 가지고 있습니다. StackOverflow 스크린 샷의 오류로 판단하면 tflearn ** models **에는 scikit-learn estimators와 동일한 메소드 나 속성이 없습니다. 이것은 그들이 잘, scikit-estim estimators가 아니기 때문에 이해할 수 있습니다.

Sklearn의 그리드 검색 CV는 scikit-lear estimators (예 : fit() 및 predict() 메소드 포함)와 동일한 메소드 및 속성을 가진 객체에서만 작동합니다. sklearn의 그리드 검색을 사용하려는 경우, sklearn 견적서 대신에 드롭으로 작동하도록 tflearn 모델 주위에 독자적인 래퍼를 작성해야합니다. 즉, 같은 스케치를 사용하여 자신 만의 클래스를 작성해야합니다. 메소드를 다른 scikit-lear 견적 기와 같지만 tflearn 라이브러리를 사용하여 실제로 이러한 메소드를 구현합니다.

기본 scikit-learn 견적 도구에 대한 코드를 이해하고 (예 : 잘 아는 사람이 바람직 함) 실제로 fit(), predict(), get_params() 등이 객체에 실제로하는 작업을 확인하고 그 내부. 그런 다음 tflearn 라이브러리를 사용하여 클래스를 작성하십시오.

빠른 Google 검색을 통해이 저장소가 "tensorflow 프레임 워크를위한 얇은 scikit-learn 스타일 래퍼"인 것으로 나타났습니다. DSLituiev/tflearn (https://github.com/DSLituiev/tflearn). 이것이 그리드 서치의 대체품으로 사용될 지 모르겠지만, 한 번 볼 가치가 있습니다.

관련 문제