0

好的。所以我对深度学习还很陌生。当我运行我的代码时,我得到了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)

我已经引用了这个,但是当我尝试解决方案(当然调整数字)时,我仍然得到了错误。任何帮助,将不胜感激。

4

1 回答 1

1

正如@Shubham Panchal 评论的那样,Tensorflow LSTM 期望输入 3D 形状,即[batch_size, time_steps, feature_vector]

工作示例代码

import tensorflow as tf
inputs = 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)
于 2021-07-02T09:11:12.730 回答