“lstm_3”层的输入 0 与该层不兼容,遗漏了什么?

问题描述 投票:0回答:2

我有一个包含 69 个特征和 225700 行的数据集,我尝试使用下面的代码运行 LSTM 模型,但我不断收到此错误消息

Input 0 of layer "lstm_3" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 68)
。请问有些事情我做对了吗?

DF = pd.read_csv(r"C:\Users\44759\All_Autoencoder_Data.csv")
DF1 = DF.drop('Labels', axis=1) # droping the Label feature

# Define input sequence shape
input_seq_shape = (DF1.shape[1], 1)

input_seq_shape
Out[8]:  (68, 1)

# Define LSTM autoencoder model
inputs = Input(shape=input_seq_shape)
encoded = LSTM(68, activation='relu')(inputs)
encoded = LSTM(32, activation='relu')(encoded)
encoded = LSTM(5, activation='relu')(encoded)

decoded = RepeatVector(DF1.shape[1])(encoded)
decoded = LSTM(5, activation='relu', return_sequences=True)(decoded)
decoded = LSTM(32, activation='relu', return_sequences=True)(decoded)
decoded = LSTM(68, activation='relu', return_sequences=True)(decoded)
decoded = LSTM(1, activation='sigmoid', return_sequences=True)(decoded)

当我运行上面的模型时,不断弹出下面的错误信息

ValueError                                Traceback (most recent call last)
<ipython-input-6-cf499e4225da> in <module>
      2 inputs = Input(shape=input_seq_shape)
      3 encoded = LSTM(68, activation='relu')(inputs)
----> 4 encoded = LSTM(32, activation='relu')(encoded)
      5 encoded = LSTM(5, activation='relu')(encoded)
      6 

~\anaconda3\lib\site-packages\keras\layers\rnn\base_rnn.py in __call__(self, inputs, initial_state, constants, **kwargs)
    554 
    555         if initial_state is None and constants is None:
--> 556             return super().__call__(inputs, **kwargs)
    557 
    558         # If any of `initial_state` or `constants` are specified and are Keras

~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

~\anaconda3\lib\site-packages\keras\engine\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    230             ndim = shape.rank
    231             if ndim != spec.ndim:
--> 232                 raise ValueError(
    233                     f'Input {input_index} of layer "{layer_name}" '
    234                     "is incompatible with the layer: "

ValueError: Input 0 of layer "lstm_3" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 68)
python tensorflow keras lstm
2个回答
0
投票

运行

LSTM
(或任何
recurrent
)模型时,
fit()
predict()
都期望输入数据的形状为
(batch_size, sequence_size, num_features)
.

如果你想在单个序列上运行模型,你可以执行以下操作:

input = np.expand_dims(input, axis=0)

阅读this question的一些答案以获得详细解释。


0
投票

如果你需要堆叠LSTM层,你需要设置

return_sequences=True
,因为你需要返回整个输出,而不仅仅是最后一个输出。 例如:

encoded = LSTM(68, return_sequences=True)(inputs)
encoded = LSTM(32, return_sequences=True)(encoded)
...

有了这个,输出将具有

(batch_size, sequence_size, num_features)
的形状。 如果你不添加
return_sequences=True
,输出形状将只是
(batch_size, num_features)
.

© www.soinside.com 2019 - 2024. All rights reserved.