如何修复'ValueError:输入0与层simple_rnn_1不兼容:预期形状=(无,无,20),找到形状=(无,无,2,20)'

Posted

技术标签:

【中文标题】如何修复\'ValueError:输入0与层simple_rnn_1不兼容:预期形状=(无,无,20),找到形状=(无,无,2,20)\'【英文标题】:How to fix 'ValueError: Input 0 is incompatible with layer simple_rnn_1: expected shape=(None, None, 20), found shape=(None, None, 2, 20)'如何修复'ValueError:输入0与层simple_rnn_1不兼容:预期形状=(无,无,20),找到形状=(无,无,2,20)' 【发布时间】:2019-05-25 05:58:26 【问题描述】:

我有几个矩阵通过多个层,最后一个是密集层,为每个矩阵生成一个向量。现在我希望将这些矩阵提供给 keras 的 RNN,这就是我面临这个错误的地方。

我尝试将向量堆叠在一起,以便将它们传递给 RNN。这是该想法的一段代码:

input1 = Dense(20, activation = "relu")(input1)
input2 = Dense(20, activation = "relu")(input2)
out = Lambda(lambda x: tf.stack([x[0], x[1]], axis=1), output_shape=(None, 2, 20))([input1, input2])
out = SimpleRNN(50, activation="relu")(out)

我收到:

>Traceback (most recent call last):
  >>File "model.py", line 106, in <module>
    model = make_model()

  >>File "model.py", line 60, in make_model
    out = SimpleRNN(50, activation="relu")(out) 

  >>File "/home/yamini/.local/lib/python3.6/site-packages/keras/layers/recurrent.py", line 532, in __call__
    return super(RNN, self).__call__(inputs, **kwargs)

  >>File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 440, in __call__
    self.assert_input_compatibility(inputs)

  >>File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 368, in assert_input_compatibility
    str(x_shape))

>>ValueError: Input 0 is incompatible with layer simple_rnn_1: expected shape=(None, None, 20), found shape=(None, None, 2, 20)

如果我在 Lambda 层更改 output_shape=(None, None, 20),我会得到:

Traceback (most recent call last):
 >> File "model.py", line 107, in <module>
    model.fit([input1, input2], y_train, epochs = 15, batch_size = 20, verbose = 2)

  >>File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/training.py", line 952, in fit
    batch_size=batch_size)

  >>File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
    exception_prefix='target')

  >>File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
    str(data_shape))

>>ValueError: Error when checking target: expected simple_rnn_1 to have shape (50,) but got array with shape (1,)

【问题讨论】:

input1input2 的形状是什么? @giser_yugang 都是(None, 20) 【参考方案1】:

您可以更改output_shape,其中不应包含batch_size

from keras.layers import Dense,Lambda,SimpleRNN,Input
import tensorflow as tf

input1 = Input(shape=(20,))
input2 = Input(shape=(20,))
input1 = Dense(20, activation = "relu")(input1)
input2 = Dense(20, activation = "relu")(input2)
out = Lambda(lambda x: tf.stack([x[0], x[1]], axis=1), output_shape=(2, 20))([input1, input2])
out = SimpleRNN(50, activation="relu")(out)

【讨论】:

我仍然收到ValueError: Error when checking target: expected simple_rnn_1 to have shape (50,) but got array with shape (1,) @yaminigoel 我工作正常。你改了吗? Lambda之后可以给print(out.shape)吗? 形状为model: Tensor("lambda_1/stack:0", shape=(?, 2, 20), dtype=float32) 好的,我再次复制粘贴了您的代码,但它仍然碰巧给出了同样的错误! 实际上,当我运行您的代码时,它并没有给出错误,但是当我将它嵌入我的代码 (model.py) 时,它给出了这个问题。我不知道为什么会这样!

以上是关于如何修复'ValueError:输入0与层simple_rnn_1不兼容:预期形状=(无,无,20),找到形状=(无,无,2,20)'的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:输入 0 与层 lstm_1 不兼容:预期 ndim=3,发现 ndim=2 [keras]

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

ValueError: 层 lstm_12 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4

ValueError:输入 0 与层 conv2d_1 不兼容:预期 ndim=4,发现 ndim=3

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

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