gru matlab实现,分别用CNN、GRU和LSTM实现时间序列预测(2019-04-06)

卷积神经网络(CNN)、长短期记忆网络(LSTM)以及门控单元网络(GRU)是最常见的一类算法,在kaggle比赛中经常被用来做预测和回归。今天,我们就抛砖引玉,做一个简单的教程,如何用这些网络预测时间序列。因为是做一个简单教程,所以本例子中网络的层数和每层的神经元个数没有调试到最佳。根据不同的数据集,同学们可以自己调网络结构和具体参数。

1.环境搭建

我们运行的环境是下载anaconda,然后在里面安装keras,打开spyder运行程序即可。其中下载anaconda和安装keras的教程在我们另一个博客“用CNN做电能质量扰动分类(2019-03-28)”中写过了,这里就不赘述了。

2.数据集下载

下载时间序列数据集和程序。其中,网盘连接是:

https://pan.baidu.com/s/1TASK3gMZoDFvoE89LzR-5A,密码是“o0sl”。

“nihe.csv”是我自己做的一个时间序列的数据集,一共有1000行4列其中,1-3列可以认为是X,第4列认为是Y。我们现在要做的就是训练3个X和Y之间的关系,然后给定X去预测Y。

3.预测

把下载的nihe.csv文件放到spyder 的默认路径下,我的默认路径是“D:\Matlab2018a\42”,新建一个.py文件,把程序放进去,运行即可。

4.CNN,LSTM,GRU预测时间序列的程序

1)GRU的程序

#1. load dataset

from pandas import read_csv

dataset = read_csv('nihe.csv')

values = dataset.values

#2.tranform data to [0,1]

from sklearn.preprocessing importMinMaxScaler

scaler=MinMaxScaler(feature_range=(0, 1))

XY= scaler.fit_transform(values)

X= XY[:,0:3]

Y = XY[:,3]

#3.split into train and test sets

n_train_hours = 950

trainX = X[:n_train_hours, :]

trainY =Y[:n_train_hours]

testX = X[n_train_hours:, :]

testY =Y[n_train_hours:]

train3DX =trainX.reshape((trainX.shape[0], 1, trainX.shape[1]))

test3DX = testX.reshape((testX.shape[0],1, testX.shape[1]))

#4. Define Network

from keras.models importSequential

from keras.layers import Dense

from keras.layers.recurrentimport GRU

model = Sequential()

model.add(GRU(units=5,input_shape=(train3DX.shape[1],train3DX.shape[2]),return_sequences=True))

model.add(GRU(units=3))

model.add(Dense(units=4,kernel_initializer='normal',activation='relu'))

model.add(Dense(units=1,kernel_initializer='normal',activation='sigmoid'))

#最后输出层1个神经元和输出的个数对应

# 5. compile the network

model.compile(loss='mae',optimizer='adam')

# 6. fit the network

history =model.fit(train3DX,trainY, epochs=100, batch_size=10,validation_data=(test3DX,testY), verbose=2, shuffle=False)

# 7. evaluate the network

from matplotlib import pyplot

pyplot.plot(history.history['loss'],label='train')

pyplot.plot(history.history['val_loss'],label='test')

pyplot.legend()

pyplot.show()

#8. make a prediction and invertscaling for forecast

from pandas import concat

forecasttestY0 =model.predict(test3DX)

inv_yhat=np.concatenate((testX,forecasttestY0), axis=1)

inv_y =scaler.inverse_transform(inv_yhat)

forecasttestY = inv_y[:,3]

# calculate RMSE

from math import sqrt

from sklearn.metrics importmean_squared_error

actualtestY=values[n_train_hours:,3]

rmse = sqrt(mean_squared_error(forecasttestY,actualtestY))

print('Test RMSE: %.3f' % rmse)

#plot the testY and actualtestY

pyplot.plot(actualtestY,label='train')

pyplot.plot(forecasttestY,label='test')

pyplot.legend()

pyplot.show()

2)LSTM的程序

#1. load dataset

from pandas import read_csv

dataset = read_csv('nihe.csv')

values = dataset.values

#2.tranform data to [0,1]

from sklearn.preprocessing importMinMaxScaler

scaler=MinMaxScaler(feature_range=(0, 1))

XY= scaler.fit_transform(values)

X= XY[:,0:3]

Y = XY[:,3]

#3.split into train and test sets

n_train_hours = 950

trainX = X[:n_train_hours, :]

trainY =Y[:n_train_hours]

testX = X[n_train_hours:, :]

testY =Y[n_train_hours:]

train3DX =trainX.reshape((trainX.shape[0], 1, trainX.shape[1]))

test3DX = testX.reshape((testX.shape[0],1, testX.shape[1]))

#4. Define Network

from keras.models importSequential

from keras.layers import Dense

from keras.layers.recurrentimport LSTM

model = Sequential()

model.add(LSTM(units=5,input_shape=(train3DX.shape[1],train3DX.shape[2]),return_sequences=True))

model.add(LSTM(units=3))

model.add(Dense(units=4,kernel_initializer='normal',activation='relu'))

model.add(Dense(units=1,kernel_initializer='normal',activation='sigmoid'))

#最后输出层1个神经元和输出的个数对应

# 5. compile the network

model.compile(loss='mae',optimizer='adam')

# 6. fit the network

history =model.fit(train3DX,trainY, epochs=100, batch_size=10,validation_data=(test3DX,testY), verbose=2, shuffle=False)

# 7. evaluate the network

from matplotlib import pyplot

pyplot.plot(history.history['loss'],label='train')

pyplot.plot(history.history['val_loss'],label='test')

pyplot.legend()

pyplot.show()

#8. make a prediction and invertscaling for forecast

from pandas import concat

import numpy as np

forecasttestY0 =model.predict(test3DX)

#forecasttestY= np.expand_dims(a,axis=1)

inv_yhat=np.concatenate((testX,forecasttestY0), axis=1)

inv_y =scaler.inverse_transform(inv_yhat)

forecasttestY = inv_y[:,3]

# calculate RMSE

from math import sqrt

from sklearn.metrics importmean_squared_error

actualtestY=values[n_train_hours:,3]

rmse =sqrt(mean_squared_error(forecasttestY, actualtestY))

print('Test RMSE: %.3f' % rmse)

#plot the testY and actualtestY

pyplot.plot(actualtestY,label='train')

pyplot.plot(forecasttestY,label='test')

pyplot.legend()

pyplot.show()

3)CNN和LSTM的合并

#1. load dataset

from pandas import read_csv

dataset = read_csv('nihe.csv')

values = dataset.values

#2.tranform data to [0,1]  3个属性,第4个是待预测量

from sklearn.preprocessing importMinMaxScaler

scaler=MinMaxScaler(feature_range=(0, 1))

XY= scaler.fit_transform(values)

X= XY[:,0:3]

Y = XY[:,3]

#3.split into train and test sets

950个训练集,剩下的都是验证集

n_train_hours = 950

trainX = X[:n_train_hours, :]

trainY =Y[:n_train_hours]

testX = X[n_train_hours:, :]

testY =Y[n_train_hours:]

#LSTM的输入格式要3维,因此先做变换

train3DX =trainX.reshape((trainX.shape[0], 1, trainX.shape[1]))

test3DX =testX.reshape((testX.shape[0], 1, testX.shape[1]))

#4. Define Network

from keras.models importSequential

from keras.layers import Dense

from keras.layers.recurrentimport LSTM

from keras.layers.convolutionalimport Conv1D

from keras.layers.convolutionalimport MaxPooling1D

from keras.layers import Flatten

model = Sequential()

model.add(Conv1D(filters=10,kernel_size=1, padding='same', strides=1, activation='relu',input_shape=(1,3)))

model.add(MaxPooling1D(pool_size=1))

model.add(LSTM(units=3,return_sequences=True))

model.add(Flatten())

#可以把LSTM和Flatten删除,仅保留LSTM

#model.add(LSTM(units=3))

model.add(Dense(5,activation='relu'))

#在lstm层之后可以添加隐含层,也可以不加,直接加输出层

#model.add(Dense(units=4,kernel_initializer='normal',activation='relu'))

model.add(Dense(units=1,kernel_initializer='normal',activation='sigmoid'))

#最后输出层1个神经元和输出的个数对应

# 5. compile the network

model.compile(loss='mae',optimizer='adam')

# 6. fit the network

history =model.fit(train3DX,trainY, epochs=100, batch_size=10,validation_data=(test3DX,testY), verbose=2, shuffle=False)

# 7. evaluate the network

from matplotlib import pyplot

pyplot.plot(history.history['loss'],label='train')

pyplot.plot(history.history['val_loss'],label='test')

pyplot.legend()

pyplot.show()

#8. make a prediction and invertscaling for forecast

from pandas import concat

import numpy as np

forecasttestY0 =model.predict(test3DX)

inv_yhat=np.concatenate((testX,forecasttestY0), axis=1)

inv_y =scaler.inverse_transform(inv_yhat)

forecasttestY = inv_y[:,3]

# calculate RMSE

from math import sqrt

from sklearn.metrics importmean_squared_error

actualtestY=values[n_train_hours:,3]

rmse =sqrt(mean_squared_error(forecasttestY, actualtestY))

print('Test RMSE: %.3f' % rmse)

#plot the testY and actualtestY

pyplot.plot(actualtestY,label='train')

pyplot.plot(forecasttestY,label='test')

pyplot.legend()

pyplot.show()

你可能感兴趣的:(gru,matlab实现)