ValueError: 层序号_29 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。收到的完整形状:[无,22]

Posted

技术标签:

【中文标题】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] 【发布时间】:2021-08-01 04:34:12 【问题描述】:

X_train 的维度是 (7059, 22),y_train 是 (7059,)。数据集本身是来自 Google 云平台 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.收到的完整形状:[None, 22] 当我运行 model.fit() 时。谁能帮帮我?

【问题讨论】:

【参考方案1】:

我能够使用示例代码复制您的问题,如下所示

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) 

输出:

---------------------------------------------------------------------------
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)

输出:

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)

【讨论】:

以上是关于ValueError: 层序号_29 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。收到的完整形状:[无,22]的主要内容,如果未能解决你的问题,请参考以下文章

ValueError: 层 lstm_21 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。收到的完整形状:(无,546)

ValueError:层顺序的输入0与层不兼容::预期的min_ndim = 4,发现ndim = 2。收到的完整形状:(无,1024)

ValueError:层激活_1的输入不是符号张量

ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入具有形状

ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。收到的完整形状:[8, 28, 28]

Tensorflow ValueError:层“顺序”的输入0与层不兼容:预期形状=(无,20,20,3),找到形状=(无,20,3)