1

I am writing a chatbot in python with the help of the Keras library and the Seq2Seq model. I train the model first and then save that into a .h5 file and load from that file to use the trained model. However, when I try loading my model from my .h5 file I get the error: "ValueError: The name "input_2" is used 2 times in the model. All layer names should be unique." for reference the code I'm using to load the model is(with training_model.h5 as the save file)

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)

it doesn't seem like any layer names are being repeated to me. Could someone please help me figure out what the issue is.

stack trace:

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.

Thanks in Advance.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82

4 Answers4

1

I got rid of this error by adding names while creating the input layers for decoder_state_input_hidden and decoder_state_input_cell. I think the problem was that by doing this:

decoder_inputs = training_model.input[1] 

and this:

decoder_state_input_cell = Input(shape=(latent_dim,))

it will result in 2 layers named input_2 in the dec_model (the first one from the training model and the second one from the second Input(...) used).

You should try this instead:

decoder_state_input_hidden = Input(shape=(latent_dim,), name='anotherInput1')
decoder_state_input_cell = Input(shape=(latent_dim,), name='anotherInput2')
Anca
  • 11
  • 1
0

Try this :

decoder_model = Model(inputs=[decoder_inputs].append(decoder_states_inputs), outputs=[decoder_outputs].append(decoder_states))
Shivam Bharadwaj
  • 1,864
  • 21
  • 23
  • Thank you for answering. When I use this I get another error ``` ValueError: Input tensors to a Model must come from `keras.layers.Input`. Received: None (missing previous layer metadata).``` – Akshat Kumar Jun 22 '20 at 12:59
  • The same here, just error after using "append" version is different: AttributeError: 'NoneType' object has no attribute 'op' – user2194898 Aug 12 '20 at 11:27
0
decoder_inputs_t = model.input[1]  # input_2
decoder_inputs = tf.identity(decoder_inputs_t)

This can run.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 23 '22 at 08:48
0

I know I'm quite late, but it might be helpful for others. The code given in the documentation page is raising the error for me too

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

So, I tried looking into the error myself as there was no solution online. This error was coming from the decoder inference part. I made some changes and this weird trick worked for me

instead of doing this -

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)

I just made this change -

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)

I have no idea how or why it works this way, but it did for me.

P.S. - For my seq2seq model, this was the decoder input shape decoder_inputs = keras.Input(shape=(None,))

Bibekjit Singh
  • 142
  • 1
  • 1
  • 9