首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模型中使用了2次"batch_normalization_1“的名称。

模型中使用了2次"batch_normalization_1“的名称。
EN

Stack Overflow用户
提问于 2019-01-16 09:24:17
回答 1查看 399关注 0票数 0

我试图对模型进行更改(替换层),但当我试图编译模型时,会遇到以下错误:

“batch_normalization_1”这个名字在模型中使用了2次

我不知道我做错了什么:

代码语言:javascript
复制
def add_batch_normalization(model_path):
    model = load_model(model_path)
    weights = model.get_weights()
    dense_idx = [index for index,layer in enumerate(model.layers) if type(layer) is Dense][-1] #get indices for dense layers
    x = model.layers[dense_idx -1].output
    new_model = Model(inputs = model.input, outputs = x)
    x= BatchNormalization()(new_model.output)
    x = Dense(2048, activation='relu')(x)
    x =BatchNormalization()(x)
    x = Dropout(.10)(x)
    x=  Dense(512, activation='relu')(x)
    x= BatchNormalization()(x)
    predictions = Dense(num_of_classes, activation='softmax')(x)
    new_model = Model(inputs= new_model.input, outputs=predictions)
    print(new_model.summary())
    model.set_weights(weights)
    return new_model

StackTrace:

代码语言:javascript
复制
Traceback (most recent call last):
  File "E:\test\APP test PROJECT\test\PYTHON SCRIPTS\testing_saved_models.py", line 542, in <module>
    MODEL =  add_batch_normalization(PATH)       #{load_model(PATH), add_conv_layers(PATH, how_many = 1), change_dropout(PATH, .5)   }
  File "E:\test\APP test PROJECT\test\PYTHON SCRIPTS\testing_saved_models.py", line 104, in add_batch_normalization
    new_model = Model(inputs= new_model.input, outputs=predictions)
  File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 231, in _init_graph_network
    self.inputs, self.outputs)
  File "C:\Users\yomog\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1455, in _map_graph_network
    ' times in the model. '
ValueError: The name "batch_normalization_1" is used 2 times in the model. All layer names should be unique.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-16 09:44:02

我的猜测是,您的模型已经具有批处理规范化层,当您添加一个新的层时,它的名称与现有的批处理规范化层中的一个相同。

在这种情况下,您应该手动定义新批处理规范化层的名称,这样就不会出现名称冲突,例如:

代码语言:javascript
复制
x = BatchNormalization(name='batch_normalization_11')(x)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54213855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档