PyTorch去除批量范数后,得到了不同的模型结果

2024-05-19 22:11:16 发布

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

我从模型中移除了批处理规范层,并加载了所有其他层的权重以进行推断。原始模型与无批量范数模型的预测结果不尽相同。你知道吗

这种差异是不是因为取消了批量定额造成的?你知道吗

#load pretrained model
checkpoint = torch.load(restore_file)
pretrained_model = checkpoint['model']
pretrained_dict = pretrained_model.state_dict()

#create new model without batch norm
model = no_batch_model()
model_dict = model.state_dict()

# 1. filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}

# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)

# 3. load the new state dict
model.load_state_dict(pretrained_dict)

Tags: thein模型规范范数newmodelbatch