【问题标题】:Score remains same during hyper parameter tuning在超参数调整期间分数保持不变
【发布时间】:2020-06-25 19:38:45
【问题描述】:

我的模型-

    model = Sequential()
    model.add(Dense(128, activation='relu', input_dim=n_input_1))
    model.add(Dense(64, activation='relu'))
    #model.add(Dense(32, activation='relu'))
    #model.add(Dense(16, activation='relu'))
    model.add(Dense(1))
    model.compile(optimizer='adam', loss='mse',metrics=['mse'])

现在我正在做超参数调整,但它对每个可能的结果都是一样的-

Best: -61101.514139 using {'batch_size': 10, 'epochs': 2}
-61101.514139 (25108.783936) with: {'batch_size': 10, 'epochs': 2}
-61101.514139 (25108.783936) with: {'batch_size': 10, 'epochs': 4}
-61101.514139 (25108.783936) with: {'batch_size': 10, 'epochs': 5}
-61101.514139 (25108.783936) with: {'batch_size': 10, 'epochs': 10}
-61101.514139 (25108.783936) with: {'batch_size': 10, 'epochs': 15}
-61101.514139 (25108.783936) with: {'batch_size': 20, 'epochs': 2}
-61101.514139 (25108.783936) with: {'batch_size': 20, 'epochs': 4}
-61101.514139 (25108.783936) with: {'batch_size': 20, 'epochs': 5}
-61101.514139 (25108.783936) with: {'batch_size': 20, 'epochs': 10}
-61101.514139 (25108.783936) with: {'batch_size': 20, 'epochs': 15}
-61101.514139 (25108.783936) with: {'batch_size': 30, 'epochs': 2}
-61101.514139 (25108.783936) with: {'batch_size': 30, 'epochs': 4}
-61101.514139 (25108.783936) with: {'batch_size': 30, 'epochs': 5}
-61101.514139 (25108.783936) with: {'batch_size': 30, 'epochs': 10}
-61101.514139 (25108.783936) with: {'batch_size': 30, 'epochs': 15}

这是我第一次做超参数,这让我很难过。如果需要,我可以提供更多详细信息。这种可能行为的原因是什么?

我正在使用 MLP 进行时间序列预测。我在 gridsearchCV 中使用了 'neg_mean_absolute_error 作为评分函数。

编辑-这是我正在运行的-

from sklearn.model_selection import GridSearchCV
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)

# define the grid search parameters
model = KerasClassifier(build_fn=create_model, verbose=1)
batch_size = [10,20,2000]
epochs = [2,4,5,10, 25]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3,scoring='neg_mean_squared_error')
grid_result = grid.fit(scaled_train,scaled_train_y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

【问题讨论】:

    标签: python machine-learning keras scikit-learn


    【解决方案1】:

    您似乎没有为这些变量提供足够的范围来查看差异。对于神经网络,有很多超参数需要调整,所以我将简要解释一些变量以及它可以做什么。

    1) batch size,假设我们有 100 万个示例供我们的机器学习,并且我们希望我们的模型通过不丢弃一些示例来查看所有数据集,因此我们增加了 batch size 所以我们在看到样本的批量大小后更新一次我们的体重。因此,增加这意味着我们失去了数据效率(见多次样本更新一次),但获得了样本的多样性。

    2) epoch,表示当我们的模型使用所有给定数据进行训练时,我们计为 1 个 epoch。因此,如果我们增加批量大小,我们将为每个 epoch 更新几次权重。

    3) 学习率,这个数字表示我们每次迭代更新模型的权重多少,太高,你的损失会反弹或飙升,太低你的损失会降低超慢。

    因此,您正在做的是改变 epoch 和 batch size,您可能会看到损失没有减少,因为通常人们所做的是将模型训练数百或数千个 epoch,这样您就可以看到损失的差异。我建议您使用学习率并修复所有其他参数并运行 100 个 epoch,然后您会看到差异。此外,您不必改变 epoch,因为您可以运行一次并在每个 epoch 中收集损失并将其与其他实验进行比较。

    here 是链接,如果您想了解有关参数可以做什么的更多信息。

    【讨论】:

    • 我的意思是,在给定相同学习率的情况下,您的模型有可能无法在该时期拟合该数据。代码本身没有错,但您只给出 2 到 25 个 epochs(在某些数据中,您需要数百万个 epochs )。如果要调试代码,请创建一个玩具实例,例如scaled_train = np.arange(2000)scaled_train_y= np.arange(2000,4000),然后您可以看到损失的差异
    • 第二,不需要运行不同的epoch,你可以为每个配置运行100个epoch,看看它在训练时会发生什么。在您的情况下,您可以运行 25 个 epoch 并监控每一步的损失。
    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2021-09-21
    • 2020-10-17
    • 2020-07-24
    相关资源
    最近更新 更多