我正在尝试使用keras实现一个lstm模型。问题是我有不同形状的数据。我的数据如下:
col1 col2 col3 col4 col5
[1,2,3] [2,3,4] [3,4,5] [5,6,7] [4,5,9]
[0,2] [1,5] [1,24] [11,7] [-1,4]
[0,2,4,5] [1,5,7,8] [1,24,-7,6] [11,7,4,5] [-1,4,1,2]
我的代码是
import numpy as np
import pandas as pd
import h5py
from sklearn.model_selection import train_test_split
from keras.layers import Dense
from keras.layers import Input, LSTM
from keras.models import Model
X_train, X_test, y_train, y_test = train_test_split(X, y_target, test_size=0.2, random_state=1)
batch_size = 32
timesteps = 300
output_size = 1
epochs=120
inputs = Input(batch_shape=(batch_size, timesteps, output_size))
lay1 = LSTM(10, stateful=True, return_sequences=True)(inputs)
lay2 = LSTM(10, stateful=True, return_sequences=True)(lay1)
output = Dense(units = output_size)(lay2)
regressor = Model(inputs=inputs, outputs = output)
regressor.compile(optimizer='adam', loss = 'mae')
regressor.summary()
for i in range(epochs):
print("Epoch: " + str(i))
regressor.fit(X_train, y_train, shuffle=False, epochs = 1, batch_size = batch_size)
regressor.reset_states()
运行代码时出现的错误是:
ValueError: Error when checking input: expected input_5 to have 3 dimensions, but got array with shape (11200, 5) #11200 lines, 5 columns
由于
答案 0 :(得分:0)
多维numpy数组需要具有清晰的形状,因此在同一个numpy数组中放置不同长度的数组将导致一个numpy对象数组,而不是所需的多维数组。
所以基本上它不可能一次性将数据提供给keras。
然而,有几种可能的解决方案。其中大多数要求您的时间尺度中的keras输入形状必须为None:
最后两个选项需要使用fit_generator选项,因为您必须逐步提供数据。