내가 직접 코딩한 Keras Tuner, HyperParemeter 코드!

 

실행 환경: Google Colab

 

//Colab은 tf 2.x를 기본 제공하지 않아, 아래 코드 실행

%tensorflow_version 2.x

 

//train data를 매 번 업로드할 수 없으니, 구글 드라이브와 연동

from google.colab import drive

drive.mount('/content/drive')

 

//tuner 관련 소스 설치

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

 

//소스 시작

import tensorflow as tf  

import kerastuner as kt   

import pandas as pd

import numpy as np

import os

 

from tensorflow.keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense, BatchNormalization, Activation

from tensorflow.keras import Input

from sklearn.metrics import classification_report,confusion_matrix

 

from tensorflow import keras

from tensorflow.keras import layers

 

from kerastuner.tuners import RandomSearch

from kerastuner.engine.hypermodel import HyperModel

from kerastuner.engine.hyperparameters import HyperParameters

 

from kerastuner.applications import HyperResNet

from kerastuner.tuners import Hyperband



def train(hp):

 

    model_save_path = './Train_Result'

    try:

      if not(os.path.isdir(model_save_path)):

        os.makedirs(os.path.join(model_save_path))

 

    except OSError as e:

      if e.errno != errno.EEXIST:

        print('학습 결과를 저장할 폴더 생성에 실패하였습니다.')

        raise



    model = tf.keras.Sequential()

    model.add(Input(shape=input_shape))

 

    for i in range(hp.Int('num_layers'23, step=1)):

      filters_ = hp.Int('units'3296, step=32)

      kernel_size_ = hp.Int('units'35, step=1)

 

      model.add(Conv2D(filters=filters_, kernel_size=(kernel_size_, 1), strides=(11), padding='same'))

      model.add(BatchNormalization())

      model.add(Activation('relu'))

      model.add(MaxPool2D((21), padding='same'))

 

    model.add(Flatten())

 

    fc_block_info = [128]

 

    for fc_unit in fc_block_info:

      fc_unit_ = int(fc_unit)

 

      model.add(Dense(fc_unit_, activation='relu'))

      model.add(Dropout(hp.Float('drop_out_rate'0.10.3, step=0.1)))

 

    model.add(Dense(2, activation='softmax'))

    model.summary()

 

    #adam = tf.keras.optimizers.Adam(learning_rate = hp.Float('learning_rate', 1e-4, 1e-2) )

    adam = tf.keras.optimizers.Adam(hp.Float('learning_rate'1e-41e-1, sampling='log'))

    loss_fn = tf.keras.losses.SparseCategoricalCrossentropy()

 

    model.compile(optimizer = adam, 

                  loss = loss_fn,

                  metrics = ['accuracy'])

 

    model.save( model_save_path, save_format='tf' )

    

    return model



### 2.1 Data 불러오기 ###

train_data_path = './drive/My Drive/Colab Notebooks/train.csv'       

if not os.path.isfile(train_data_path):

    print('학습데이터 파일이 경로에 존재하지 않습니다.')

 

dataset         = pd.read_csv(train_data_path)

print(dataset.head())

batch_size = 10

 

feature         = dataset.iloc[:,1:3073].values.astype(float)     

label           = dataset['label'].values

nrow, ncol      = feature.shape

 

input_data_size = 3072

feature         = tf.reshape(feature, (-1, input_data_size, 11))   

input_shape     = (input_data_size, 11)

 

full_dataset    = tf.data.Dataset.from_tensor_slices((feature , label))

 

train_size      = int(0.9 * nrow)                 #train size 설정

train_dataset   = full_dataset.take(train_size)

val_dataset     = full_dataset.skip(train_size)

 

train_dataset   = train_dataset.shuffle(train_size).batch(batch_size).repeat()

val_dataset     = val_dataset.batch(batch_size)

 

val_label = []

for data, label in val_dataset:

  val_label.extend(label.numpy().tolist())




tuner = kt.Hyperband(

    train,

    objective='val_accuracy',

    max_epochs=20,

    hyperband_iterations=2)

 

tuner.search(train_dataset,

             validation_data=val_dataset,

             epochs=20,

             steps_per_epoch=train_size//batch_size,

             callbacks=[tf.keras.callbacks.EarlyStopping(patience=1)])

             

best_model = tuner.get_best_models(1)[0]

best_hyperparameters = tuner.get_best_hyperparameters(1)[0]

//소스 끝

 

설명 : 없음

ㅅㄱ

+ Recent posts