简体   繁体   English

Keras 重命名模型和层

[英]Keras rename model and layers

1) I try to rename a model and the layers in Keras with TF backend, since I am using multiple models in one script. 1) 我尝试使用 TF 后端重命名模型和 Keras 中的层,因为我在一个脚本中使用了多个模型。 Class Model seem to have the property model.name, but when changing it I get "AttributeError: can't set attribute".类模型似乎具有属性model.name,但是在更改它时我得到“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. 2)此外,我正在使用顺序 API,我想给层命名,这似乎可以使用功能 API,但我没有找到顺序 API 的解决方案。 Does anonye know how to do it for sequential API? anonye 是否知道如何为顺序 API 执行此操作?

UPDATE TO 2): Naming the layers works, although it seems to be not documented.更新至 2):命名图层有效,但似乎没有记录。 Just add the argument name, eg model.add(Dense(...,...,name="hiddenLayer1"). Watch out, Layers with same name share weights!只需添加参数名称,例如 model.add(Dense(...,...,name="hiddenLayer1")。注意,具有相同名称的图层共享权重!

For changing names of model.layers with tf.keras you can use the following lines:要使用 tf.keras 更改 model.layers 的名称,您可以使用以下几行:

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.我在一个双输入模型案例中需要这个,也遇到了“AttributeError: can't set attribute”。 The thing is that there is an underlying hidden attribute _name, which causes the conflict.问题是有一个潜在的隐藏属性_name,这会导致冲突。

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就命名层而言,您可以像这样在 Sequential 模型中进行

model=Sequential()
model.add(Dense(2,input_shape=(...),name="NAME"))

The Answer from user239457 only works with Standard keras .来自 user239457 的答案仅适用于标准 keras

If you want to use the Tensorflow Keras , you can do it like this:如果你想使用Tensorflow Keras ,你可以这样做:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential(name='Name')
model.add(Dense(2,input_shape=(5, 1)))

To rename a keras model in TF2.2.0:要在 TF2.2.0 中重命名 keras 模型:

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.要确认,请调用model.summary() ,您应该会看到新名称。

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:只是为了涵盖所有选项,关于问题的标题,如果您使用的是 Keras 功能 API,您可以通过以下方式定义模型和层名称:

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")

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'

Detailed answer is here How to rename Pre-Trained model ?详细答案在这里如何重命名预训练模型? ValueError 'Trained Model' is not a valid scope name ValueError 'Trained Model' 不是有效的范围名称

We can use model.name = "Model_Name" when are developing model and making it ready to train.我们可以在开发模型并准备好训练时使用model.name = "Model_Name" 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

In order to change the layer name of a pre-trained model on Tensorflow Keras, the solution is a bit more complex.为了在 Tensorflow Keras 上更改预训练模型的层名称,解决方案有点复杂。 A simple layer.name = "new_name" or layer._name = "new_name" as proposed by other answers will not work.其他答案提出的简单layer.name = "new_name"layer._name = "new_name"将不起作用。

This blog post offers a solution that works for that case. 这篇博文提供了适用于这种情况的解决方案。

for 1), I think you may build another model with right name and same structure with the exist one.对于 1),我认为您可以构建另一个与现有模型具有正确名称和相同结构的模型。 then set weights from layers of the exist model to layers of the new model.然后将现有模型层的权重设置为新模型层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM