修剪模型后,在Pytorch中删除weight_orig

问题描述

在Pytorch中修剪模型后,保存的模型同时包含修剪的权重和weight_orig。这将导致修剪的模型大小大于未修剪的模型。 有没有办法删除weight_orig并减少​​修剪的模型大小?

解决方法

offcial documentation中所述,您可以为此目的使用torch.nn.utils.prune.remove()
remove()根据weight_origweight_mask删除重新参数化,并删除forward_pre_hook。 您将像这样使用它:

for module in model.modules():
    if isinstance(module,torch.nn.Conv2d):
        prune.remove(module,'weight')
    # etc...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...