Reputation: 617
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!
Upvotes: 40
Views: 64486
Reputation: 879
As pointed out by the author, simply changing the name of a custom layer is not enough if you wish to use said layer multiple times. Here is a temporary but very viable solution of mine. Please feel free to copy/paste:
def name_custom_activation(activation):
""" Currently, the Tensorflow library does not provide auto
incrementation for custom layer names. Tensorflow Graphs
requires each layer to have a unique name. Manually
incrementing layer names is annoying. The following trick
is the best method to change a layer name. It is especially
useful when using tf.keras.utils.plot_model to look at
synergistic activation models.
"""
class_name = tf.keras.layers.Activation.__name__
tf.keras.layers.Activation.__name__ = f'Activation_{activation.__name__}'
activation_layer = tf.keras.layers.Activation(activation)
tf.keras.layers.Activation.__name__ = class_name
return activation_layer
When plotting your model's graph, it looks like this:
def my_custom_activation(x):
return ... # Create your own activation function.
def create_model():
return ... # Add your own model, which uses your custom activation.
model = create_model()
tf.keras.utils.plot_model(model, show_shapes=True)
Upvotes: 0
Reputation: 1886
Doesn't work any more as per tf2+
Your first problem about the model name is not reproducible on my machine. I can set it like this. many a times these errors are caused by software versions.
model=Sequential()
model.add(Dense(2,input_shape=(....)))
model.name="NAME"
As far as naming the layers, you can do it in Sequential model like this
model=Sequential()
model.add(Dense(2,input_shape=(...),name="NAME"))
Latest solution
use _name
Upvotes: 35
Reputation: 1908
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._name = "new_name"
as proposed by other answers will not work.
This blog post offers a solution that works for that case.
Upvotes: 1
Reputation: 397
To change only one layer name in a model you can use the following lines:
my_model.layers[0]._name = 'my_new_name_for_the_first_layer'
my_model.layers[1]._name = 'my_new_name_for_the_second_layer'
my_model.layers[-1]._name = 'my_new_name_for_the_last_layer'
Upvotes: 4
Reputation: 1804
To rename a keras model in TF2.2.0:
model._name = "newname"
I have no idea if this is a bad idea - they don't seem to want you to do it, but it does work. To confirm, call model.summary()
and you should see the new name.
Upvotes: 13
Reputation: 91
Detailed answer is here How to rename Pre-Trained model ? ValueError 'Trained Model' is not a valid scope name
We can use model.name = "Model_Name"
when are developing model and making it ready to train. We can also give name to layers. Ex:
model = Sequential()
model.name = "My_Model" #Naming model
model.add(Dense(2,input_shape=(...),name="Name") #Naming layer
Upvotes: 0
Reputation: 509
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.
Upvotes: 47
Reputation: 3577
Just to cover all the options, regarding the title of the question, if you are using the Keras functional API you can define the model and the layers name by:
inputs = Input(shape=(value, value))
output_layer = Dense(2, activation = 'softmax', name = 'training_output')(dropout_new_training_layer)
model = Model(inputs= inputs, outputs=output_layer, name="my_model")
Upvotes: 4
Reputation: 113
The Answer from user239457 only works with Standard keras.
If you want to use the Tensorflow Keras, you can do it like this:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential(name='Name')
model.add(Dense(2,input_shape=(5, 1)))
Upvotes: 11
Reputation: 41
for 1), I think you may build another model with right name and same structure with the exist one. then set weights from layers of the exist model to layers of the new model.
Upvotes: -4