Jack
Jack

Reputation: 23

tf.keras "All layer names should be unique." but layer names are already changed

I am trying create an ensemble of lstm. Below is my implementation of one lstm:

def lstm_model(n_features, n_hidden_unit, learning_rate, p, recurrent_p):
   model = keras.Sequential()
   model.add(Masking(mask_value=-1, input_shape=(100, n_features)))
   model.add(Bidirectional(LSTM(n_hidden_unit, input_shape=(None, n_features), return_sequences=True,
                            dropout = p, recurrent_dropout = recurrent_p)))

   model.add(TimeDistributed(Dense(3, activation='softmax')))
   model.compile(loss=CategoricalCrossentropy(from_logits=True),
              optimizer=Adam(learning_rate=learning_rate), 
              metrics=['categorical_accuracy'])
   return model

Then I trained a few lstm. The models are stored as a list then pass into the function below

def define_stacked_model(members):
    for i in range(len(members)):
        model = members[i]['model']
        model.input._name = 'ensemble_' + str(i+1) + '_' + model.input.name
        for layer in model.layers:
            # make not trainable
            layer.trainable = False
            # rename to avoid 'unique layer name' issue
            layer._name = 'ensemble_' + str(i+1) + '_' + layer.name

            print(layer._name)

    # define multi-headed input
    ensemble_visible = [model_dictionary['model'].input for model_dictionary in members]

    # concatenate merge output from each model
    ensemble_outputs = [model_dictionary['model'].output for model_dictionary in members]

    merge = tf.keras.layers.Concatenate(axis=2)(ensemble_outputs)
    lstm_n_features = merge.shape[-1]

    stack_lstm = Bidirectional(LSTM(25, input_shape=(None, lstm_n_features), return_sequences=True,
                                dropout = 0, recurrent_dropout = 0))(merge)

    output = TimeDistributed(Dense(3, activation='softmax'))(stack_lstm)
    model = Model(inputs=ensemble_visible, outputs=output)
    # plot graph of ensemble
    plot_model(model, show_shapes=True, to_file='model_graph.png')
    rename(model, model.layers[1], 'new_name')
    # compile
    model.compile(loss=CategoricalCrossentropy(from_logits=True),
                  optimizer=Adam(learning_rate=learning_rate), 
                  metrics=['categorical_accuracy'])
    return model

The output shows the layer and input names are already changed

ensemble_1_masking_input:0
ensemble_1_masking
ensemble_1_bidirectional
ensemble_1_time_distributed
ensemble_2_masking_input_1:0
ensemble_2_masking
ensemble_2_bidirectional
ensemble_2_time_distributed
ensemble_3_masking_input_2:0
ensemble_3_masking
ensemble_3_bidirectional
ensemble_3_time_distributed
ensemble_4_masking_input_3:0
ensemble_4_masking
ensemble_4_bidirectional
ensemble_4_time_distributed

but there is a value error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-30bc0e96adc5> in <module>
     25 #hidden = Dense(10, activation='relu')(merge)
     26 output = TimeDistributed(Dense(3, activation='softmax'))(stack_lstm)
---> 27 model = Model(inputs=ensemble_visible, outputs=output)
     28 #model = Model(inputs=ensemble_visible)
     29 # plot graph of ensemble

~\anaconda3\envs\env_notebook\lib\site-packages\tensorflow\python\keras\engine\training.py in __init__(self, *args, **kwargs)
    165 
    166   def __init__(self, *args, **kwargs):
--> 167     super(Model, self).__init__(*args, **kwargs)
    168     _keras_api_gauge.get_cell('model').set(True)
    169     # Model must be created under scope of DistStrat it will be trained with.

~\anaconda3\envs\env_notebook\lib\site-packages\tensorflow\python\keras\engine\network.py in __init__(self, *args, **kwargs)
    171         'inputs' in kwargs and 'outputs' in kwargs):
    172       # Graph network
--> 173       self._init_graph_network(*args, **kwargs)
    174     else:
    175       # Subclassed network

~\anaconda3\envs\env_notebook\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
    454     self._self_setattr_tracking = False  # pylint: disable=protected-access
    455     try:
--> 456       result = method(self, *args, **kwargs)
    457     finally:
    458       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~\anaconda3\envs\env_notebook\lib\site-packages\tensorflow\python\keras\engine\network.py in _init_graph_network(self, inputs, outputs, name, **kwargs)
    304 
    305     # Keep track of the network's nodes and layers.
--> 306     nodes, nodes_by_depth, layers, _ = _map_graph_network(
    307         self.inputs, self.outputs)
    308     self._network_nodes = nodes

~\anaconda3\envs\env_notebook\lib\site-packages\tensorflow\python\keras\engine\network.py in _map_graph_network(inputs, outputs)
   1800   for name in all_names:
   1801     if all_names.count(name) != 1:
-> 1802       raise ValueError('The name "' + name + '" is used ' +
   1803                        str(all_names.count(name)) + ' times in the model. '
   1804                        'All layer names should be unique.')

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

I cant find where is this "masking_input" and how to change it.

Upvotes: 0

Views: 1434

Answers (1)

JMSB
JMSB

Reputation: 21

Not sure if you ever fixed this, or if anyone else will find the same issue; I just spent hours on the same problem and finally found the solution.

When listing layers for renaming, instead of model.layers, you have to use model._layers, otherwise the input name remains unchanged. Changing model.inputs.name does not work and produces the mentioned error.

Upvotes: 2

Related Questions