在各种迭代中选择最佳性能模型

时间:2020-10-19 13:14:46

标签: python tensorflow keras neural-network

我想创建一个for循环,以便多次运行我的模型并为每次运行保留性能最佳的模型。这是因为我已经注意到,每次训练模型时,它在一个运行中的性能可能会更好,而在另一运行中的性能可能会差很多。因此,我想将每个模型都存储在列表中,或者只是选择最佳模型。

我有当前的流程,但是我不确定这是否是最合适的方法,并且我实际上也不确定如何在所有这些迭代中选择性能最佳的模型。在这里,我只进行10次迭代,但是我想知道是否有更好的方法可以做到这一点。

我的代码实现

def build_model(input1, input2):
    
    """
    Creates the a multi-channel ANN, capable of accepting multiple inputs.

    :param: none
    :return: the model of the ANN with a single output given
    """
    
    input1 = np.expand_dims(input1,1)

    # Define Inputs for ANN
    input1 = Input(shape = (input1.shape[1], ), name = "input1")
    input2 = Input(shape = (input2.shape[1],), name = "input2")

    # First Branch of ANN (Weight)
    x = Dense(units = 1, activation = "relu")(input1)
    x = BatchNormalization()(x)  

    # Second Branch of ANN (Word Embeddings)
    y = Dense(units = 36, activation = "relu")(input2)
    y = BatchNormalization()(y)  
    
    # Merge the input models into a single large vector
    combined = Concatenate()([x, y])
    
    #Apply Final Output Layer
    outputs = Dense(1, name = "output")(combined)

    # Create an Interpretation Model (Accepts the inputs from previous branches and has single output)
    model = Model(inputs = [input1, input2], outputs = outputs)

    # Compile the Model
    model.compile(loss='mse', optimizer = Adam(lr = 0.01), metrics = ['mse'])

    # Summarize the Model Summary
    model.summary()
    
    return model


test_outcomes = [] # list of model scores
r2_outcomes = [] #list of r2 scores
stored_models = [] #list of stored_models

for i in range(10):
    model = build_model(x_train['input1'], x_train['input2'])
    print("Model Training")
    model.fit([x_train['input1'], x_train['input2']], y_train, 
                    batch_size = 25, epochs = 60, verbose = 0 #, validation_split = 0.2
                    ,validation_data = ([x_valid['input1'],x_valid['input2']], y_valid))
    
    #Determine Model Predictions
    print("Model Predictions")
    y_pred = model.predict([x_valid['input1'], x_valid['input2']])
    y_pred = y_pred.flatten()

    #Evaluate the Model
    print("Model Evaluations")
    score = model.evaluate([x_valid['input1'], x_valid['input2']], y_valid, verbose=1)
    test_loss = round(score[0], 3)
    print ('Test loss:', test_loss)    
    test_outcomes.append(test_loss)

    #Calculate R_Squared
    r_squared = r2_score(y_valid, y_pred)
    print(r_squared)
    r2_outcomes.append(r_squared)
    
    #Store Final Model
    print("Model Stored")
    stored_models.append(model) #list of stored_models
    
mean_test= np.mean(test_outcomes)
r2_means = np.mean(r2_outcomes)

输出示例

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该使用Callbacks

您可以使用回调停止训练

这里有一个示例,说明如何在一定精度阈值时创建一个custum回调以停止训练

#example
acc_threshold =0.95

class myCallback(tf.keras.callbacks.Callback): 
    def on_epoch_end(self, epoch, logs={}): 
        if(logs.get('acc') > acc_threshold):   
        print("\nReached %2.2f%% accuracy, so stopping training!!" %(acc_threshold))   
        self.model.stop_training = True

my_callback = myCallback()
    

model.fit([x_train['input1'], x_train['input2']], y_train, 
                        batch_size = 25, epochs = 60, verbose = 0 #, validation_split = 0.2
                        ,validation_data = ([x_valid['input1'],x_valid['input2']], y_valid),
     callbacks=my_callback )

您还可以使用EarlyStopping来监控指标(如损失没有改善时就停止)

相关问题