[ 仅 API 调用方式不一致 ]torch.nn.Module.apply

torch.nn.Module.apply

torch.nn.Module.apply(fn)

paddle.nn.Layer.apply

paddle.nn.Layer.apply(fn)

两者功能一致,但调用方式不一致,具体如下:

转写示例

# PyTorch 写法
def init_weights(m):
    pass
net = torch.nn.Sequential(
    torch.nn.Linear(2, 2, bias=False), torch.nn.Linear(2, 2, bias=False)
)
net.apply(init_weights)

# Paddle 写法
def init_weights(m):
    pass
net = paddle.nn.Sequential(
    paddle.nn.Linear(in_features=2, out_features=2, bias_attr=False),
    paddle.nn.Linear(in_features=2, out_features=2, bias_attr=False),
)
net.apply(init_weights)