好的。我对深度学习还比较新手。当我运行我的代码时,遇到了 ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)
我的维度是
X = (6018,)y = (6018,)
我的模型是
model = Sequential() model.add(LSTM(64,input_shape=(1, 2),return_sequences=True))model.add(LSTM(64,input_shape=(1, 2)))model.add(Dense(32,activation='relu'))model.add(Dense(1,activation='softmax'))model.compile(optimizer='adam', loss = 'categorical_crossentropy',metrics = ['accuracy'])model.fit(X,y,epochs=25,batch_size=5)
我已经参考了这个,但当我尝试解决方案(当然是调整了数字)时,仍然得到了这个错误。任何帮助都会被感激的。
回答:
正如@*** 评论的,Tensorflow LSTM 期望输入形状为 3D,即,[batch_size, time_steps, feature_vector]。
工作样本代码
import tensorflow as tfinputs = tf.random.normal([32, 10, 8])print(inputs.shape)lstm = tf.keras.layers.LSTM(4)output = lstm(inputs)print(output.shape)print(output)
输出
(32, 10, 8)(32, 4)tf.Tensor([[ 0.2610239 0.05922208 0.27408293 0.06004168] [ 0.09504984 -0.09526936 0.15872665 0.06893176] [-0.00466381 0.25115085 0.09392853 0.30679247] [-0.10023813 -0.13132107 0.05071423 0.09878921] [ 0.02017667 -0.21159768 0.13270041 0.05142007] [-0.0524956 -0.04429652 0.01828319 -0.24461922] [ 0.4463299 -0.21829244 0.07631823 -0.02308152] [-0.21405925 -0.0525855 0.2637051 -0.36504647] [ 0.1591202 0.18965898 0.0729805 0.03004023] [-0.04382075 0.03762269 -0.2226677 -0.04472603] [-0.01629235 -0.15920192 0.23090638 0.00677661] [ 0.23487142 -0.07626372 -0.01555465 0.06253306] [ 0.2891141 0.1554318 -0.25290352 0.0484328 ] [ 0.11477407 0.07930709 -0.39913383 0.04535771] [ 0.24248329 -0.01814366 0.32974967 0.22873886] [-0.08170582 0.04182371 0.19988067 -0.00295247] [ 0.40137917 0.08512016 -0.26209465 0.04500046] [ 0.12149049 -0.14915761 0.26120573 0.3150496 ] [ 0.59085524 0.10106529 -0.34999618 0.03516199] [ 0.29735157 0.05837289 -0.06764269 -0.09297346] [-0.28203732 -0.33540457 0.02560275 -0.20562115] [-0.06041891 -0.12593071 0.00945436 0.12473141] [ 0.12043521 -0.11561332 -0.09308923 0.01790712] [-0.0218282 0.28214487 -0.11302455 0.0034459 ] [-0.12396459 -0.08603923 0.3626035 0.11152762] [ 0.13282476 -0.01545438 0.09813337 -0.002675 ] [ 0.5333185 -0.08465756 -0.27699044 -0.14487849] [-0.01635256 -0.2978716 -0.13272133 -0.07292715] [ 0.01473135 -0.04989992 0.11208256 0.19093426] [-0.042191 -0.11678784 -0.15534575 -0.02011094] [ 0.21434435 -0.0957195 0.44054344 -0.15512279] [ 0.4018504 0.20203398 -0.44193134 0.06368993]], shape=(32, 4), dtype=float32)