愉快的学习就从翻译开始吧_8-Time Series Forecasting with the Long Short-Term Memory Network in Python

Complete LSTM Example/完整的LSTM示例

In this section, we will fit an LSTM to the Shampoo Sales dataset and evaluate the model.

在本节中,我们将为洗发水销售数据集拟合LSTM并评估模型。

This will involve drawing together all of the elements from the prior sections. There are a lot of them, so let’s review:

这将涉及将前面各节的所有内容汇总在一起。 有很多,所以让我们来回顾一下:

  1. Load the dataset from CSV file.
    从CSV文件加载数据集。
  2. Transform the dataset to make it suitable for the LSTM model, including:
    转换数据集以使其适用于LSTM模型,其中包括:
    1. Transforming the data to a supervised learning problem.
      将数据转化为监督学习问题。
    2. Transforming the data to be stationary.
      将数据转换为静止(一直认为这个转换时多此一举)。
    3. Transforming the data so that it has the scale -1 to 1(将数据转换到-1到1范围内).
  3. Fitting a stateful LSTM network model to the training data.
    将有状态的LSTM网络模型拟合到训练数据中。
  4. Evaluating the static LSTM model on the test data.
    在测试数据上评估静态LSTM模型。
  5. Report the performance of the forecasts.
    报告预测的性能。

Some things to note about the example:

关于这个例子,要注意的事项

  • The scaling and inverse scaling behaviors have been moved to the functions scale() and invert_scale() for brevity.
    为了简洁起见,缩放和反比例缩放行为已经被移到了scale()和invert_scale()函数中。
  • The test data is scaled using the fit of the scaler on the training data, as is required to ensure the min/max values of the test data do not influence the model.
    用在训练数据上拟合来缩放器来缩放测试数据,这是为了确保测试数据的最小/最大值不影响模型。
  • The order of data transforms was adjusted for convenience to first make the data stationary, then a supervised learning problem, then scaled.
    为了方便起见,调整了数据转换的顺序,首先使数据静止,然后监督学习问题,然后缩放。
  • Differencing was performed on the entire dataset prior to splitting into train and test sets for convenience. We could just as easily collect observations during the walk-forward validation and difference them as we go. I decided against it for readability.
    为了方便起见,在对整个数据集进行差分处理,先于将数据分成训练和测试集。 我们可以在向前验证过程中轻松收集观察结果,并随着时间推移对它们进行区分。 为了可读性,我决定反对这样做。
    (一步一步做下来,本来觉得大部分都理解了,这特么一总结,又有些地方被绕晕了,特么的!)
The complete example is listed below.
下面列出完整的示例
from pandas import DataFrame
from pandas import Series
from pandas import concat
from pandas import read_csv
from pandas import datetime
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from math import sqrt
from matplotlib import pyplot
import numpy
 
# date-time parsing function for loading the dataset
def parser(x):
	return datetime.strptime('190'+x, '%Y-%m')
 
# frame a sequence as a supervised learning problem
def timeseries_to_supervised(data, lag=1):
	df = DataFrame(data)
	columns = [df.shift(i) for i in range(1, lag+1)]
	columns.append(df)
	df = concat(columns, axis=1)
	df.fillna(0, inplace=True)
	return df
 
# create a differenced series
def difference(dataset, interval=1):
	diff = list()
	for i in range(interval, len(dataset)):
		value = dataset[i] - dataset[i - interval]
		diff.append(value)
	return Series(diff)
 
# invert differenced value
def inverse_difference(history, yhat, interval=1):
	return yhat + history[-interval]
 
# scale train and test data to [-1, 1]
def scale(train, test):
	# fit scaler
	scaler = MinMaxScaler(feature_range=(-1, 1))
	scaler = scaler.fit(train)
	# transform train
	train = train.reshape(train.shape[0], train.shape[1])
	train_scaled = scaler.transform(train)
	# transform test
	test = test.reshape(test.shape[0], test.shape[1])
	test_scaled = scaler.transform(test)
	return scaler, train_scaled, test_scaled
 
# inverse scaling for a forecasted value
def invert_scale(scaler, X, value):
	new_row = [x for x in X] + [value]
	array = numpy.array(new_row)
	array = array.reshape(1, len(array))
	inverted = scaler.inverse_transform(array)
	return inverted[0, -1]
 
# fit an LSTM network to training data
def fit_lstm(train, batch_size, nb_epoch, neurons):
	X, y = train[:, 0:-1], train[:, -1]
	X = X.reshape(X.shape[0], 1, X.shape[1])
	model = Sequential()
	model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
	model.add(Dense(1))
	model.compile(loss='mean_squared_error', optimizer='adam')
	for i in range(nb_epoch):
		model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
		model.reset_states()
	return model
 
# make a one-step forecast
def forecast_lstm(model, batch_size, X):
	X = X.reshape(1, 1, len(X))
	yhat = model.predict(X, batch_size=batch_size)
	return yhat[0,0]
 
# load dataset
series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
 
# transform data to be stationary
raw_values = series.values
diff_values = difference(raw_values, 1)
 
# transform data to be supervised learning
supervised = timeseries_to_supervised(diff_values, 1)
supervised_values = supervised.values
 
# split data into train and test-sets
train, test = supervised_values[0:-12], supervised_values[-12:]
 
# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)
 
# fit the model
lstm_model = fit_lstm(train_scaled, 1, 3000, 4)
# forecast the entire training dataset to build up state for forecasting
train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
lstm_model.predict(train_reshaped, batch_size=1)
 
# walk-forward validation on the test data
predictions = list()
for i in range(len(test_scaled)):
	# make one-step forecast
	X, y = test_scaled[i, 0:-1], test_scaled[i, -1]
	yhat = forecast_lstm(lstm_model, 1, X)
	# invert scaling
	yhat = invert_scale(scaler, X, yhat)
	# invert differencing
	yhat = inverse_difference(raw_values, yhat, len(test_scaled)+1-i)
	# store forecast
	predictions.append(yhat)
	expected = raw_values[len(train) + i + 1]
	print('Month=%d, Predicted=%f, Expected=%f' % (i+1, yhat, expected))
 
# report performance
rmse = sqrt(mean_squared_error(raw_values[-12:], predictions))
print('Test RMSE: %.3f' % rmse)
# line plot of observed vs predicted
pyplot.plot(raw_values[-12:])
pyplot.plot(predictions)
pyplot.show()

Running the example prints the expected and predicted values for each of the 12 months in the test dataset.

运行该示例将打印测试数据集中12个月中每个月的预期值和预测值。

The example also prints the RMSE of all forecasts. The model shows an RMSE of 71.721 monthly shampoo sales, which is better than the persistence model that achieved an RMSE of 136.761 shampoo sales.

该示例还打印所有预测的RMSE。 该模型显示每月洗发水销售额的均方根误差为71.721,优于持续性模型,该类型的洗发水销售额的均方根误差为136.761。

Random numbers are used in seeding the LSTM, and as a result, you may have a different result from a single run of the model. We cover this further in the next section.

随机数用于初始化LSTM中,因此,您可能会从模型的单次运行获得不同的结果。 我们在下一节进一步介绍这一点

特么的整个例子哪里用到随机数了?还是说LSTM初始化权重的时候默认使用随机数,能不能说的清楚点呀,WF,百度一圈,

keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', 
kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)

glorot_uniform

glorot_uniform(seed=None)

Glorot均匀分布初始化方法,又成Xavier均匀初始化,参数从[-limit, limit]的均匀分布产生,其中limit为sqrt(6 / (fan_in + fan_out))。fan_in为权值张量的输入单元数,fan_out是权重张量的输出单元数。

  • seed:随机数种子

可以看到,keras默认的初始化权重方法是‘glorot_uniform’,且seed=None,没有使用随机数种子,也就是说初始化权重的确是随机的,更加详细的介绍看如下博文:

https://blog.csdn.net/VictoriaW/article/details/73000632

)。

Month=1, Predicted=351.582196, Expected=339.700000
Month=2, Predicted=432.169667, Expected=440.400000
Month=3, Predicted=378.064505, Expected=315.900000
Month=4, Predicted=441.370077, Expected=439.300000
Month=5, Predicted=446.872627, Expected=401.300000
Month=6, Predicted=514.021244, Expected=437.400000
Month=7, Predicted=525.608903, Expected=575.500000
Month=8, Predicted=473.072365, Expected=407.600000
Month=9, Predicted=523.126979, Expected=682.000000
Month=10, Predicted=592.274106, Expected=475.300000
Month=11, Predicted=589.299863, Expected=581.300000
Month=12, Predicted=584.149152, Expected=646.900000
Test RMSE: 71.721

A line plot of the test data (blue) vs the predicted values (orange) is also created, providing context for the model skill.

测试数据(蓝色)与预测值(橙色)的线图也被创建出来,可以看到两根线的关联(为模型的性能提供联系,直译绕死了)。

Line Plot of LSTM Forecast vs Expected Values

Line Plot of LSTM Forecast vs Expected Values

As an afternote, you can do a quick experiment to build your trust in the test harness and all of the transforms and inverse transforms.

作为一个备注,您可以做一个快速实验,建立您对测试工具以及所有变换和逆变换的信任。

Comment out the line that fits the LSTM model in walk-forward validation:

注释掉前行验证中拟合LSTM的代码:

yhat = forecast_lstm(lstm_model, 1, X)

and replace it with the following:

yhat = y

This should produce a model with perfect skill (e.g. a model that predicts the expected outcome as the model output).

这应该产生性能更好的模型(例如,模型输出了完全符合预期的输出,这特么不是废话,都直接用期望值代替预测值了)

The results should look as follows, showing that if the LSTM model could predict the series perfectly, the inverse transforms and error calculation would show it correctly.

结果应该如下所示,表明如果LSTM模型能够完美地预测该系列,则逆变换和误差计算应该正确显示它。

Month=1, Predicted=339.700000, Expected=339.700000
Month=2, Predicted=440.400000, Expected=440.400000
Month=3, Predicted=315.900000, Expected=315.900000
Month=4, Predicted=439.300000, Expected=439.300000
Month=5, Predicted=401.300000, Expected=401.300000
Month=6, Predicted=437.400000, Expected=437.400000
Month=7, Predicted=575.500000, Expected=575.500000
Month=8, Predicted=407.600000, Expected=407.600000
Month=9, Predicted=682.000000, Expected=682.000000
Month=10, Predicted=475.300000, Expected=475.300000
Month=11, Predicted=581.300000, Expected=581.300000
Month=12, Predicted=646.900000, Expected=646.900000
Test RMSE: 0.000


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值