[英]Pytorch Global Pruning is not reducing the size of the model
我正在尝试通过全局修剪修剪我的深度学习 model。 原始的未修剪 model 约为 77.5 MB。 但是修剪后,当我保存 model 时,model 的大小与原始大小相同。 谁能帮我解决这个问题?
以下是修剪代码:-
import torch.nn.utils.prune as prune
parameters_to_prune = (
(model.encoder[0], ‘weight’),
(model.up_conv1[0], ‘weight’),
(model.up_conv2[0], ‘weight’),
(model.up_conv3[0], ‘weight’),
)
print(parameters_to_prune)
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.L1Unstructured,
amount=0.2,
)
print(
“Sparsity in Encoder.weight: {:.2f}%”.format(
100. * float(torch.sum(model.encoder[0].weight == 0))
/ float(model.encoder[0].weight.nelement())
)
)
print(
“Sparsity in up_conv1.weight: {:.2f}%”.format(
100. * float(torch.sum(model.up_conv1[0].weight == 0))
/ float(model.up_conv1[0].weight.nelement())
)
)
print(
“Sparsity in up_conv2.weight: {:.2f}%”.format(
100. * float(torch.sum(model.up_conv2[0].weight == 0))
/ float(model.up_conv2[0].weight.nelement())
)
)
print(
“Sparsity in up_conv3.weight: {:.2f}%”.format(
100. * float(torch.sum(model.up_conv3[0].weight == 0))
/ float(model.up_conv3[0].weight.nelement())
)
)
print(
“Global sparsity: {:.2f}%”.format(
100. * float(
torch.sum(model.encoder[0].weight == 0)
+ torch.sum(model.up_conv1[0].weight == 0)
+ torch.sum(model.up_conv2[0].weight == 0)
+ torch.sum(model.up_conv3[0].weight == 0)
)
/ float(
model.encoder[0].weight.nelement()
+ model.up_conv1[0].weight.nelement()
+ model.up_conv2[0].weight.nelement()
+ model.up_conv3[0].weight.nelement()
)
)
)
**Setting Pruning to Permanent**
prune.remove(model.encoder[0], “weight”)
prune.remove(model.up_conv1[0], “weight”)
prune.remove(model.up_conv2[0], “weight”)
prune.remove(model.up_conv3[0], “weight”)
**Saving the model**
PATH = “C:\PrunedNet.pt”
torch.save(model.state_dict(), PATH)
如果像这样应用,修剪不会改变 model 大小。
如果你有张量,可以这样说:
[1., 2., 3., 4., 5., 6., 7., 8.]
你修剪了50%
的数据,例如:
[1., 2., 0., 4., 0., 6., 0., 0.]
您仍然会有8
个浮点值,并且它们的大小相同。
Conv2d
,如果它的权重为零或可忽略不计,则可以将其删除)否则它将无法正常工作。 查看一些相关项目,这些项目可以让您无需自己编写代码就可以完成,例如Torch-Pruning 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.