我正在尝试构建入侵检测LSTM和自动编码器。但是我不明白为什么repeat_vector_58需要ndim = 3。我无法弄清楚。下面是我的代码:
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
答案 0 :(得分:2)
LSTM 层需要 3 维输入,因为它是循环层。预期的输入是 (batch_size, timesteps, input_dim)
。
规范 input_dim=82
需要 2-dim 输入,但预期输入是 3-dim。
因此,解决您的错误的方法是将 input_dim=82
更改为 input_shape=(82,1)
。
model = Sequential()
model.add(LSTM(128,activation='relu',recurrent_dropout=0.5,return_sequences=True,input_shape=(82,1)))