image data, CNN, and Bayesian optimization [keras, tensorflow] by 바죠

CNN 모델의 초월 매개변수를 베이지안 옵티마이제이션으로 처리하기

LightGBM의 초월 매개변수들을 최적화하는 것이 때때로 중요할 수 있다.

이 때, 통상, 베이지안 옵티마이제이션을 활용한다.
함수가 노이즈를 가지고 있고, 초월 매개 변수들의 숫자가 많지 않기 때문에 최고의 선택이라고 볼 수 있다.
마찬가지로, deep learning model의 초월 매개변수들도 최적화 할 수 있다.

# architecture of CNN
def buildModel(neurons,drop,nb_batch,hidden_layers):  
    nb_epoch=100
    neurons=int(neurons)
    hidden_layers=int(hidden_layers)
    # input layer
    inputs = Input(shape=x_train.shape[1:])
    x = Conv2D(neurons, (3, 3), padding='same', activation='relu')(inputs)
    x = MaxPooling2D((2, 2), padding='same')(x)
    # hidden layers
    if hidden_layers !=0:
        for i in range(1,hidden_layers+1):
            x = Conv2D(neurons*(2**i), (3, 3), padding='same', activation='relu')(x)
            x = MaxPooling2D((2, 2), padding='same')(x)
    x = Flatten()(x)
    x = Dense(neurons*(2**(hidden_layers+1)), activation='relu')(x)
    x = Dropout(drop)(x)
    # output
    predictions = Dense(nb_classes, activation='softmax')(x)
    # modeling
    model = Model(inputs=inputs, outputs=predictions)
    model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
    early_stopping = EarlyStopping(patience=10, verbose=1)
    # learning
    hist = model.fit(x_train, y_train, validation_split=0.2, epochs=int(nb_epoch), batch_size=int(nb_batch),callbacks=[early_stopping])
    # evaluate the model with test data
    score = model.evaluate(x_test, y_test)
    print("loss : ",score[0])
    print("accuracy : ",score[1])
    acc.append(score[1])
    return score[0]*(-1)
# baeysian optimization
def bayesOpt():
    pbounds = {
        # define the range of each parameter
        'drop' : (0,0.9),
        "neurons" : (2,50),
        'nb_batch' : (4,100),
        'hidden_layers' : (0,6)
    }
    optimizer = BayesianOptimization(f=buildModel, pbounds=pbounds)
    #  init_points: number of initial points
    #  n_iter: number of iteration
    #  acq:acquire function, ucb as default
    optimizer.maximize(init_points=10, n_iter=30, acq='ucb')
    return optimizer
# run Bayesian optimization
acc =[]
result = bayesOpt()
result.res
result.max


# load training and test data sets
x_train = np.load(path+"/data/x_train.npy")
y_train = np.load(path+"/data/y_train.npy")
x_test = np.load(path +"/data/X_test.npy")
y_test = np.load(path +"/data/Y_test.npy")
# normalize
x_train = x_train/255
x_test = x_test/255
y_train = np_utils.to_categorical(y_train,nb_classes)
y_test = np_utils.to_categorical(y_test,nb_classes)
# shuffle sequence of data
index=list(range(x_train.shape[0]))
index=random.sample(index,len(index))
x_train = x_train[index]
y_train = y_train[index]
# pick color channels
if nb_ch == 2:
    x_train=x_train[...,:2]
    x_test=x_test[...,:2]
else:
    x_train=x_train[...,single_ch]
    x_test=x_test[...,single_ch]
    x_train=x_train[...,None]
    x_test=x_test[...,None]
print(x_train.shape)
print(x_test.shape)
def buildModel(neurons,drop,nb_batch,hidden_layers):  
    nb_epoch=100
    neurons=int(neurons)
    hidden_layers=int(hidden_layers)
    # input layer
    inputs = Input(shape=x_train.shape[1:])
    x = Conv2D(neurons, (3, 3), padding='same', activation='relu')(inputs)
    x = MaxPooling2D((2, 2), padding='same')(x)
    # hidden layers
    if hidden_layers !=0:
        for i in range(1,hidden_layers+1):
            x = Conv2D(neurons*(2**i), (3, 3), padding='same', activation='relu')(x)
            x = MaxPooling2D((2, 2), padding='same')(x)
    x = Flatten()(x)
    x = Dense(neurons*(2**(hidden_layers+1)), activation='relu')(x)
    x = Dropout(drop)(x)
    # output
    predictions = Dense(nb_classes, activation='softmax')(x)
    # modeling
    model = Model(inputs=inputs, outputs=predictions)
    model.compile(optimizer='rmsprop',  loss='binary_crossentropy',  metrics=['accuracy'])
    early_stopping = EarlyStopping(patience=10, verbose=1)
    # learning
    hist = model.fit(x_train, y_train, validation_split=0.2, epochs=int(nb_epoch), batch_size=int(nb_batch),callbacks=[early_stopping])
    # evaluate the model with test data
    score = model.evaluate(x_test, y_test)       
    print("loss : ",score[0])
    print("accuracy : ",score[1])
    acc.append(score[1]) 
    return score[0]*(-1)
# functional model
inputs = Input(shape=x_train.shape[1:])
# hyperparameters
drop = 0.3          # dropout rate
hidden_layers = 2   # number of hidden convolutional/max pooling layer sets
batch = 89          # batch size
neurons = 28        # number of convolution kernels
x = Conv2D(neurons, (3, 3), padding='same', activation='relu')(inputs)
x = MaxPooling2D((2, 2), padding='same')(x)
if hidden_layers !=0:
    for i in range(1,hidden_layers+1):
        x = Conv2D(neurons*(2**i), (3, 3), padding='same', activation='relu')(x)
        x = MaxPooling2D((2, 2), padding='same')(x)
x = Flatten()(x)
x = Dense(neurons*(2**(hidden_layers+1)), activation='relu')(x)
x = Dropout(drop)(x)
predictions = Dense(nb_classes, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
display(model.summary())
early_stopping = EarlyStopping(patience=5, verbose=1)
# learning
hist = model.fit(x_train, y_train, validation_split=0.2, epochs=100, batch_size=batch,callbacks=[early_stopping])
# evaluated accuracy for test data
print(model.evaluate(x_test, y_test))
# make graphs
fig, (axL, axR) = plt.subplots(ncols=2,figsize=(16,8))
acc = hist.history['accuracy']             # accuracy for training data
val_acc = hist.history['val_accuracy']     # accuracy for validation data
loss = hist.history['loss']           # loss for training data
val_loss = hist.history['val_loss']   # loss for validation
plt.rcParams["font.size"] = 16
axL.plot(acc,label='Training acc')
axL.plot(val_acc,label='Validation acc')
axL.set_title('Accuracy')
axL.legend(loc='best')                # position of legend
axR.plot(loss,label='Training loss')
axR.plot(val_loss,label='Validation loss')
axR.set_title('Loss')
axR.legend(loc='best')
plt.show()
# save model and parameters
model_json = model.to_json()
open(path + "/model/"+model_name+".json", "w").write(model_json)
hdf5_file = path + "/model/"+model_name+".hdf5"
model.save_weights(hdf5_file)

curve = []
curve.append([acc,val_acc,loss,val_loss])
curve = np.array(curve[0])
curve = curve.T
curve_df = pd.DataFrame(curve, columns=["train_acc","val_acc","train_loss","val_loss"])
curve_df.to_csv(path + "/model/" + model_name + "_curve.csv")
print(curve_df)
loss_fin = model.evaluate(x_test,y_test)[0]
acc_fin = model.evaluate(x_test,y_test)[1]
fin_df = pd.DataFrame(np.array([acc_fin,loss_fin]), index=["test_acc","test_loss"])
fin_df.to_csv(path + "/model/" + model_name + "_test-acc.csv")
print(fin_df)
# load saved model
with open(path+"/model/"+model_name+".json", "r") as f:
    model = model_from_json(f.read())
model.load_weights(path+"/model/"+model_name+".hdf5")
model.summary()

--------------------------------------------------------------------------------------------------------------
    # evaluate the model
    scores = model.evaluate(X_test, y_test, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    model.save_weights('my_model_weights.h5')
    print('weights writing')

if True:
    if os.path.exists('my_model_weights.h5'):
        model.load_weights('my_model_weights.h5')
        print('reading weights')

--------------------------------------------------------------------------------------------------------------






덧글

  • 바죠 2021/03/01 11:35 # 답글

    list1=['x1','x2']
    list2=[(0. , 2.), (0., 3.)]
    dict( zip( list1, list2))
  • 바죠 2021/03/12 13:45 # 답글

    https://www.youtube.com/watch?v=LtBeRblni7M
  • 바죠 2021/07/17 14:59 # 답글

    if True:
    print("Saving model...")
    train_dataset = tf.data.Dataset.from_tensor_slices(X_train).batch(BATCH_SIZE)
    test_dataset = tf.data.Dataset.from_tensor_slices(X_test).batch(BATCH_SIZE)
    input_shape_set=False
    if not input_shape_set:
    # Note: Why set input shape manually and why here:
    # 1. If I do not set input shape manually: ValueError: Model <main.CVAE object at 0x7f1cac2e7c50> cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit() or .predict(). To manually set the shapes, call model._set_inputs(inputs).
    # 2. If I set input shape manually BEFORE the first actual train step, I get: RuntimeError: Attempting to capture an EagerTensor without building a function.
    model._set_inputs(train_dataset.__iter__().next())
    input_shape_set = True
    # Note: Why choose tf format: model.save('my_model.h5') will return NotImplementedError:
    # Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model.
    # It does not work for subclassed models, because such models are defined via the body of a Python method,
    # which isn't safely serializable.
    # Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using save_weights.
    model.save('my_model', save_format='tf' , overwrite=True, include_optimizer=True)
  • 바죠 2021/07/18 19:07 # 답글

    --------------------------------------------------------------------------------------------------------------
    # evaluate the model
    scores = model.evaluate(X_test, y_test, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    model.save_weights('my_model_weights.h5')
    print('weights writing')

    if True:
    if os.path.exists('my_model_weights.h5'):
    model.load_weights('my_model_weights.h5')
    print('reading weights')

    -
댓글 입력 영역

최근 포토로그



MathJax