DNN GridsearchCV시 score가 좀 이상합니다
미해결
안녕하세요, 딥러닝 모델을 sklearn GridsearchCV를 통해 튜닝하고 있습니다. 모델과 GridsearchCV는 아래와 같이 정의되어 있습니다. def create_model (n_filter_1st_conv , conv_n_filter , conv_kernel_sizes , n_params_1st_dense , dense_layer_sizes , dropout , lr , l1 , l2): model = Sequential() model.add(BatchNormalization()) model.add(Conv2D( filters =n_filter_1st_conv , activation =tf.nn.leaky_relu , kernel_size =(conv_kernel_sizes , conv_kernel_sizes) , padding = 'same' , strides =( 1 , 1 ) , kernel_initializer = 'he_normal' , bias_initializer = 'he_normal' , kernel_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2) , activity_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2))) for n_filters in conv_n_filter: model.add(Conv2D( filters =n_filters , activation =tf.nn.leaky_relu , kernel_size =(conv_kernel_sizes , conv_kernel_sizes) , padding = 'same' , strides =( 1 , 1 ) , kernel_initializer = 'he_normal' , bias_initializer = 'he_normal' , kernel_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2) , activity_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2))) model.add(MaxPooling2D( pool_size =( 4 , 2 ))) model.add(Dropout(dropout)) model.add(Flatten()) model.add(Dense(n_params_1st_dense , activation =tf.nn.leaky_relu , kernel_initializer = 'he_normal' , bias_initializer = 'he_normal' , kernel_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2) , activity_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2))) model.add(Dropout(dropout)) for layer_sizes_2 in dense_layer_sizes: model.add(Dense(layer_sizes_2 , activation =tf.nn.leaky_relu , kernel_initializer = 'he_normal' , bias_initializer = 'he_normal' , kernel_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2) , activity_regularizer =regularizers.l1_l2( l1 =l1 , l2 =l2))) model.add(Dense( 1 , activation = 'sigmoid' , kernel_initializer = 'he_normal' , bias_initializer = 'he_normal' )) model.compile( loss =tf.keras.losses.BinaryCrossentropy() , optimizer =Adam( learning_rate =lr) , metrics =[tf.keras.metrics.BinaryCrossentropy() , tf.keras.metrics.Accuracy() , tf.keras.metrics.BinaryAccuracy() , tf.keras.metrics.AUC()]) return model Classification_model = KerasClassifier( build_fn =create_model) X_train , X_test , y_train , y_test = train_test_split(X , y , test_size = 0.1 , random_state =rand_st) parameters = { 'n_filter_1st_conv' : [ 16 ] , 'conv_kernel_sizes' : [ 2 ] , 'conv_n_filter' : [( 16 , 32 , 64 )] , 'n_params_1st_dense' : [ 1000 ] , 'dense_layer_sizes' : [( 64 , 8 )] , 'lr' : [ 1e-3 ] , 'dropout' : [ 0.25 ] , 'l1' : [ 1e-3 ] , 'l2' : [ 1e-3 ] , 'batch_size' : [ 64 ] , 'epochs' : [ 100 ] , } grid_search = GridSearchCV( estimator =Classification_model , param_grid =parameters , refit = False, scoring = 'accuracy' , return_train_score = True, cv = 2 , error_score = "raise" ) with tf.device( '/GPU:1' ): grid_search = grid_search.fit(X_train , y_train , verbose = 1 ) # gridesearch 결과 print ( " 최고의 파라미터 :" , grid_search.best_params_) print ( " 최고 평균 accuracy : " , grid_search.best_score_) (문제를 해결하기 위해 cv=2로 두고, parameter들을 하나씩 하여 단순화해두었습니다. ) CV가 진행 중에는 아래와 같이 accuracy와 auc가 높게 나오다가, CV 1/2 Epoch 100/100 2/2 [==============================] - 0s 8ms/step - loss: 34.9196 - binary_crossentropy: 0.3310 - accuracy: 0.0000e+00 - binary_accuracy: 0.9829 - auc: 0.9911 CV 2/2 Epoch 100/100 2/2 [==============================] - 0s 10ms/step - loss: 38.7264 - binary_crossentropy: 0.3268 - accuracy: 0.0000e+00 - binary_accuracy: 0.9744 - auc_1: 0.9958 CV가 끝난 후 grid_search.best_score_로 accuracy를 뽑을 때는 아래처럼 낮은 accuracy가 나오는 것을 확인하였습니다. 최고 평균 accuracy : 0.6196581196581197 제가 알기로는 best_score_는 metric에 지정된 값에 대한 평균이라고 알고있는데.. 제가 생각하기로는 1. model compile에 넣어준 Keras loss 및 Keras metric과 GridsearchCV에서 사용하는 sklearn metric이 작동하는 방식이 달라 생기는 문제 2. CV부분에서 출력되는 loss와 metric들이 training set의 것 이라서 일어나는 게 아닐까 생각만 하고 있는데, 혹시 왜 이런 일이 일어나는지 알고계시는 분 계시면 답변 부탁드립니다.
- gridsearchcv
- sklearn
- keras
- deeplearning





