【问题标题】:"ValueError: The name "input_2" is used 2 times in the model. All layer names should be unique." Error in keras with seq2seq model“ValueError:名称“input_2”在模型中使用了 2 次。所有层名称都应该是唯一的。”使用 seq2seq 模型的 keras 错误
【发布时间】:2020-06-22 12:34:48
【问题描述】:

在 Keras 库和 Seq2Seq 模型的帮助下,我正在用 python 编写一个聊天机器人。我首先训练模型,然后将其保存到 .h5 文件中并从该文件加载以使用经过训练的模型。但是,当我尝试从 .h5 文件加载模型时,出现错误:“ValueError:名称“input_2”在模型中使用了 2 次。所有图层名称都应该是唯一的。”供参考我用来加载模型的代码是(以 training_model.h5 作为保存文件)

latent_dim = 256
decoder_inputs = training_model.input[1] 
decoder_state_input_hidden = Input(shape=(latent_dim,))
decoder_state_input_cell = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_hidden, decoder_state_input_cell]
decoder_lstm = training_model.layers[3]
decoder_outputs, state_hidden, state_cell = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_hidden, state_cell]
decoder_dense = training_model.layers[4]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)

我似乎没有重复任何图层名称。有人可以帮我找出问题所在。

堆栈跟踪:

Traceback (most recent call last):
  File "chatbot.py", line 169, in <module>
    decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)
  File "C:\_MyPrograms\anaconda\envs\Alicia\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\_MyPrograms\anaconda\envs\Alicia\lib\site-packages\keras\engine\network.py", line 94, in __init__
    self._init_graph_network(*args, **kwargs)
  File "C:\_MyPrograms\anaconda\envs\Alicia\lib\site-packages\keras\engine\network.py", line 241, in _init_graph_network
    self.inputs, self.outputs)
  File "C:\_MyPrograms\anaconda\envs\Alicia\lib\site-packages\keras\engine\network.py", line 1523, in _map_graph_network
    ' times in the model. '
ValueError: The name "input_2" is used 2 times in the model. All layer names should be unique.

提前致谢。

【问题讨论】:

标签: python keras seq2seq


【解决方案1】:

试试这个:

decoder_model = Model(inputs=[decoder_inputs].append(decoder_states_inputs), outputs=[decoder_outputs].append(decoder_states))

【讨论】:

  • 感谢您的回答。当我使用它时,我得到另一个错误```ValueError:模型的输入张量必须来自keras.layers.Input。收到:无(缺少上一层元数据)。```
  • 这里也一样,只是使用“append”版本不同后报错:AttributeError: 'NoneType' object has no attribute 'op'
【解决方案2】:
decoder_inputs_t = model.input[1]  # input_2
decoder_inputs = tf.identity(decoder_inputs_t)

这可以运行。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2017-09-13
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
相关资源
最近更新 更多