LSTM 上的 Pytorch 修剪会增加模型大小吗?

问题描述

我正在使用 pytorch 的 torch.nn.utils.prune 对具有 LSTM 层的模型进行修剪。但是,当我保存 state_dict内容时,模型比修剪前大得多。不知道为什么,好像我把剪枝前后state_dict的元素的大小打印出来一样,一切都是一样的维度,而且state_dict中没有额外的元素。

我的修剪代码非常标准,我确保调用 prune.remove()

        model_state = model.state_dict()
        torch.save(model.state_dict(),'pre_pruning.pth')
        for param_tensor in model_state:
            print(param_tensor,"\t",model_state[param_tensor].size())

        parameters_to_prune = []
        for param,_ in model.rnn.named_parameters():
            if "weight" in param:
                parameters_to_prune.append((model.rnn,param))
        prune.global_unstructured(parameters_to_prune,pruning_method=prune.L1Unstructured,amount=0.6)
        for module,param in parameters_to_prune:
            prune.remove(module,param)

        model_state = model.state_dict()
        torch.save(model_state,'pruned.pth') # This file is much larger than the original
        for param_tensor in model_state:
            print(param_tensor,model_state[param_tensor].size())

当我尝试修剪模型中的线性层时,保存的模型不会显示出与修剪 LSTM 层时相同的大小增加。知道是什么原因造成的吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)