【问题标题】:Loss history for MLPRegressorMLPRegressor 的损失历史
【发布时间】:2017-08-03 15:26:33
【问题描述】:

我正在使用 MLPRegressor 来解决一个问题,并想绘制损失函数,即每个训练时期损失减少了多少。但是,可用于 MLPRegressor 的属性 model.loss_ 仅允许访问最后的损失值。有没有可能访问整个损失历史?

【问题讨论】:

  • 我也对这个感兴趣,你搞清楚了吗?
  • 不,很遗憾没有。我只是手动设置verbose = True并复制输出..
  • 我明白了,我最终也做了同样的事情

标签: python scikit-learn


【解决方案1】:

实际上非常简单:model.loss_curve_ 为您提供每个时期的损失值。然后,您可以通过将时期放在 x 轴上并将上述值放在 y 轴上来轻松绘制学习曲线

【讨论】:

  • 请添加一些关于如何使用上面的代码 sn-p 的详细信息,以改进您的答案。
  • 请注意,这仅适用于默认的 'adam' 求解器。不会使用 'sgd''lbfgs' 求解器生成 model.loss_curve_。我花了一段时间才弄明白!
【解决方案2】:

正如@francesco 建议的那样,使用model.loss_curve_ 非常简单,并将这些值绘制为y 轴,将索引或纪元数绘制为x 轴。

绘制使用 sci-kit learn 训练的模型损失的示例代码:

最后一行很重要!

import pandas as pd
from sklearn.neural_network import MLPRegressor

# Create Neural Net MLP regressor 
# Explore settings logarithmically (0.1, 0.01, 0.001, 0.00001)
model = MLPRegressor(
    # try some layer & node sizes
    hidden_layer_sizes=(5,17), 
    # find a learning rate?
    learning_rate_init=.001,
    # activation functions (relu, tanh, identity)
    activation='relu',
    max_iter=2000
);

# Train it (where X_train is your feature matrix & Y_train is a vector with desired target values for each record)
nn_regr.fit(X_train,Y_train)

# Plot the 'loss_curve_' protery on model to see how well we are learning over the iterations
# Use Pandas built in plot method on DataFrame to creat plot in one line of code
pd.DataFrame(model.loss_curve_).plot()

Model Loss Curve Plot using Pandas/Matplotlib

要在上下文中查看此代码,请查看 Jupyter 笔记本 https://github.com/atomantic/ml_class/blob/master/06%20-%20Regression%20Examples.ipynb 从介绍到 ML 研讨会 @atomantic@jasonlewismedia 放在一起。

【讨论】:

  • 请注意,这仅适用于默认的 'adam' 求解器。不会使用 'sgd''lbfgs' 求解器生成 model.loss_curve_。我花了一段时间才弄明白!
猜你喜欢
  • 2022-12-21
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
  • 2017-11-24
  • 2019-01-09
  • 1970-01-01
  • 2012-02-29
相关资源
最近更新 更多