reset_excluded_layers

paddle.incubate.asp. reset_excluded_layers ( main_program=None ) [源代码]

重置与 main_program 对应的 excluded_layers 设置。如果 main_program 为 None,则 excepted_layers 的所有配置都将被清除。

参数

  • main_program (Program,可选) - 包含模型定义及其参数的 Program。如果为 None,那么这个函数将重置所有 exclusion_layers。 默认为 None。

代码示例

  1. 动态图模式

 >>> # 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()])
 >>> # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow.
 >>> # Please note, reset_excluded_layers also must be called before calling asp.decorate().
 >>> paddle.incubate.asp.reset_excluded_layers()

 >>> optimizer = paddle.incubate.asp.decorate(optimizer)
  1. 静态图模式

 >>> # 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)
 ...     # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow.
 ...     # Please note, reset_excluded_layers also must be called before calling optimizer.minimize().
 ...     paddle.incubate.asp.reset_excluded_layers(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)