MLPRegressor的损失历史

时间:2017-08-03 15:26:34

标签: python scikit-learn

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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

最后一行是重要的一行!

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放在一起。