[英]Keras LSTM input shape error for input shape
I'm getting this error when using Time Series with Keras: 将时间序列与Keras结合使用时出现此错误:
ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (31, 3)
This is my function: 这是我的功能:
def CreateModel(shape):
"""Creates Keras Model.
Args:
shape: (set) Dataset shape. Example: (31,3).
Returns:
A Keras Model.
Raises:
ValueError: Invalid shape
"""
if not shape:
raise ValueError('Invalid shape')
logging.info('Creating model')
model = Sequential()
model.add(LSTM(4, input_shape=(31, 3)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
return model
Main code: 主要代码:
print(training_features.shape)
model = CreateModel(training_features.shape)
model.fit(
training_features,
training_label,
epochs=FLAGS.epochs,
batch_size=FLAGS.batch_size,
verbose=FLAGS.keras_verbose_level)
Complete error: 完成错误:
Traceback (most recent call last):
File "<embedded module '_launcher'>", line 149, in run_filename_as_main
File "<embedded module '_launcher'>", line 33, in _run_code_in_main
File "model.py", line 300, in <module>
app.run(main)
File "absl/app.py", line 433, in run
_run_main(main, argv)
File "absl/app.py", line 380, in _run_main
sys.exit(main(argv))
File "model.py", line 274, in main
verbose=FLAGS.keras_verbose_level)
File "keras/models.py", line 960, in fit
validation_steps=validation_steps)
File "keras/engine/training.py", line 1581, in fit
batch_size=batch_size)
File "keras/engine/training.py", line 1414, in _standardize_user_data
exception_prefix='input')
File "keras/engine/training.py", line 141, in _standardize_input_data
str(array.shape))
ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (31, 3)
Code originally from here 最初来自这里的代码
I have tried: 我努力了:
training_features = numpy.reshape(
training_features,
(training_features.shape[0], 1, training_features.shape[1]))
But I get: 但是我得到:
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4
If your original data is (31,3) then I think what you're looking for is a training_features.shape = (31,3,1). 如果您的原始数据是(31,3),那么我认为您正在寻找的是training_features.shape =(31,3,1)。 You can get that with the following line...
您可以通过以下行来获取...
training_features = training_features.reshape(-1, 3, 1)
This will simply add a new axis to the existing data (the -1 just tells numpy to figure out this dimension using the values in the original data). 这将仅向现有数据添加一个新轴(-1只是告诉numpy使用原始数据中的值来确定该维)。
You also need to fix your model's input shape. 您还需要修复模型的输入形状。 The 31 should be the number of samples in your data.
31应该是您数据中的样本数。 This doesn't get included in the Keras
input_shape
parameter. 这不会包含在
input_shape
参数中。 You should be using... 您应该使用...
model.add(LSTM(4, input_shape=(3, 1)))
Keras will automatically set the batch size to None
meaning that any number of samples will work with the model. Keras会自动将批次大小设置为“
None
这意味着该模型可以使用任何数量的样本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.