我有一个时间序列中包含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的形状应为(None,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条答案
按热度按时间du7egjpx1#
您的Y形状与模型中的最后一层不匹配。您的Y的形式为
(num_samples, 1)
,这意味着对于每个样本,它都输出一个长度为1的向量。但是,最后一层是
Dense(3)
层,它输出(num_samples, 3)
,这意味着对于每个样本,它输出一个长度为3的向量。由于神经网络的输出与y数据的格式不同,因此神经网络无法训练。
您可以通过两种方式解决此问题:
1.通过将
Dense(3)
替换为Dense(1)
,将神经网络的输出转换为y数据的形状:2.通过修改
create_dataset()
函数将y数据的形状转换为神经网络的输出,以便将所有要素(而不是仅一个要素)添加到y:既然你说你想预测3特征,那么你很可能会使用第二个选项。注意,第二个选项确实会打断你代码的最后一部分来扩展y,但是你的模型训练得很好。