Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add alpha_dropout in nn.functional and nn.layer, test=develop #26365

Merged
merged 9 commits into from
Aug 23, 2020

Conversation

huangjun12
Copy link
Contributor

PR types

New features

PR changes

APIs

Describe

add apis of alpha_dropout:
(1) paddle.nn.functional.alpha_dropout(x, p=0.5, training=True, name = None)
(2) paddle.nn.AlphaDropout(p=0.5, name = None)

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

keep_mask)

#apply mask
alpha_p = layers.fill_constant(shape=[1], dtype=dtype, value=alpha_p)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些 tensor * scalar 的情形其实用 scale 可能会更快。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


class AlphaDropout(layers.Layer):
"""
:alias_main: paddle.nn.AlphaDropout

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alis 可以不用写了。由官网解析自动处理

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

[Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515)
`paddle.nn.functional.alpha_dropout`

In dygraph mode, please use ``eval()`` to indicate whether it is in test phrase or not.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to switch to evaluation mode, where dropout is disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

iclementine
iclementine previously approved these changes Aug 19, 2020
Copy link

@iclementine iclementine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -456,6 +460,80 @@ def _is_list_or_turple_(data):
return out


def alpha_dropout(x, p=0.5, training=True, name=None):
"""
alpha_dropout function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

x (Tensor): The input tensor. The data type is float32 or float64.
p (float): Probability of setting units to zero. Default 0.5.
training (bool): A flag indicating whether it is in train phrase or not. Default True.
name (str|None): A name for this layer(optional). If set None, the layer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use template phrase for name.

name (str|None): A name for this layer(optional). If set None, the layer
will be named automatically.
Returns:
A Tensor representing the dropout, has same shape and data type with `x`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with -> as

x = np.array([[-1, 1], [-1, 1]]).astype('float32')
x = paddle.to_tensor(x)
y_train = paddle.nn.functional.alpha_dropout(x, 0.5)
y_test = paddle.nn.functional.alpha_dropout(x, 0.5, training=False) #test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove #test

# [[-0.10721093, 1.6655989 ], [-0.7791938, -0.7791938]] (randomly)
print(y_test.numpy())
"""
if not isinstance(p, (float, int)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring only says float

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

def __init__(self, p=0.5, name=None):
super(AlphaDropout, self).__init__()
self.p = p
self.training = _dygraph_tracer()._train_mode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layer now has train()/eval(), this can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@huangjun12
Copy link
Contributor Author

add alpha_dropout and AlphaDropout;
following #26111
(1) refine doc of dropout series
(2) remove _dygraph_tracer()._train_mode in Dropout and Dropout3D

@@ -80,42 +80,34 @@ def interpolate(input,
'trilinear' : Trilinear interpolation
'nearest' : Nearest neighbor interpolation
'bicubic' : Bicubic interpolation
Linear interpolation is the method of using a line connecting two known quantities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to this PR?

For details of linear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Linear_interpolation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto formatting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

[4 0 6]]
(3) What about ``axis=[0, 1]`` ? This means the dropout is performed in all axes of x,
which is the same case as default setting ``axis=None`` .
(4) You may note that logically `axis=None` means the dropout is performed in no axis of x,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no axis -> all axis

When x is a 4d tensor with shape `NCHW`, we can set ``axis=[0,1]`` and the dropout will be performed
in channel `N` and `C`, `H` and `W` is tied, i.e.
paddle.nn.dropout(x, p, axis=[0,1])
This is something we called dropout2d. Please refer to ``paddle.nn.functional.dropout2d``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove This is something we called dropout2d.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@willthefrog willthefrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@willthefrog willthefrog merged commit 7bd7b18 into PaddlePaddle:develop Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants