簡體   English   中英

ValueError: 層序號_29 的輸入 0 與層不兼容:預期 ndim=3,發現 ndim=2。 收到的完整形狀:[無,22]

[英]ValueError: Input 0 of layer sequential_29 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 22]

X_train 的維度是 (7059, 22),y_train 是 (7059,)。 數據集本身是來自谷歌雲平台 samples.gsod 的數值天氣數據集,可公開獲得。

model = Sequential()
model.add(keras.Input(shape=(X_train.shape[1],1)))
model.add(keras.layers.SimpleRNN(100, return_sequences=True, activation="relu"))
model.add(keras.layers.SimpleRNN(75, activation="softmax"))
model.add(keras.layers.Dense(1))

model.compile(
    loss=keras.losses.BinaryCrossentropy(from_logits=True),
    optimizer=keras.optimizers.Adam(),
    metrics=["accuracy"]
)

model.fit(X_train, y_train, batch_size=64, epochs=10, verbose=2)
model.evaluate(X_test, y_test, batch_size=64, verbose=2)

我收到以下錯誤 ValueError: Input 0 of layer sequence_29 is in compatible with the layer: expected ndim=3, found ndim=2. 收到的完整形狀:[無,22] 當我運行 model.fit() 時。 誰能幫我嗎?

我能夠使用示例代碼復制您的問題,如下所示

import tensorflow as tf
import numpy as np

inputs = np.random.random([10, 8]).astype(np.float32)
simple_rnn = tf.keras.layers.SimpleRNN(4)

output = simple_rnn(inputs) 

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-5be0091c56c4> in <module>()
      5 simple_rnn = tf.keras.layers.SimpleRNN(4)
      6 
----> 7 output = simple_rnn(inputs)  
      8 

2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    217                          'expected ndim=' + str(spec.ndim) + ', found ndim=' +
    218                          str(ndim) + '. Full shape received: ' +
--> 219                          str(tuple(shape)))
    220     if spec.max_ndim is not None:
    221       ndim = x.shape.rank

ValueError: Input 0 of layer simple_rnn is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (10, 8)

固定代碼:

SimpleRNN期望輸入一個 3D 張量,其形狀為[batch, timesteps, feature]

inputs = np.random.random([32, 10, 8]).astype(np.float32)
simple_rnn = tf.keras.layers.SimpleRNN(4)

output = simple_rnn(inputs) 
print(output)

Output:

tf.Tensor(
[[-0.7171318  -0.08893692 -0.69077575  0.38328102]
 [-0.83120173 -0.14909095 -0.71403515  0.4345429 ]
 [-0.6006592   0.29866692 -0.8272924   0.05154758]
 [-0.7838807  -0.47415066 -0.70932215  0.5764332 ]
 [-0.7824479  -0.45385727 -0.8656322   0.28529072]
 [-0.6194738  -0.18733113 -0.5153756   0.3143776 ]
 [-0.95213604  0.41222277 -0.547589    0.33968422]
 [-0.7492875   0.18794847 -0.26124486  0.3043786 ]
 [-0.61159176 -0.743155   -0.07791959  0.64934397]
 [-0.5336786  -0.0184313  -0.774236    0.34506366]
 [-0.88712215 -0.03032754 -0.28529617  0.5635988 ]
 [-0.5926473  -0.49532327 -0.69920903  0.31282505]
 [-0.90393895 -0.05117951 -0.15240784  0.124594  ]
 [-0.7957143   0.04542146 -0.69029963  0.6492506 ]
 [-0.5646224   0.05792991 -0.21317112  0.34447974]
 [-0.90470845 -0.05670586 -0.37624207  0.3244714 ]
 [-0.88079983 -0.01762105 -0.09037696 -0.28829068]
 [-0.95380247 -0.09199464 -0.3780675   0.46749404]
 [-0.6376102   0.1043698  -0.89859253  0.3811665 ]
 [-0.4754285   0.23955886 -0.75150895  0.57153827]
 [-0.8260284  -0.1638191  -0.8365587   0.70133436]
 [-0.8197604  -0.460793   -0.45423204  0.5086527 ]
 [-0.8188014  -0.29039773 -0.39448202 -0.58558536]
 [-0.8414408  -0.04482244 -0.08608516  0.5385121 ]
 [-0.8133365   0.30670735 -0.857128    0.38289943]
 [-0.92091554 -0.17124711 -0.36027014  0.21229681]
 [-0.6782963  -0.5565081  -0.85855854  0.14851192]
 [-0.9134299   0.00566503 -0.37631485  0.1724117 ]
 [-0.8070814  -0.34617537 -0.05682215  0.6945626 ]
 [-0.5029106  -0.01262121 -0.73743176  0.26491827]
 [-0.85670465 -0.817243   -0.81651765  0.3292996 ]
 [-0.8086945  -0.7836522  -0.5303039   0.39167196]], shape=(32, 4), dtype=float32)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM