Keras |无法定义模型 - ValueError:具有2个维度的输入的缩减维度2无效

时间:2018-05-02 15:59:54

标签: python tensorflow keras rnn

当我尝试创建此音频生成RNN时,我的输入面临一个奇怪的错误。但我真的不知道如何解释这个错误。

我创建了两个输入张量:噪音和暗淡的标签。 (100,)和(1,)。然后我嵌入了标签。然后我创建一个propper输入并初始化模型输入并返回带有输入和输出的完成模型。 该错误表明它无法将尺寸从2暗淡减少到2。输入和输入的形状[?,100],[2]是"不是"案件?

提前致谢!

代码:

调用:build_audio_generator(100,1)

def build_audio_generator(latent_dim, num_classes):

    model = Sequential()
    model.add(LSTM(512, input_dim=latent_dim, return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(512, return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(512))
    model.add(Dense(256))
    model.add(Dropout(0.3))
    model.add(Dense(num_classes))
    model.add(Activation('softmax'))

    model.summary()

    noise = Input(shape=(latent_dim,))
    label = Input(shape=(1,), dtype='int32')

    label_embedding = Flatten()(Embedding(num_classes, 100)(label))
    model_input = multiply([noise, label_embedding])

    sound = model(model_input)

    return Model([noise, label], sound)

错误:

Traceback (most recent call last):
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 686, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid reduction dimension 2 for input with 2 dimensions. for 'sequential_3/lstm_1/Sum' (op: 'Sum') with input shapes: [?,100], [2] and with comput

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 94, in <module>
    main()
  File "main.py", line 67, in main
    audio_generator = build_audio_generator(latent_dim, num_classes)
  File "C:\Users\MrGrimod\Desktop\gan-audio-generator\model.py", line 70, in build_audio_generator
    sound = model(model_input)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\topology.py", line 603, in __call__
    output = self.call(inputs, **kwargs)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py", line 546, in call
    return self.model.call(inputs, mask)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\topology.py", line 2061, in call
    output_tensors, _, _ = self.run_internal_graph(inputs, masks)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\topology.py", line 2212, in run_internal_graph
    output_tensors = _to_list(layer.call(computed_tensor, **kwargs))
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\layers\recurrent.py", line 2023, in call
    initial_state=initial_state)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\layers\recurrent.py", line 540, in call
    initial_state = self.get_initial_state(inputs)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\layers\recurrent.py", line 469, in get_initial_state
    initial_state = K.sum(initial_state, axis=(1, 2))  # (samples,)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 1242, in sum
    return tf.reduce_sum(x, axis=axis, keep_dims=keepdims)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1307, in reduce_sum
    name=name)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 4681, in _sum
    keep_dims=keep_dims, name=name)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2958, in create_op
    set_shapes_for_outputs(ret)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2209, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2159, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 627, in call_cpp_shape_fn
    require_shape_fn)
  File "C:\Users\MrGrimod\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 691, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Invalid reduction dimension 2 for input with 2 dimensions. for 'sequential_3/lstm_1/Sum' (op: 'Sum') with input shapes: [?,100], [2] and with computed input tensors: input[1] = <1 2>.

1 个答案:

答案 0 :(得分:0)

如您所知,LSTM输入应为ndim=3,在您的情况下为(None, None, 100)。我不确定你的意见。但是,根据代码,您的noisemodel_input是2D形状(None, 100)。这与要求不匹配,从而触发了错误。也许您想要将您的输入重新整理为noise = Input(shape=(None, latent_dim))