简体   繁体   English

层“lstm”的输入 0 与层不兼容:预期形状=(1, None, 1),找到的形状=(32, 9, 1)

[英]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.

相关问题 ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3) Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33) - Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) ValueError: 层“sequential_32”的输入 0 与层不兼容:预期形状=(None, 3, 1),发现形状=(32, 0, 1) - ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) - TensorFlow ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError: 层 lstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,18] - ValueError : Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 18] LSTM 模型:ValueError:层“lstm”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,7) - LSTM model: ValueError: Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 7) ValueError:“顺序”层的输入 0 与层不兼容:预期形状 =(无,223461,5),找到形状 =(无,5) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 455, 30),找到的形状 =(None, 30) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 455, 30), found shape=(None, 30) ValueError: 层“sequential_1”的输入 0 与层不兼容:预期形状=(None, 60, 1),发现形状=(None, 59, 1) - ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 60, 1), found shape=(None, 59, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM