[英]Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found shape=(32, 9, 1)
This is the code in question:这是有问题的代码:
batch_size = 1
epochs = 1
begin_from_row = 3
rows_to_train = 1000
data = data.loc[begin_from_row:rows_to_train, :]
data['Close_next'] = data['Close'].shift(-1)
data = data.dropna()
output_data = data['Close_next']
input_data = data.drop(columns=['Close_next'])
input_size = 9
output_size = 1
hidden_size_1 = 9
input_layer = tf.keras.Input(batch_shape=(batch_size, input_size))
input_layer_expanded = tf.expand_dims(input_layer, axis=-1)
hidden_1 = tf.keras.layers.LSTM(hidden_size_1, stateful=True)(input_layer_expanded)
output_layer = tf.keras.layers.Dense(1, activation='relu')(hidden_1)
model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
model.compile(loss='mean_squared_error', optimizer='adam', run_eagerly=True)
model.fit(input_data, output_data, epochs=epochs)
model.save("model_1.h5")
It returns the following error:它返回以下错误:
Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found shape=(32, 9, 1)
I can't quite get where it gets the number 32 from, since it soesn'™ appear anywhere in my code我不太明白它从哪里得到数字 32,因为它 soesn'™ 出现在我的代码中的任何地方
The code works when I specify the batch_size=32, just for one batch.当我为一个批次指定 batch_size=32 时,代码有效。 The number 32 doesn't appear anywhere in the code, so I would like to know where it's coming from.
数字 32 没有出现在代码中的任何地方,所以我想知道它是从哪里来的。
The vector (32, 9, 1) represents the size of your input data, whereas (1, None, 1) is the expected shape that you defined in the InputLayer (batch_size = 1).向量 (32, 9, 1) 表示输入数据的大小,而 (1, None, 1) 是您在 InputLayer (batch_size = 1) 中定义的预期形状。
Batch_size 32 is the default value in the fit method. batch_size 32 是 fit 方法中的默认值。 The default value is being used since you did not specify the batch_size argument and you are not using tensorflow datasets:
由于您没有指定 batch_size 参数并且您没有使用 tensorflow 数据集,因此正在使用默认值:
batch_size: Integer or None.
batch_size:整数或无。 Number of samples per gradient update.
每次梯度更新的样本数。 If unspecified, batch_size will default to 32. Do not specify the batch_size if your data is in the form of datasets, generators, or keras.utils.Sequence instances (since they generate batches).
如果未指定,batch_size 将默认为 32。如果您的数据是数据集、生成器或 keras.utils.Sequence 实例的形式(因为它们生成批次),请不要指定 batch_size。
From: Tensorflow fit function来自: Tensorflow 拟合函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.