如何从预训练模型中去除正则化?

2024-05-14 22:46:53 发布

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

我在Keras中得到了一个部分训练的模型,在进一步训练之前,我想更改dropout、l2正则化器、高斯噪声等的参数。我将模型保存为.h5文件,但是当我加载它时,我不知道如何删除这些正则化层或更改它们的参数。有什么线索能告诉我怎么做吗?你知道吗


Tags: 文件模型参数噪声dropoutkerash5化器
2条回答

不用将整个模型保存到一个.h5文件中,你可以用你自己的格式分别保存每个层的权重。e、 g

import pickle

# Create model and train ...

#save the weights for each layer in your model
network_config = {
    'layer1': layer1.get_weights(),
    'layer2': layer2.get_weights(),
    'layer3': layer3.get_weights()
}

with open('network_config.pickle', 'wb') as file:
    pickle.dump(network_config, file)

然后,只能为仍在使用的层加载权重。你知道吗

with open('network_config.pickle', 'rb') as file:
    network_config = pickle.load(file)

#build new model that may be missing some layers

layer1.set_weights(network_config['layer1'])
layer3.set_weights(network_config['layer3'])

使用所需的超参数创建模型,并使用load_weight()将参数加载到模型中。你知道吗

相关问题 更多 >

    热门问题