set_excluded_layers

paddle.incubate.asp. set_excluded_layers ( param_names, main_program=None ) [source]

Set parameter name of layers which would not be pruned as sparse weights.

Parameters
  • param_names (list of string) – A list contains names of parameters.

  • main_program (Program, optional) – Program with model definition and its parameters. If None is given, then it would be set as `paddle.static.default_main_program(). Default is None.

Examples

>>> # Example1: Usage of Dynamic Graph
>>> import paddle

>>> class MyLayer(paddle.nn.Layer):
...     def __init__(self):
...         super().__init__()
...         self.conv1 = paddle.nn.Conv2D(
...             in_channels=3, out_channels=4, kernel_size=3, padding=2)
...         self.linear1 = paddle.nn.Linear(4624, 100)
...
...     def forward(self, img):
...         hidden = self.conv1(img)
...         hidden = paddle.flatten(hidden, start_axis=1)
...         prediction = self.linear1(hidden)
...         return prediction

>>> my_layer = MyLayer()
>>> optimizer = paddle.optimizer.SGD(
...     learning_rate=0.01, parameters=my_layer.parameters())

>>> # Need to set excluded layers before calling decorate
>>> paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()])

>>> optimizer = paddle.incubate.asp.decorate(optimizer)
>>> # Example2: Usage of Static Graph
>>> import paddle

>>> paddle.enable_static()

>>> class MyLayer(paddle.nn.Layer):
...     def __init__(self):
...         super().__init__()
...         self.conv1 = paddle.nn.Conv2D(
...             in_channels=3, out_channels=4, kernel_size=3, padding=2)
...         self.linear1 = paddle.nn.Linear(4624, 100)
...
...     def forward(self, img):
...         hidden = self.conv1(img)
...         hidden = paddle.flatten(hidden, start_axis=1)
...         prediction = self.linear1(hidden)
...         return prediction

>>> main_program = paddle.static.Program()
>>> startup_program = paddle.static.Program()

>>> with paddle.static.program_guard(main_program, startup_program):
...     input_data = paddle.static.data(name='data', shape=[None, 3, 224, 224])
...     label = paddle.static.data(name='label', shape=[None, 100])
...     my_layer = MyLayer()
...     prob = my_layer(input_data)
...     loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label))
...
...     # Setup excluded layers out from ASP workflow.
...     # Please note, excluded_layers must be set before calling optimizer.minimize().
...     paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()], main_program)
...
...     optimizer = paddle.optimizer.SGD(learning_rate=0.1)
...     optimizer = paddle.static.amp.decorate(optimizer )
...     # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which
...     # will insert necessary masking operations for ASP workflow.
...     optimizer = paddle.incubate.asp.decorate(optimizer)
...     optimizer.minimize(loss, startup_program)