Skip to main content
Update
Source Link
n1k31t4
  • 15.1k
  • 2
  • 31
  • 51

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        
=================================================================
In [9]: for i, layer in enumerate(model.layers):
    ...:     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        
=================================================================

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        
=================================================================

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

ReanmeRename the layers

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

Reanme the layers

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

Rename the layers

Source Link
n1k31t4
  • 15.1k
  • 2
  • 31
  • 51

I made a toy example, which basically does the same as your code, and I show that the epxected 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 [5]: model.add(Dense(30, name='dense2'))
In [6]: model.add(Dense(1, name='dense3'))
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        
=================================================================

Reanme the layers

In [9]: for i, layer in enumerate(model.layers):
    ...:     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.