From 6da552a22f21452978959a8cca371b082693aec7 Mon Sep 17 00:00:00 2001 From: zhulei <563755780@qq.com> Date: Fri, 9 Oct 2020 15:52:41 +0800 Subject: [PATCH] Update initializer examples of Bilinear (#27709) --- python/paddle/fluid/initializer.py | 41 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/python/paddle/fluid/initializer.py b/python/paddle/fluid/initializer.py index 7a92adf0a89..67c572d4988 100644 --- a/python/paddle/fluid/initializer.py +++ b/python/paddle/fluid/initializer.py @@ -729,31 +729,32 @@ class BilinearInitializer(Initializer): .. code-block:: python - import paddle.fluid as fluid import math + + import paddle + import paddle.nn as nn + from paddle.regularizer import L2Decay + factor = 2 C = 2 B = 8 H = W = 32 - w_attr = fluid.param_attr.ParamAttr( - learning_rate=0., - regularizer=fluid.regularizer.L2Decay(0.), - initializer=fluid.initializer.Bilinear()) - x = fluid.data(name="data", shape=[B, 3, H, W], - dtype="float32") - conv_up = fluid.layers.conv2d_transpose( - input=x, - num_filters=C, - output_size=None, - filter_size=2 * factor - factor % 2, - padding=int(math.ceil((factor - 1) / 2.)), - stride=factor, - groups=C, - param_attr=w_attr, - bias_attr=False) - - Where, `num_filters=C` and `groups=C` means this is channel-wise transposed - convolution. The filter shape will be (C, 1, K, K) where K is `filer_size`, + w_attr = paddle.ParamAttr(learning_rate=0., + regularizer=L2Decay(0.), + initializer=nn.initializer.Bilinear()) + data = paddle.rand([B, 3, H, W], dtype='float32') + conv_up = nn.ConvTranspose2d(3, + out_channels=C, + kernel_size=2 * factor - factor % 2, + padding=int( + math.ceil((factor - 1) / 2.)), + stride=factor, + weight_attr=w_attr, + bias_attr=False) + x = conv_up(data) + + Where, `out_channels=C` and `groups=C` means this is channel-wise transposed + convolution. The filter shape will be (C, 1, K, K) where K is `kernel_size`, This initializer will set a (K, K) interpolation kernel for every channel of the filter identically. The resulting shape of the output feature map will be (B, C, factor * H, factor * W). Note that the learning rate and the -- GitLab