"""The pruner used to prune channels of convolution.
Args:
criterion(str): the criterion used to sort channels for pruning.
It only supports 'l1_norm' currently.
criterion(str): the criterion used to sort channels for pruning. It only supports 'l1_norm' currently.
"""
def__init__(self,criterion="l1_norm"):
self.criterion=criterion
defprune(self,
...
...
@@ -44,9 +46,10 @@ class Pruner():
only_graph=False,
param_backup=False,
param_shape_backup=False):
"""
Pruning the given parameters.
"""Pruning the given parameters.
Args:
program(fluid.Program): The program to be pruned.
scope(fluid.Scope): The scope storing paramaters to be pruned.
params(list<str>): A list of parameter names to be pruned.
...
...
@@ -58,10 +61,9 @@ class Pruner():
False means modifying graph and variables in scope. Default: False.
param_backup(bool): Whether to return a dict to backup the values of parameters. Default: False.
param_shape_backup(bool): Whether to return a dict to backup the shapes of parameters. Default: False.
Returns:
Program: The pruned program.
param_backup: A dict to backup the values of parameters.
param_shape_backup: A dict to backup the shapes of parameters.
tuple: ``(pruned_program, param_backup, param_shape_backup)``. ``pruned_program`` is the pruned program. ``param_backup`` is a dict to backup the values of parameters. ``param_shape_backup`` is a dict to backup the shapes of parameters.
"""
self.pruned_list=[]
...
...
@@ -131,6 +133,7 @@ class Pruner():
def_cal_pruned_idx(self,param,ratio,axis):
"""
Calculate the index to be pruned on axis by given pruning ratio.
Args:
name(str): The name of parameter to be pruned.
param(np.array): The data of parameter to be pruned.
...
...
@@ -138,6 +141,7 @@ class Pruner():
axis(int): The axis to be used for pruning given parameter.
If it is None, the value in self.pruning_axis will be used.
"""Compute the sensitivities of convolutions in a model. The sensitivity of a convolution is the losses of accuracy on test dataset in differenct pruned ratios. The sensitivities can be used to get a group of best ratios with some condition.
This function return a dict storing sensitivities as below:
.. code-block:: python
{"weight_0":
{0.1: 0.22,
0.2: 0.33
},
"weight_1":
{0.1: 0.21,
0.2: 0.4
}
}
``weight_0`` is parameter name of convolution. ``sensitivities['weight_0']`` is a dict in which key is pruned ratio and value is the percent of losses.
Args:
program(paddle.fluid.Program): The program to be analysised.
place(fluid.CPUPlace | fluid.CUDAPlace): The device place of filter parameters.
param_names(list): The parameter names of convolutions to be analysised.
eval_func(function): The callback function used to evaluate the model. It should accept a instance of `paddle.fluid.Program` as argument and return a score on test dataset.
sensitivities_file(str): The file to save the sensitivities. It will append the latest computed sensitivities into the file. And the sensitivities in the file would not be computed again. This file can be loaded by `pickle` library.
pruned_ratios(list): The ratios to be pruned. default: ``[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]``.