set_excluded_layers¶
设置不会被裁剪为稀疏权重的 layer 的参数名称。
参数¶
param_names (list of string) - 包含参数名的列表
main_program (Program,可选) - 包含模型定义及其参数的 Program。如果为 None,那么它将被设置为 paddle.static.default_main_program()。默认为 None。
代码示例¶
动态图模式
>>> # 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)