# How to change the names of the layers of deep learning in Keras?

I am using vgg16 to create a deep learning model. I want to know how to change the names of the layers of deep learning in Keras?

I tried this

for layer in vgg_model.layers:
layer.name = layer.name + str("_")


But when I change the names of the layers, the model accuracy become low.

• Have you seen here? Nov 7 '18 at 20:57
• Changing the string name attribute of a layer should not affect the accuracy of a model. They are simply descriptors. Nov 7 '18 at 23:01
• Thanks @Media, I saw the link, but the problem is little different.
– N.IT
Nov 8 '18 at 8:11

I made a small example, which basically does the same as your code, and I show that the expected results are obtained:

### Imports

In [1]: from keras.models import Sequential
In [2]: from keras.layers import Dense


## Build a model

Note the name arguments:

In [3]: model = Sequential()
In [4]: model.add(Dense(50, input_shape=(20, 20), name='dense1'))
In [7]: model.compile('rmsprop', 'mse')
In [8]: model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense1 (Dense)               (None, 20, 50)            1050
_________________________________________________________________
dense2 (Dense)               (None, 20, 30)            1530
_________________________________________________________________
dense3 (Dense)               (None, 20, 1)             31
=================================================================


## Rename the layers

EDIT: it seems newer versions of Keras and the tf.keras API now do not allow renaming layers via layer.name = "new_name". Instead you must assign your new name to the private attribute, layer._name.

In [9]: for i, layer in enumerate(model.layers):
...:     # layer.name = 'layer_' + str(i)    <-- old way
...:     layer._name = 'layer_' + str(i)
In [10]: model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
layer_0 (Dense)              (None, 20, 50)            1050
_________________________________________________________________
layer_1 (Dense)              (None, 20, 30)            1530
_________________________________________________________________
layer_2 (Dense)              (None, 20, 1)             31
=================================================================


So we see the layers now have the new names!

## Further inspection

I can see that the internal identifiers of the layers themselves are not changed. I show the first layer's weights, as we see the original label is still present:

In [11]: a = model.layers[0]
In [12]: a.weights
Out[12]:
[<tf.Variable 'dense1/kernel:0' shape=(20, 50) dtype=float32_ref>,
<tf.Variable 'dense1/bias:0' shape=(50,) dtype=float32_ref>]


We see the dense1 name is still visible in the layer container, along with its unchanged shape and datatype.

I also initialised the model weights by running model.get_weights(), then renamed the layers as before to new names, compiled the model and saw that the layers' names were altered as before, and the weights themselves were not changed i.e. reinitialised. The exact same values were contained. This means that renaming your layers cannot be behind the drop in accuracy.

• I am using vgg16..... In my case, the weights and the structures of the model before and after changing the names are same. But the performance is different. It is strange :/
– N.IT
Nov 8 '18 at 8:14

Maybe it is caused by not correctly load pre-trained weights. Check if you use model.load_weights with by_name=True

See this.

You need to use: layer._name = layer.name + str('asdf')