具有多个输出的LSTM时间序列预测

时间:2018-04-19 21:29:47

标签: python keras time-series lstm

我有一个时间序列中有3个功能的数据集。数据集的维度为1000 x 3(1000个步骤和3个要素)。基本上,1000行和3列

数据如下所示: A B C 131 111 100 131 110 120 131 100 100 ... 131 100 100 问题是如何训练前25个步骤并预测接下来的25个步骤,以获得3个特征预测的输出,即(A,B和C)。我成功训练并预测1-D(1个特征(A))阵列。但我不知道如何使用相同的数据集预测3个特征。

我收到了这个错误:

  

检查目标时出错:期望dense_1有形状(无,3)但是有形状的数组(21,1)

代码如下:

# -*- coding: utf-8 -*-
import numpy as np
import numpy
import matplotlib.pyplot as plt
import pandas
import math

from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error


# convert an array of values into a dataset matrix

def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset) - look_back - 1):
        a = dataset[i:(i + look_back):]
        dataX.append(a)
        dataY.append(dataset[i + look_back, :])
    return numpy.array(dataX), numpy.array(dataY)



# fix random seed for reproducibility
numpy.random.seed(7)


# load the dataset
dataframe = pandas.read_csv('v77.csv', engine='python',skiprows=0) 
dataset = dataframe.values
print dataset
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)

# split into train and test sets
train_size = 10
test_size = 10
train, test = dataset[0:train_size, :], dataset[train_size:train_size+test_size, :]
print (train_size,test_size)

# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)  
testX, testY = create_dataset(test, look_back)
print trainX

# reshape input to be  [samples, time steps, features]
#trainX = numpy.reshape(trainX, (trainX.shape[0], look_back, 3))
#testX = numpy.reshape(testX, (testX.shape[0],look_back, 3))

# create and fit the LSTM network

model = Sequential()
model.add(LSTM(32, input_shape=(3,3)))
model.add(Dense(3))
model.compile(loss='mean_squared_error', optimizer='adam')
history= model.fit(trainX, trainY,validation_split=0.33, nb_epoch=10, batch_size=16)

# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# print testPredict
# print np.shape(testPredict)
# Get something which has as many features as dataset
trainPredict_extended = numpy.zeros((len(trainPredict),3))
print trainPredict_extended
print np.shape(trainPredict_extended[:,2])
print np.shape(trainPredict[:,0])
# Put the predictions there
trainPredict_extended[:,2] = trainPredict[:,0]
# Inverse transform it and select the 3rd coumn.
trainPredict = scaler.inverse_transform(trainPredict_extended) [:,2]  
# print(trainPredict)
# Get something which has as many features as dataset
testPredict_extended = numpy.zeros((len(testPredict),3))
# Put the predictions there
testPredict_extended[:,2] = testPredict[:,0]
# Inverse transform it and select the 3rd column.
testPredict = scaler.inverse_transform(testPredict_extended)[:,2]   
# print testPredict_extended

trainY_extended = numpy.zeros((len(trainY),3))
trainY_extended[:,2]=trainY
trainY=scaler.inverse_transform(trainY_extended)[:,2]


testY_extended = numpy.zeros((len(testY),3))
testY_extended[:,2]=testY
testY=scaler.inverse_transform(testY_extended)[:,2]
# print 

# print testY
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY, trainPredict))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY, testPredict))
print('Test Score: %.2f RMSE' % (testScore))

示例数据: v77.txt

需要帮助。感谢

1 个答案:

答案 0 :(得分:1)

您的Y形状与模型中的最后一层不匹配。你的Y是initializers.glorot_uniform()的形式,这意味着对于每个样本,它输出一个长度为1的向量。

然而,您的最后一个图层是(num_samples, 1)图层,输出Dense(3),这意味着每个图像都会输出一个长度为3的矢量。

由于您的神经网络输出和您的y数据格式不同,神经网络无法训练。

您可以通过两种方式解决此问题:

1.将(num_samples, 3)替换为Dense(3),将神经网络的输出转换为y数据的形状:

Dense(1)

2.通过修改model = Sequential() model.add(LSTM(32, input_shape=(3,3))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam')history= model.fit(trainX, trainY,validation_split=0.33, nb_epoch=10, batch_size=16) 函数将y数据的形状转换为神经网络的输出,以便将所有要素添加到y而不是仅添加一个:

create_dataset()

由于您声明您想要预测3个功能,因此您很可能会使用第二个选项。请注意,第二个选项确实会破坏代码的最后一部分以扩展y,但您的模型可以正常运行。