본문 바로가기

Deep Learning/Tensorflow

Tensorflow - GridSearch 방법

반응형
Grid Search

 

GridSearch를 이용한 최적의 하이퍼파라미터 찾기

 

 

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense

 

def build_classifier(optimizer) :
  # 모델링
  model = Sequential()
  model.add(Dense(units=6, activation= 'relu', input_shape = (11,)))
  model.add(Dense(units=8, activation= 'relu' ))
  model.add(Dense(units=1, activation= 'sigmoid' ))
  model.compile(optimizer = optimizer, loss = 'binary_crossentropy', metrics = ['accuracy'])
  return model
model = KerasClassifier(build_fn = build_classifier)

 

my_parameters = {'epochs' : [30,50], 'batch_size':[10,20], 'optimizer' : ['adam','rmsprop']}
grid_search = GridSearchCV(estimator=model, param_grid=my_parameters, scoring='accuracy')
grid_search.fit(X_train,y_train)

 

grid_search.best_params_

 

grid_search.best_score_

 

model2=grid_search.best_estimator_
model2

 

반응형