为了账号安全,请及时绑定邮箱和手机立即绑定

名称“batch_normalization_1”在模型中使用了 2 次

名称“batch_normalization_1”在模型中使用了 2 次

绝地无双 2021-10-12 15:05:58
我正在尝试对模型进行更改(替换图层),但是当我尝试编译模型时遇到了错误:名称“batch_normalization_1”在模型中使用了 2 次'我无法弄清楚我做错了什么: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堆栈跟踪: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
查看完整描述

1 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

我的猜测是您的模型已经有 Batch Normalization 层,当您添加一个新层时,它的名称与现有的 Batch Normalization 层之一具有相同的名称。

在这种情况下,您应该手动定义新的 Batch Normalization 层的名称,这样就不会出现名称冲突,例如:

x = BatchNormalization(name='batch_normalization_11')(x)


查看完整回答
反对 回复 2021-10-12
  • 1 回答
  • 0 关注
  • 222 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号