Dirac

class paddle.nn.initializer. Dirac ( groups=1, name=None ) [source]

Initialize the 3D/4D/5D Tensor with Dirac delta function.

It can reserve the feature of convolution layer input, which means that as many channels are reserved as possible.

In this initialize method, elements in the middle of convolution kernels will be set to 1 . The formula can be described as follow.

\[X[d, d, shape[2]//2, shape[3]//2, ...]=1, \ d=0,1...N\]

where, N is the minimum value of in_channels and out_channels

Parameters
  • groups (int, optional) – 0-dimension of the Tensor will be divided by groups, each group has the same value. Default: 1.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

Dirac initializer instance objects.

Examples

>>> import paddle

>>> # 1. For kernel_size is uneven number:
>>> attr = paddle.ParamAttr(initializer=paddle.nn.initializer.Dirac())
>>> conv = paddle.nn.Conv1D(3, 2, 3, weight_attr=attr)
>>> print(conv.weight)
Parameter containing:
Tensor(shape=[2, 3, 3], dtype=float32, place=CPUPlace, stop_gradient=False,
[[[0., 1., 0.],
  [0., 0., 0.],
  [0., 0., 0.]],
 [[0., 0., 0.],
  [0., 1., 0.],
  [0., 0., 0.]]])
>>> input = paddle.rand([8, 3, 10])
>>> output = conv(input)
>>> output == input[:, 0:2, 1:9]
>>> print(output.shape)
[8, 2, 8]
>>> # It means output is almost the same with input, 2 channels are reserved

>>> # 2. For kernel_size is even number:
>>> attr = paddle.ParamAttr(initializer=paddle.nn.initializer.Dirac())
>>> conv = paddle.nn.Conv1D(3, 2, 4, weight_attr=attr)
>>> print(conv.weight)
Parameter containing:
Tensor(shape=[2, 3, 4], dtype=float32, place=CPUPlace, stop_gradient=False,
[[[0., 0., 1., 0.],
  [0., 0., 0., 0.],
  [0., 0., 0., 0.]],
 [[0., 0., 0., 0.],
  [0., 0., 1., 0.],
  [0., 0., 0., 0.]]])