[英]How to work with multiple inputs for LSTM in Keras?
I'm trying to predict the water usage of a population. 我试图预测一个人口的用水量。
I have 1 main input: 我有1个主要输入:
and 2 secondary inputs: 和2个次要输入:
In theory they have a relation with the water supply. 从理论上讲,它们与供水有关。
It must be said that each rainfall and temperature data correspond with the water volume. 必须说每个降雨和温度数据都与水量相对应。 So this is a time series problem.
所以这是一个时间序列问题。
The problem is that I don't know how to use 3 inputs from just one .csv file, with 3 columns, each one for each input, as the code below is made. 问题是我不知道如何从一个.csv文件中使用3个输入,每个输入有3列,每个输入,如下面的代码所示。 When I have just one input (egwater volume) the network works more or less good with this code, but not when I have more than one.
当我只有一个输入(例如水量)时,网络或多或少地使用此代码,但不是当我有多个输入时。 (So if you run this code with the csv file below, it will show a dimension error).
(因此,如果您使用下面的csv文件运行此代码,则会显示维度错误)。
Reading some answers from: 阅读一些答案:
it seems to be that many people have the same problem. 似乎很多人都有同样的问题。
The code: 编码:
EDIT: Code has been updated 编辑:代码已更新
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), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 2])
return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = pandas.read_csv('datos.csv', engine='python')
dataset = dataframe.values
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
# 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)
# 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(4, input_dim=look_back))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
history= model.fit(trainX, trainY,validation_split=0.33, nb_epoch=200, batch_size=32)
# Plot training
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('pérdida')
plt.xlabel('época')
plt.legend(['entrenamiento', 'validación'], loc='upper right')
plt.show()
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# Get something which has as many features as dataset
trainPredict_extended = numpy.zeros((len(trainPredict),3))
# Put the predictions there
trainPredict_extended[:,2] = trainPredict[:,0]
# Inverse transform it and select the 3rd column.
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]
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]
# 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))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, 2] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, 2] = testPredict
#plot
serie,=plt.plot(scaler.inverse_transform(dataset)[:,2])
prediccion_entrenamiento,=plt.plot(trainPredictPlot[:,2],linestyle='--')
prediccion_test,=plt.plot(testPredictPlot[:,2],linestyle='--')
plt.title('Consumo de agua')
plt.ylabel('cosumo (m3)')
plt.xlabel('dia')
plt.legend([serie,prediccion_entrenamiento,prediccion_test],['serie','entrenamiento','test'], loc='upper right')
This is the csv file I have created, if it helps. 这是我创建的csv文件,如果有帮助的话。
After changing the code, I fixed all the errors, but I'm not really sure about the results. 更改代码后,我修复了所有错误,但我不确定结果。 This is a zoom in the prediction plot:
这是预测图的放大:
which shows that there is a "displacement" in the values predicted and in the real ones. 这表明在预测值和实际值中存在“位移”。 When there is a max in the real time series, there is a min in the forecast for the same time, but it seems like it corresponds to the previous time step.
当实时系列中存在最大值时,预测中同时存在最小值,但看起来它与前一时间步长相对应。
Change 更改
a = dataset[i:(i + look_back), 0]
To 至
a = dataset[i:(i + look_back), :]
If you want the 3 features in your training data. 如果您想要训练数据中的3个功能。
Then use 然后用
model.add(LSTM(4, input_shape=(look_back,3)))
To specify that you have look_back
time steps in your sequence, each with 3 features. 要指定序列中有
look_back
时间步长,每个步骤都有3个look_back
。
It should run 它应该运行
EDIT : 编辑:
Indeed, sklearn.preprocessing.MinMaxScaler()
's function : inverse_transform()
takes an input which has the same shape as the object you fitted. 实际上,
sklearn.preprocessing.MinMaxScaler()
的函数: inverse_transform()
接受一个与您所适合的对象具有相同形状的输入。 So you need to do something like this : 所以你需要做这样的事情:
# Get something which has as many features as dataset
trainPredict_extended = np.zeros((len(trainPredict),3))
# Put the predictions there
trainPredict_extended[:,2] = trainPredict
# Inverse transform it and select the 3rd column.
trainPredict = scaler.inverse_transform(trainPredict_extended)[:,2]
I guess you will have other issues like this below in your code but nothing that you can't fix :) the ML part is fixed and you know where the error comes from. 我想你的代码中会有下面这样的其他问题,但没有你无法解决的问题:) ML部分是固定的,你知道错误的来源。 Just check the shapes of your objects and try to make them match.
只需检查对象的形状,并尝试使它们匹配。
位移可能是由于在给定数据随机性的情况下预测最大值/最小值的滞后。
You can change what you are optimizing, for maybe better results. 您可以更改正在优化的内容,以获得更好的结果。 For example, try predicting binary 0,1 if there will be a 'spike up' for the next day.
例如,如果第二天会出现“尖峰”,请尝试预测二进制0,1。 Then feed the probability of a 'spike up' as a feature to predict the usage itself.
然后提供“尖峰”的概率作为预测用法本身的特征。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.