๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Machine Learning/๋”ฅ๋Ÿฌ๋‹

Keras Tuner

๋ฐ˜์‘ํ˜•

์„ค์น˜

ํ•„์š”:

  • Python 3.6

  • TensorFlow 2.0 Beta

pip install git+https://github.com/keras-team/keras-tuner.git

 

๊ธฐ๋ณธ ์‚ฌํ•ญ

random search๋ฅผ ์‚ฌ์šฉํ•ด์„œ a single-layer dense neural network ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํŠœ๋‹์„ ํ•ด๋ณด์ž.

 

๋จผ์ €, model-building ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. hp๋Š” hyperparameter๋ฅผ ์ƒ˜ํ”Œ๋ง ํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ˆ˜์ด๋‹ค.

ex) hp.Range('units', min_value=32, max_value=512, step=32) (ํŠน์ • ๋ฒ”์œ„์˜ ์ •์ˆ˜)

 

return์€ ์ปดํŒŒ์ผ๋œ model

from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch


def build_model(hp):
    model = keras.Sequential()
    model.add(layers.Dense(units=hp.Range('units',
                                          min_value=32,
                                          max_value=512,
                                          step=32),
                           activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate',
                      values=[1e-2, 1e-3, 1e-4])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model

 

 

๋‹ค์Œ์€, tuner๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•œ๋‹ค.

 

์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ tuner๋กœ RandomSearch, Hyperband๊ฐ€ ์žˆ์Œ

 

์ฐธ๊ณ  : executions per trial์„ ์—ฌ๋Ÿฌ๋ณ€ ํ•˜๋Š” ์ด์œ ๋Š” ๊ฒฐ๊ณผ ์ฐจ์ด๋ฅผ ์ค„์—ฌ์„œ ๋ชจ๋ธ ์„ฑ๋Šฅ์„ ์ •ํ™•ํ•˜๊ฒŒ ํ‰๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ณผ๋ฅผ ๋นจ๋ฆฌ ์–ป์œผ๋ ค๋ฉด executions_per_trial=1๋กœ ํ•œ๋‹ค.

tuner = RandomSearch(
    build_model, # model-building ํ•จ์ˆ˜
    objective='val_accuracy', # ์ตœ์ ํ™” ํ•  objective (
    max_trials=5, # ํ…Œ์ŠคํŠธํ•  trials ์ˆ˜
    executions_per_trial=3, # ๊ฐ trial์— built & fit์— ํ•„์š”ํ•œ ๋ชจ๋ธ ์ˆ˜ 
    directory='my_dir',
    project_name='helloworld')

 

the search space ์š”์•ฝ ๋ณด๊ธฐ:

tuner.search_space_summary()

 

์ด์ œ ์ตœ๊ณ ์˜ hyperparameter configuration ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค. search ํ˜ธ์ถœ์€ model.fit() ๊ณผ ๊ฐ™์Œ

tuner.search(x, y,
             epochs=5,
             validation_data=(val_x, val_y))

 

search์—์„œ ์ผ์–ด๋‚˜๋Š” ์ผ : ๋ชจ๋ธ์€ hp ๊ฐ์ฒด๊ฐ€ ์ถ”์ ํ•˜๋Š” hyperparameter space (search space)์„ ์ฑ„์šฐ๋Š” model-building ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋นŒ๋“œ๋œ๋‹ค. 

 

๊ฒ€์ƒ‰์ด ๋๋‚˜๋ฉด ์ตœ๊ณ ์˜ ๋ชจ๋ธ์„ ์–ป์„์ˆ˜ ์žˆ๋‹ค.

models = tuner.get_best_models ( num_models = 2 )

 

๊ฒฐ๊ณผ ์š”์•ฝ ๋ณด๊ธฐ ~

tuner.results_summary ()

search space๋Š” ์กฐ๊ฑด๋ถ€ hyperparameters ๊ฐ€๋Šฅ 

def build_model(hp):
    model = keras.Sequential()
    for i in range(hp.Range('num_layers', 2, 20)):
        model.add(layers.Dense(units=hp.Range('units_' + str(i),
                                              min_value=32,
                                              max_value=512,
                                              step=32),
                               activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model

 

model-building function ๋Œ€์‹  HyperModel subclass ์‚ฌ์šฉ

build(self, hp) method๋งŒ ๊ตฌํ˜„ํ•˜๋ฉด hypermodels ์žฌ์‚ฌ์šฉ&๊ณต์œ ๋ฅผ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

from kerastuner import HyperModel


class MyHyperModel(HyperModel):

    def __init__(self, num_classes):
        self.num_classes = num_classes

    def build(self, hp):
        model = keras.Sequential()
        model.add(layers.Dense(units=hp.Range('units',
                                              min_value=32,
                                              max_value=512,
                                              step=32),
                               activation='relu'))
        model.add(layers.Dense(self.num_classes, activation='softmax'))
        model.compile(
            optimizer=keras.optimizers.Adam(
                hp.Choice('learning_rate',
                          values=[1e-2, 1e-3, 1e-4])),
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy'])
        return model


hypermodel = MyHyperModel(num_classes=10)

tuner = RandomSearch(
    hypermodel,
    objective='val_accuracy',
    max_trials=10,
    directory='my_dir',
    project_name='helloworld')

tuner.search(x, y,
             epochs=5,
             validation_data=(val_x, val_y))

 

from kerastuner import HyperParameters

hypermodel = HyperXception(input_shape=(128, 128, 3), num_classes=10)

hp = HyperParameters()
# This will override the `learning_rate` parameter with your
# own selection of choices
hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])

tuner = Hyperband(
    hypermodel,
    hyperparameters=hp,
    # `tune_new_entries=False` prevents unlisted parameters from being tuned
    tune_new_entries=False,
    objective='val_accuracy',
    max_trials=40,
    directory='my_dir',
    project_name='helloworld')

tuner.search(x, y,
             epochs=20,
             validation_data=(val_x, val_y))

 

parameter ๊ธฐ๋ณธ ๊ฐ’

model-building function ์ด๋‚˜ hypermodel์˜ build ๋ฉ”์†Œ๋“œ์— hyperparameter ๋“ฑ๋ก ํ•  ๋•Œ ๊ธฐ๋ณธ ๊ฐ’ ์ง€์ • ๊ฐ€๋Šฅํ•จ:

hp.Range('units',
         min_value=32,
         max_value=512,
         step=32,
         default=128)

์•ˆํ•˜๋ฉด hyperparameters ๋Š” ํ•ญ์ƒ default ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. (Range์˜ ๊ฒฝ์šฐ it is equal to min_value)

 

 

์ฐธ๊ณ 

https://github.com/keras-team/keras-tuner

 

keras-team/keras-tuner

Hyperparameter tuning for humans. Contribute to keras-team/keras-tuner development by creating an account on GitHub.

github.com

 

 

 

 

๋ฐ˜์‘ํ˜•

'Machine Learning > ๋”ฅ๋Ÿฌ๋‹' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ๊ต์ฐจ๊ฒ€์ฆ (Cross Validation)  (0) 2019.08.21
[Machine Learning] Train data normalization  (0) 2019.07.12
MDN  (0) 2019.07.11
Hyperparameter Tuning  (1) 2019.07.11
Relu ํ•จ์ˆ˜  (0) 2019.06.28