简体   繁体   English

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

[英]LSTM timeseries prediction with multiple outputs

I have a dataset with 3 features in a timeseries.我有一个时间序列中包含 3 个特征的数据集。 The dimension of the dataset is 1000 x 3 (1000 timesteps and 3 features).数据集的维度是 1000 x 3(1000 个时间步长和 3 个特征)。 Basically, 1000 rows and 3 columns基本上,1000行3列

The data looks like this: ABC 131 111 100 131 110 120 131 100 100... 131 100 100 The problem is how to train the first 25 steps and predict the next 25 steps in order to get the output of 3 features predictions which is (A, B and C).数据如下所示: ABC 131 111 100 131 110 120 131 100 100... 131 100 100问题是如何训练前 25 步并预测接下来的 25 步以获得 3 个特征预测的输出,即(A、B 和 C)。 I successful train and predict 1-D (1 features(A)) array.我成功地训练和预测了一维(1 个特征(A))数组。 But I have no idea how to predict the 3 features using same the dataset.但我不知道如何使用相同的数据集预测这 3 个特征。

And I got this error:我得到了这个错误:

Error when checking target: expected dense_1 to have shape (None, 3) but got array with shape (21, 1)检查目标时出错:预期 dense_1 具有形状 (None, 3) 但得到形状为 (21, 1) 的数组

The code as below:代码如下:

# -*- 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))

Sample data: v77.txt示例数据: v77.txt

Help Needed.需要帮助。 Thanks谢谢

Your Y shape does not match up with the last layer in your model.您的 Y 形与模型中的最后一层不匹配。 Your Y is in the form of (num_samples, 1) , which means that for every sample it outputs a vector of length 1. Y 的形式为(num_samples, 1) ,这意味着对于每个样本,它都会输出一个长度为 1 的向量。

Your last layer, however, is a Dense(3) layer, which outputs (num_samples, 3) , which means that for every sample it outputs a vector of length 3.然而,您的最后一层是Dense(3)层,它输出(num_samples, 3) ,这意味着对于每个样本,它输出一个长度为 3 的向量。

Since the output of your neural network and your y-data aren't in the same format, the neural network cannot train.由于神经网络的输出和 y 数据的格式不同,因此神经网络无法训练。

You can fix this in two ways:您可以通过两种方式解决此问题:

1.Convert the output of your neural network to the shape of your y data by replacing Dense(3) with Dense(1) : 1.通过将Dense(3)替换为Dense(1) ,将神经网络的输出转换为 y 数据的形状:

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)

2.Convert the shape of your y data to the output of your neural network by modifying your create_dataset() function such that all of the features are added to the y instead of just one: 2.通过修改create_dataset()函数将 y 数据的形状转换为神经网络的输出,这样所有的特征都被添加到 y 而不是只有一个:

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)

Since you stated that you wanted to predict 3 feature most likely you will be using the second option.既然你说你想预测 3 个特征,你很可能会使用第二个选项。 Note that the second option does break the last part of your code to extend the y, but your model trains fine.请注意,第二个选项确实会破坏代码的最后一部分以扩展 y,但您的模型训练良好。

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

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