RandomSearch๋ฅผ ์ด์ฉํ ํ์ดํผ ํ๋ผ๋ฏธํฐ ํ๋
0. ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ ๋ฐ Normalization
# ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
from tensorflow.keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# input ๋ฐ target ๋ฐ์ดํฐ ํ์ธ
X_train.shape
set(y_train)
# Normalization
X_train = X_train / 255.
X_test = X_test / 255.
1. ๋ชจ๋ธ๋ง
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(X_train, y_train, epochs=30, batch_size=32, validation_data=(X_test, y_test))
# ๋ฒ ์ด์ค๋ชจ๋ธ ํ
์คํธ
model.evaluate(X_test, y_test, verbose=2)
2. ํ์ดํผ ํ๋ผ๋ฏธํฐ ํ๋
2-1. sklearn์ RandomizedSearchCV
from tensorflow.keras.regularizers import L1, L2
from sklearn.model_selection import RandomizedSearchCV
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
# ๋ชจ๋ธ ์ค๊ณ
def build_model_rscv(nodes, L1_values, L2_values, dropout_values):
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(nodes, activation='relu',
kernel_regularizer=L2(L2_values)))
model.add(Dense(nodes, activation='relu',
activity_regularizer=L1(L1_values)))
model.add(Dropout(dropout_values))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
# KerasClassifier๋ก wrapping
temp_rscv = KerasClassifier(build_fn=build_model_rscv, verbose=0)
# params ์ค์ ๋ฐ RandomizedSearchCV
import random
nodes=[random.randint(32, 128) for n in range(5)]
L1_values=[random.uniform(0, 0.000001) for n in range(5)]
L2_values=[random.uniform(0, 0.000001) for n in range(5)]
dropout_values=[random.uniform(0, 1) for n in range(5)]
batch_sizes=[random.randint(32, 64) for n in range(5)]
epochs=[random.choice(range(30, 160, 10)) for n in range(5)]
params = dict(nodes=nodes,
L1_values=L1_values,
L2_values=L2_values,
dropout_values=dropout_values,
batch_size=batch_sizes,
epochs=epochs
)
model_best = RandomizedSearchCV(estimator=temp_rscv,
param_distributions=params,
n_iter=10,
cv=3,
scoring='accuracy',
verbose=1,
n_jobs=-1
)
model_best.fit(X_train, y_train, validation_data=(X_test, y_test))
import pandas as pd
rs = pd.DataFrame(model_best.cv_results_).sort_values(by='rank_test_score').head()
rs.T
2-2. keras_tuner์ RandomSearch
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout
!pip install -U keras-tuner
import kerastuner as kt
# ๋ชจ๋ธ๋ง(์๋์ธต์ ๋
ธ๋์์๋ง ํ๋ํด๋ณด์์ต๋๋ค.)
def model_builder(hp):
model = Sequential()
hp_units = hp.Int('units', min_value=32, max_value=512, step=64) # python์ range์ ๋น์ทํ ๊ฐ๋
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(hp_units, activation='relu'))
model.add(Dense(hp_units, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics='accuracy')
return model
# ํ๋ ์ธํ
tuner = kt.RandomSearch(
hypermodel=model_builder,
objective='val_loss',
max_trials=5
)
# ์ต์ ์ ํ์ดํผ ํ๋ผ๋ฏธํฐ๊ฐ ์์นญ
tuner.search(X_train, y_train, epochs=10, validation_data=(X_test, y_test))
best_hps = tuner.get_best_hyperparameters(num_trials = 1)[0]
model = tuner.hypermodel.build(best_hps)
model.fit(X_train, y_train, epochs = 10, validation_data = (X_test, y_test))
์ด์์ ๋๋ค. ๊ฐ์ฌํฉ๋๋ค. :)
'๐ฟ Data > ์ด๋ชจ์ ๋ชจ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฅ๋ฌ๋, NLP] ๋ถํฌ ๊ฐ์ค, Word2Vec (0) | 2022.03.06 |
---|---|
[๋ฅ๋ฌ๋, NLP] ๋ถ์ฉ์ด, ์ถ์ถ, BoW/TF-IDF (0) | 2022.03.06 |
[๋ฅ๋ฌ๋]keras_cifar100 ์ด์ฉํ ๊ฐ๋จ ์ ๊ฒฝ๋ง ๋ฐ ๊ณผ์ ํฉ ๋ฐฉ์ง, ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋ (0) | 2022.02.26 |
[๋ฅ๋ฌ๋]๊ฐ๋จ ์ ๊ฒฝ๋ง ๋ฐ ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ๋ง, ์ฑ๋ฅ ๋น๊ต (0) | 2022.02.26 |
[๋ฅ๋ฌ๋]์ตํฐ๋ง์ด์ (Optimizer) (0) | 2022.02.24 |