简体   繁体   English

如何在Scikit-Learn中使用均方根误差优化神经网络?

[英]How to use Root Mean Square Error for optimizing Neural Network in Scikit-Learn?

I am new to neural network so please pardon any silly question. 我是神经网络的新手,所以请原谅任何愚蠢的问题。 I am working with a weather dataset. 我正在使用天气数据集。 Here I am using Dewpoint, Humidity, WindDirection, WindSpeed to predict temperature. 在这里,我使用露点,湿度,WindDirection,WindSpeed来预测温度。 I have read several papers on this so I felt intrigued to do a research on my own.At first I am training the model with 4000 observations and then trying to predict next 50 temperature points. 我已经阅读了几篇论文,因此对自己进行研究很感兴趣。首先,我对4000个观测值进行训练,然后尝试预测下一个50个温度点。

Here goes my entire code. 这是我的整个代码。

from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error
from sklearn import preprocessing
import numpy as np
import pandas as pd

df = pd.read_csv('WeatherData.csv', sep=',', index_col=0)

X = np.array(df[['DewPoint', 'Humidity', 'WindDirection', 'WindSpeed']])
y = np.array(df[['Temperature']])

# nan_array = pd.isnull(df).any(1).nonzero()[0]

neural_net = MLPRegressor(
    activation='logistic',
    learning_rate_init=0.001,
    solver='sgd',
    learning_rate='invscaling',
    hidden_layer_sizes=(200,),
    verbose=True,
    max_iter=2000,
    tol=1e-6
)
# Scaling the data
max_min_scaler = preprocessing.MinMaxScaler()
X_scaled = max_min_scaler.fit_transform(X)
y_scaled = max_min_scaler.fit_transform(y)


neural_net.fit(X_scaled[0:4001], y_scaled[0:4001].ravel())

predicted = neural_net.predict(X_scaled[5001:5051])

# Scale back to actual scale
max_min_scaler = preprocessing.MinMaxScaler(feature_range=(y[5001:5051].min(), y[5001:5051].max()))
predicted_scaled = max_min_scaler.fit_transform(predicted.reshape(-1, 1))

print("Root Mean Square Error ", mean_squared_error(y[5001:5051], predicted_scaled))

First confusing thing to me is that the same program is giving different RMS error at different run. 首先让我感到困惑的是,同一程序在不同的运行时间给出不同的RMS错误。 Why? 为什么? I am not getting it. 我不明白。

Run 1: 运行1:

Iteration 1, loss = 0.01046558
Iteration 2, loss = 0.00888995
Iteration 3, loss = 0.01226633
Iteration 4, loss = 0.01148097
Iteration 5, loss = 0.01047128
Training loss did not improve more than tol=0.000001 for two consecutive epochs. Stopping.
Root Mean Square Error  22.8201171703

Run 2(Significant Improvement): 运行2(重大改进):

Iteration 1, loss = 0.03108813
Iteration 2, loss = 0.00776097
Iteration 3, loss = 0.01084675
Iteration 4, loss = 0.01023382
Iteration 5, loss = 0.00937209
Training loss did not improve more than tol=0.000001 for two consecutive epochs. Stopping.
Root Mean Square Error  2.29407183124

In the documentation of MLPRegressor I could not find a way to directly hit the RMS error and keep the network running until I reach the desired RMS error. MLPRegressor的文档中,我找不到直接达到RMS错误并保持网络运行的方法,直到达到所需的RMS错误为止。 What am I missing here? 我在这里想念什么?

Please help! 请帮忙!

First confusing thing to me is that the same program is giving different RMS error at different run. 首先让我感到困惑的是,同一程序在不同的运行时间给出不同的RMS错误。 Why? 为什么? I am not getting it. 我不明白。

Neural networks are prone to local optima . 神经网络倾向于局部最优 There is never a guarantee you will learn anything decent, nor (as a consequence) that multiple runs lead to the same solution. 永远不能保证您会学到任何体面的东西,也不能保证多次运行会导致相同的解决方案。 Learning process is heavily random, depends on the initialization, sampling order etc. thus this kind of behaviour is expected . 学习过程是高度随机的,取决于初始化,采样顺序等,因此这种行为是可以预期的

In the documentation of MLPRegressor I could not find a way to directly hit the RMS error and keep the network running until I reach the desired RMS error. 在MLPRegressor的文档中,我找不到直接达到RMS错误并保持网络运行的方法,直到达到所需的RMS错误为止。

Neural networks in sklearn are extremely basic, and they do not provide this kind of flexibility. sklearn中的神经网络非常基础,并且不能提供这种灵活性。 If you need to work with more complex settings you simply need more NN oriented library, like Keras, TF etc. scikit-learn community struggled a lot to even make this NN implementation "in", and it does not seem like they are going to add much more flexibility in near future. 如果您需要使用更复杂的设置,则只需要更多面向NN的库,例如Keras,TF等。scikit-learn社区在使该NN实现“投入”方面付出了很多努力,而且看来他们不会在不久的将来增加更多的灵活性。

As a minor thing - use of "minmaxscaler" seem slightly odd. 作为较小的事情-使用“ minmaxscaler”似乎有些奇怪。 You should not "fit_transform" each time, you should fit only once, and later on - use transform (or inverse_transform). 您不应该每次都“ fit_transform”,而应该只适合一次,然后再使用-使用transform(或inverse_transform)。 In particular, it should be 特别是应该

y_max_min_scaler = preprocessing.MinMaxScaler()
y_scaled = y_max_min_scaler.fit_transform(y)

...

predicted_scaled = y_max_min_scaler.inverse_transform(predicted.reshape(-1, 1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM