首页 > 解决方案 > ValueError: Input 0 is incompatible with layer repeat_vector_58: expected ndim=2, found ndim=3

问题描述

I am trying to build intrusion detection LSTM and auto encoders. However I am not able to understand why repeat_vector_58 requires ndim=3. I am not able to figure this out. Below is my code:

x_train.shape: (8000, 1, 82)

x_test.shape: (2000, 1, 82)

x_train = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
x_test = np.reshape(testT, (testT.shape[0], 1, testT.shape[1]))

start = time.time()
model = Sequential()
model.add(LSTM(128, activation='relu',recurrent_dropout=0.5,return_sequences=True,input_dim=82))
model.add(RepeatVector(82))
model.add(Dropout(0.3))
model.add(LSTM(64, activation='relu',recurrent_dropout=0.5,return_sequences=False))
model.add(Dropout(0.3))
model.add(TimeDistributed(Dense(1,activation='softmax')))

ValueError: Input 0 is incompatible with layer repeat_vector_58: expected ndim=2, found ndim=3

标签: pythonkerasdeep-learninglstmautoencoder

解决方案


LSTM 层需要 3 维输入,因为它是循环层。预期的输入是(batch_size, timesteps, input_dim)。规范input_dim=82需要 2-dim 输入,但预期输入是 3-dim。
因此,您的错误的解决方案是更改input_dim=82input_shape=(82,1).

model = Sequential()
model.add(LSTM(128,activation='relu',recurrent_dropout=0.5,return_sequences=True,input_shape=(82,1)))

推荐阅读