繁体   English   中英

“ValueError:名称“input_2”在 model 中使用了 2 次。所有图层名称都应该是唯一的。” keras 错误 seq2seq model

[英]"ValueError: The name "input_2" is used 2 times in the model. All layer names should be unique." Error in keras with seq2seq model

我在 Keras 库和 Seq2Seq model 的帮助下在 python 中编写了一个聊天机器人。我首先训练 model,然后将其保存到 a.h5 文件中并从该文件加载以使用经过训练的 model。但是,当我尝试加载时我的 model 来自 my.h5 文件,我收到错误:“ValueError:名称“input_2”在 model 中使用了 2 次。所有图层名称都应该是唯一的。” 作为参考,我用来加载 model 的代码是(以 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.

提前致谢。

尝试这个:

decoder_model = Model(inputs=[decoder_inputs].append(decoder_states_inputs), outputs=[decoder_outputs].append(decoder_states))
decoder_inputs_t = model.input[1]  # input_2
decoder_inputs = tf.identity(decoder_inputs_t)

这可以运行。

我知道我来晚了,但这可能对其他人有帮助。 文档页面中给出的代码也对我提出了错误

ValueError: The name "input_2" is used 2 times in the model. All layer names should be unique.

因此,我尝试自己查看错误,因为在线没有解决方案。 此错误来自解码器推理部分。 我做了一些改变,这个奇怪的技巧对我有用

而不是这样做 -

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)

我刚刚做了这个改变 -

try:
    latent_dim = 256
    decoder_inputs = keras.Input(shape=(None,))
    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)

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

我不知道它是如何或为什么以这种方式工作的,但它对我有用。

PS - 对于我的 seq2seq model,这是解码器输入形状decoder_inputs = keras.Input(shape=(None,))

我通过在为 decoder_state_input_hidden 和 decoder_state_input_cell 创建输入层时添加名称来消除此错误。 我认为问题在于这样做:

decoder_inputs = training_model.input[1] 

还有这个:

decoder_state_input_cell = Input(shape=(latent_dim,))

它将在 dec_model 中产生名为 input_2 的 2 层(第一个来自训练 model,第二个来自使用的第二个 Input(...))。

你应该试试这个:

decoder_state_input_hidden = Input(shape=(latent_dim,), name='anotherInput1')
decoder_state_input_cell = Input(shape=(latent_dim,), name='anotherInput2')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM