1. 程式人生 > >keras中訓練好的模型儲存與載入

keras中訓練好的模型儲存與載入

keras中的採用Sequential模式建立DNN並持久化保持、重新載入


def DNN_base_v1(X_train, y_train):
    
    model = models.Sequential()
    model.add(layers.Dense(96, activation='elu',kernel_regularizer=regularizers.l2(0.005), input_shape=(X_train.shape[1], )))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(64, activation='elu',kernel_regularizer=regularizers.l2(0.005)))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(32, activation='elu',kernel_regularizer=regularizers.l2(0.005)))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(32, activation='elu',kernel_regularizer=regularizers.l2(0.005)))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(1, activation='sigmoid'))

    model.compile(optimizer=optimizers.Adadelta(), loss='binary_crossentropy', metrics=['accuracy'])

    model.fit(X_train, y_train, epochs=1200, batch_size=50, validation_split=0.2, verbose=0, shuffle=True)
    results_train = model.evaluate(X_train, y_train)
    
    print('accuracy: %s' %(results_train))
    return model

def DNN_fit_and_save(X_train, y_train, doc_dir, model_numbers):
    if os.path.exists(doc_dir) == True:
        pass
    else:
        os.makedirs(doc_dir)
    for i in range(model_numbers):
        model = DNN_base_v1(X_train, y_train)
        filename = os.path.join(doc_dir, 'model_'+ np.str(i + 1)+'.h5')
        model.save(filename)
        print('>save %s' %(filename))