Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras rename model and layers

Tags:

1) I try to rename a model and the layers in Keras with TF backend, since I am using multiple models in one script. Class Model seem to have the property model.name, but when changing it I get "AttributeError: can't set attribute". What is the Problem here?

2) Additionally, I am using sequential API and I want to give a name to layers, which seems to be possibile with Functional API, but I found no solution for sequential API. Does anonye know how to do it for sequential API?

UPDATE TO 2): Naming the layers works, although it seems to be not documented. Just add the argument name, e.g. model.add(Dense(...,...,name="hiddenLayer1"). Watch out, Layers with same name share weights!

like image 912
user3921232 Avatar asked Mar 29 '18 07:03

user3921232


People also ask

How do I rename a layer in Keras?

In order to change the layer name of a pre-trained model on Tensorflow Keras, the solution is a bit more complex. A simple layer.name = "new_name" or layer.

What is input layer in Keras?

In a Keras layer, the input shape is generally the shape of the input data provided to the Keras model while training. The model cannot know the shape of the training data. The shape of other tensors(layers) is computed automatically.


1 Answers

For changing names of model.layers with tf.keras you can use the following lines:

for layer in model.layers:     layer._name = layer.name + str("_2") 

I needed this in a two-input model case and ran into the "AttributeError: can't set attribute", too. The thing is that there is an underlying hidden attribute _name, which causes the conflict.

like image 187
ChriSquared Avatar answered Sep 26 '22 16:09

ChriSquared