LSTM层输入维度不兼容报错:预期ndim=3,实际得到ndim=2
LSTM自编码器维度不匹配报错排查
问题背景
拥有含69个特征、225700行的数据集,运行LSTM自编码器代码时出现以下报错:
ValueError: Input 0 of layer "lstm_3" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 68)
用户代码
import pandas as pd from keras.layers import Input, LSTM, RepeatVector from keras.models import Model DF = pd.read_csv(r"C:\Users\44759\All_Autoencoder_Data.csv") DF1 = DF.drop('Labels', axis=1) # 移除标签特征 # 定义输入序列形状 input_seq_shape = (DF1.shape[1], 1) print(input_seq_shape) # 输出: (68, 1) # 定义LSTM自编码器模型 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)
问题原因
LSTM层默认参数return_sequences=False,此时输出是2D张量,形状为(batch_size, units)。而后续的LSTM层要求输入必须是3D张量,形状为(batch_size, timesteps, features)。
代码中第一层LSTM输出2D张量(None,68),直接传给第二层LSTM时,就会触发维度不匹配的报错。
解决方法
在编码部分的前两层LSTM中添加return_sequences=True,让它们返回完整的序列(3D张量),只有最后一层编码LSTM保持默认(return_sequences=False),输出2D张量给RepeatVector层使用。
修改后的编码部分代码:
# 定义LSTM自编码器模型 inputs = Input(shape=input_seq_shape) # 前两层LSTM添加return_sequences=True encoded = LSTM(68, activation='relu', return_sequences=True)(inputs) encoded = LSTM(32, activation='relu', return_sequences=True)(encoded) # 最后一层编码LSTM不需要返回序列 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)
额外注意事项
确保输入数据已经调整为符合LSTM要求的3D形状:(样本数, 时间步长, 特征数)。代码中input_seq_shape=(68,1),对应每个样本是68个时间步、每个时间步1个特征,需要提前将DF1转换为该形状:
# 转换输入数据形状 X = DF1.values.reshape(-1, DF1.shape[1], 1)
内容的提问来源于stack exchange,提问作者Frenzy
相关产品推荐
相关产品推荐

