如何在 for 循环中为不同的数据集调用 model.fit()?

How can i call model.fit() for different data set in for loop?

我在 python 开发卷积神经网络。我创建了 Sequential 模型,我想让这个模型适合不同的数据集。所以我在 for 循环中调用拟合模型。但是调用一个数据集与调用 for 循环会给出不同的结果。如何重置模型参数? 我的代码如下:

for tr_ind in range(len(train_set_month_list)):
    test_dataset_month_info = test_set_month_list[tr_ind];
    train_dataset_month_info = train_set_month_list[tr_ind];

    model = Sequential()
    history = fit_model_cnn(model, train_x_df, train_x_df_reshaped, train_y_df, validation_data_x_df_reshaped,
                            validation_data_y_df, timesteps, epoch_size, batch_size);


def fit_model_cnn(model, train_x_df, train_x_df_reshaped, train_y_df, validation_data_x_df_reshaped,
                  validation_data_y_df,
                  timesteps, epoch_size, batch_size):
    model.add(
        Conv1D(filters=filter_size, kernel_size=kernel_size, activation=activation_func, padding='same',
               input_shape=(timesteps, train_x_df.shape[1] / timesteps)))
    model.add(MaxPooling1D(pool_size=1))
    model.add(Flatten())
    model.add(Dense(node_count, activation=activation_func, kernel_initializer='he_uniform'))
    model.add(Dense(1))
    model.compile(optimizer=optimizer_type, loss='mse')

    # fit model
    history = model.fit(train_x_df_reshaped, train_y_df.values,
                        validation_data=(validation_data_x_df_reshaped, validation_data_y_df), batch_size=batch_size,
                        epochs=epoch_size)
    return history;

从非常简单的角度来看,数据是按顺序输入的,这表明至少数据顺序可能会对输出产生影响。如果顺序无关紧要,随机化肯定不会有什么坏处。如果顺序确实重要,随机化将有助于消除这些随机效应,使它们不会成为系统性偏差。简而言之,随机化既便宜又无害,而且通常会最大限度地减少数据排序的影响。

换句话说,当您为神经网络提供不同的数据集时,您的模型可能会偏向于它所训练的最新数据集。

你应该始终确保你是从你拥有的所有数据集中随机抽样的。

我认为你应该在循环中清除你以前的模型,这样你就可以使用这个函数 keras.backend.clear_session()。来自 https://keras.io/backend/: 这将解决您的问题。