ML (MachineLearning)

GridSearch ๋ฅผ ์ด์šฉํ•œ ์ตœ์ ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฐพ๊ธฐ

567Rabbit 2024. 4. 17. 12:36

https://www.tensorflow.org/api_docs/python/tf/keras?_gl=1*7074vd*_up*MQ..*_ga*MjEzMDk5NTY2NC4xNzEzNDg3ODQ0*_ga_W0YLR4190T*MTcxMzQ4Nzg0NC4xLjAuMTcxMzQ4Nzg0NC4wLjAuMA..

 

Module: tf.keras  |  TensorFlow v2.16.1

DO NOT EDIT.

www.tensorflow.org

 

 

! pip install scikeras

 

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

 

def build_model(optimizer = 'adam') :
  model = Sequential()
  model.add(Dense(8, 'relu', input_shape=(11,)))
  model.add(Dense(6, 'relu'))
  model.add(Dense(1, 'sigmoid'))
  model.compile(optimizer = optimizer, loss = 'binary_crossentropy', metrics = ['accuracy'])
  return model

 

 

grid.best_params_      # ํŒŒ๋ผ๋ฏธํ„ฐ ์ •๋ณด

{'batch_size': 10, 'epochs': 30, 'optimizer': 'rmsprop'}

 

grid.best_score_       # ์ •ํ™•๋„

0.862125

 

 

best_model = grid.best_estimator_

best_model.predict(X_test)

array([0, 0, 0, ..., 0, 0, 0])

 

 

best_model.predict_proba(X_test)   # ํ™•๋ฅ ๋กœ ๋ณด๊ธฐ

 
array([[0.7514862 , 0.24851382],
       [0.6307688 , 0.36923125],
       [0.8103051 , 0.18969488],
       ...,
       [0.8100885 , 0.1899115 ],
       [0.81499493, 0.1850051 ],
       [0.85258085, 0.14741915]], dtype=float32)

 

 

 

๊ฒฐ๊ณผ ๋ณด๊ธฐ

model = build_model()

model.summary()

model.fit(X_train, y_train, epochs=500, validation_split=0.2)    
# 80%๋Š” ํ•™์Šต์‹œํ‚ค๊ณ  20%๋Š” ์‹œํ—˜์‹œํ‚จ๋‹ค

 

# validation์ด๋ž€ ๊ฒ€์ฆ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•œ๋‹ค.

 

- ํ…Œ์ŠคํŠธ? ์ธ๊ณต์ง€๋Šฅ ํ•™์Šต์ด ์™„์ „ํžˆ ๋‹ค ๋๋‚ฌ์„ ๋•Œ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ ex)์ˆ˜๋Šฅ
- validation? ์—ํฌํฌ๊ฐ€ ํ•œ๋ฒˆ ๋๋‚ ๋•Œ๋งˆ๋‹ค ํ•™์Šต์— ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋กœ ์‹œํ—˜์„ ๋ณด๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ex)๋ชจ์˜๊ณ ์‚ฌ

 

 

-์—ํฌํฌ๋ฅผ 500๋ฒˆ ํ–ˆ์„ ๋•Œ, ์–ด๋Š ์ˆœ๊ฐ„๋ถ€ํ„ฐ ๋จธ์‹ ๋Ÿฌ๋‹ ํ•™์Šต์—์„œ loss(์˜ค์ฐจ๊ฐ€) ๋” ๋Š˜์–ด๋‚˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์ด๋ฅผ overfitting(๊ณผ์ ํ•ฉ)ํ˜„์ƒ์ด๋ผ๊ณ  ํ•œ๋‹ค. overfitting(๊ณผ์ ํ•ฉ)ํ˜„์ƒ์ด๋ž€ ๊ณผ๊ฑฐ๋ฐ์ดํ„ฐ๋ฅผ ์ง€๋‚˜์น˜๊ฒŒ ๋งŽ์ด ํ•™์Šตํ•˜์—ฌ ๊ณผ๊ฑฐ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ๋Š” ์ค„์–ด๋“ค์ง€๋งŒ ๋ฏธ๋ž˜์— ๋Œ€ํ•œ ์˜ˆ์ธก์—๋Š” ๋” ํฐ ์˜ค์ฐจ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

 

 

 

 

 

์ตœ์ ์˜ ์—ํฌํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด?

model.fit ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๊ฒ€์ฆ ์ ์ˆ˜๊ฐ€ ํ–ฅ์ƒ๋˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ํ›ˆ๋ จ์„ ๋ฉˆ์ถ”๋„๋ก ๋งŒ๋“ค์–ด ๋ณธ๋‹ค. ์—ํฌํฌ๋งˆ๋‹ค ํ›ˆ๋ จ ์ƒํƒœ๋ฅผ ์ ๊ฒ€ํ•˜๊ธฐ ์œ„ํ•ด EarlyStopping ์ฝœ๋ฐฑ(callback)์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ง€์ •๋œ ์—ํฌํฌ ํšŸ์ˆ˜ ๋™์•ˆ ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ์—†์œผ๋ฉด ์ž๋™์œผ๋กœ ํ›ˆ๋ จ์ด ๋ฉˆ์ถ˜๋‹ค

 

model = build_model()     # def๋ฅผ ๋นˆ ๊นกํ†ต ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด์คŒ

early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
# patience๋ฅผ 5๋กœ ์„ค์ •ํ•˜๋ฉด, 5๋ฒˆ์˜ ์—ํฌํฌ๋™์•ˆ ์„ฑ๋Šฅํ–ฅ์ƒ์ด ์—†์œผ๋ฉด ํ•™์Šต์„ ๋ฉˆ์ถ”๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.


from keras.callbacks import EarlyStopping

epoch_history = model.fit(X_train, y_train, epochs = 100000, validation_split=0.2, callbacks=[early_stop])

 

 

 

 

์ตœ์ข… ์‹œํ—˜๋ณด๊ธฐ

model.evaluate(X_test, y_test)

[==============================] - 0s 6ms/step - loss: 0.1021 - mae: 0.2288
[0.10210247337818146, 0.22881145775318146]