【问题标题】: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)

标签: tensorflow keras recurrent-neural-network rnn keras-layer


【解决方案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) 时,它给出了这个问题。我不知道为什么会这样!
猜你喜欢
  • 2021-11-04
  • 2022-01-03
  • 2022-06-18
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2021-12-28
  • 2020-08-01
  • 2021-12-10
相关资源
最近更新 更多