|
|
|
@ -1898,8 +1898,12 @@ class PRelu(layers.Layer):
|
|
|
|
|
and element. all: all elements share same weight
|
|
|
|
|
channel:elements in a channel share same weight
|
|
|
|
|
element:each element has a weight
|
|
|
|
|
channel (int, optional): The number of channels.
|
|
|
|
|
This argument is required when mode is "channel".
|
|
|
|
|
Default: None.
|
|
|
|
|
input_shape (list or tuple, optional): The shape of input.
|
|
|
|
|
This parameter is required when mode is not "all". Default: None.
|
|
|
|
|
This argument is required when mode is "element".
|
|
|
|
|
Default: None.
|
|
|
|
|
param_attr(ParamAttr, optional): The parameter attribute for the learnable
|
|
|
|
|
weight (alpha). Default: None.
|
|
|
|
|
dtype (str, optional): Data type, it can be "float32" or "float64". Default: "float32".
|
|
|
|
@ -1921,31 +1925,47 @@ class PRelu(layers.Layer):
|
|
|
|
|
inp_np = np.ones([5, 200, 100, 100]).astype('float32')
|
|
|
|
|
with fluid.dygraph.guard():
|
|
|
|
|
inp_np = to_variable(inp_np)
|
|
|
|
|
mode = 'channel'
|
|
|
|
|
prelu = fluid.PRelu(
|
|
|
|
|
mode=mode,
|
|
|
|
|
prelu0 = fluid.PRelu(
|
|
|
|
|
mode='all',
|
|
|
|
|
param_attr=fluid.ParamAttr(initializer=fluid.initializer.Constant(1.0)))
|
|
|
|
|
dy_rlt0 = prelu0(inp_np)
|
|
|
|
|
prelu1 = fluid.PRelu(
|
|
|
|
|
mode='channel',
|
|
|
|
|
channel=200,
|
|
|
|
|
param_attr=fluid.ParamAttr(initializer=fluid.initializer.Constant(1.0)))
|
|
|
|
|
dy_rlt1 = prelu1(inp_np)
|
|
|
|
|
prelu2 = fluid.PRelu(
|
|
|
|
|
mode='element',
|
|
|
|
|
input_shape=inp_np.shape,
|
|
|
|
|
param_attr=fluid.ParamAttr(initializer=fluid.initializer.Constant(1.0)))
|
|
|
|
|
dy_rlt = prelu(inp_np)
|
|
|
|
|
dy_rlt2 = prelu2(inp_np)
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, mode, input_shape=None, param_attr=None,
|
|
|
|
|
def __init__(self,
|
|
|
|
|
mode,
|
|
|
|
|
channel=None,
|
|
|
|
|
input_shape=None,
|
|
|
|
|
param_attr=None,
|
|
|
|
|
dtype='float32'):
|
|
|
|
|
super(PRelu, self).__init__()
|
|
|
|
|
self._mode = mode
|
|
|
|
|
self._param_attr = param_attr
|
|
|
|
|
if self._mode not in ['all', 'channel', 'element']:
|
|
|
|
|
raise ValueError('mode should be one of all, channel, element.')
|
|
|
|
|
self._dtype = dtype
|
|
|
|
|
self._alpha_shape = [1]
|
|
|
|
|
if mode is not 'all':
|
|
|
|
|
assert input_shape is not None
|
|
|
|
|
input_shape = list(input_shape)
|
|
|
|
|
if self._mode == 'channel':
|
|
|
|
|
self._alpha_shape = [1, input_shape[1], 1, 1]
|
|
|
|
|
elif self._mode == 'element':
|
|
|
|
|
self._alpha_shape = input_shape
|
|
|
|
|
if mode == 'all':
|
|
|
|
|
self._alpha_shape = [1]
|
|
|
|
|
elif mode == 'channel':
|
|
|
|
|
assert isinstance(
|
|
|
|
|
channel,
|
|
|
|
|
int), "channel argument is required when mode is 'channel'."
|
|
|
|
|
self._alpha_shape = [1, channel, 1, 1]
|
|
|
|
|
elif mode == 'element':
|
|
|
|
|
assert isinstance(input_shape, (
|
|
|
|
|
list, tuple
|
|
|
|
|
)), "input_shape argument is required when mode is 'element'."
|
|
|
|
|
self._alpha_shape = [1] + list(input_shape)[1:]
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError('mode should be one of all, channel, element.')
|
|
|
|
|
self.weight = self.create_parameter(
|
|
|
|
|
attr=self._param_attr,
|
|
|
|
|
shape=self._alpha_shape,
|
|
|
|
|