在mod中,名称“batch_normalization_1”被使用了2次

2024-05-12 21:32:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

The name 'batch_normalization_1" is used 2 times in the model'

我不知道我做错了什么:

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

堆栈跟踪:

^{pr2}$

Tags: pathinnewgetmodelislayersbatch
1条回答
网友
1楼 · 发布于 2024-05-12 21:32:16

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

在这种情况下,应手动定义新批处理规范化层的名称,以便不存在名称冲突,例如:

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

相关问题 更多 >