Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2057df7a
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2057df7a
编写于
12月 06, 2019
作者:
F
Feiyu Chan
提交者:
lanxianghit
12月 06, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add fluid.layers.gelu & doc (#21515)
Add a python interface for Gelu. Add documentation for fluid.layers.gelu.
上级
29c38445
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
133 addition
and
0 deletion
+133
-0
python/paddle/fluid/layers/ops.py
python/paddle/fluid/layers/ops.py
+79
-0
python/paddle/fluid/tests/unittests/test_gelu_op.py
python/paddle/fluid/tests/unittests/test_gelu_op.py
+54
-0
未找到文件。
python/paddle/fluid/layers/ops.py
浏览文件 @
2057df7a
...
...
@@ -239,3 +239,82 @@ Examples:
# array([[ 0.21134382, -0. , 0.32876605],
# [-0. , -0. , 1.0013918 ]], dtype=float32)
"""
__all__
+=
[
'gelu'
]
_gelu_
=
generate_layer_fn
(
'gelu'
)
def
gelu
(
x
):
locals_var
=
locals
().
copy
()
kwargs
=
dict
()
for
name
,
val
in
locals_var
.
items
():
if
val
is
not
None
:
kwargs
[
name
]
=
val
return
_gelu_
(
**
kwargs
)
gelu
.
__doc__
=
"""
:strong:`GeLU Activation Operator`
For more details, see [Gaussian Error Linear Units](https://arxiv.org/abs/1606.08415).
Equation:
.. math::
out = 0.5 * x * (1 + erf(
\\
frac{x}{
\\
sqrt{2}}))
Args:
x(Variable): The input of GeLU op, Tensor or LoDTensor, dtype: float32 or float64.
Returns:
Variable: The output of GeLU op, Tensor or LoDTensor, dtype: float32 or float64, the same as the input, shape: the same as the input.
Examples:
.. code-block:: python
# declarative mode
import numpy as np
from paddle import fluid
x = fluid.data(name="x", shape=(-1, 3), dtype="float32")
y = fluid.layers.gelu(x)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
start = fluid.default_startup_program()
main = fluid.default_main_program()
data = np.random.randn(2, 3).astype("float32")
exe.run(start)
y_np, = exe.run(main, feed={"x": data}, fetch_list=[y])
data
# array([[ 0.87165993, -1.0541513 , -0.37214822],
# [ 0.15647964, 0.32496083, 0.33045998]], dtype=float32)
y_np
# array([[ 0.70456535, -0.15380788, -0.13207214],
# [ 0.08796856, 0.20387867, 0.2080159 ]], dtype=float32)
.. code-block:: python
# imperative mode
import numpy as np
from paddle import fluid
import paddle.fluid.dygraph as dg
data = np.random.randn(2, 3).astype("float32")
place = fluid.CPUPlace()
with dg.guard(place) as g:
x = dg.to_variable(data)
y = fluid.layers.gelu(x)
y_np = y.numpy()
data
# array([[ 0.87165993, -1.0541513 , -0.37214822],
# [ 0.15647964, 0.32496083, 0.33045998]], dtype=float32)
y_np
# array([[ 0.70456535, -0.15380788, -0.13207214],
# [ 0.08796856, 0.20387867, 0.2080159 ]], dtype=float32)
"""
python/paddle/fluid/tests/unittests/test_gelu_op.py
0 → 100644
浏览文件 @
2057df7a
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
unittest
import
numpy
as
np
from
scipy.special
import
erf
import
paddle.fluid
as
fluid
import
paddle.fluid.dygraph
as
dg
class
TestGeluOp
(
unittest
.
TestCase
):
def
_test_case1_cpu
(
self
):
x
=
np
.
random
.
uniform
(
-
1
,
1
,
size
=
(
11
,
17
)).
astype
(
np
.
float32
)
y_ref
=
0.5
*
x
*
(
1
+
erf
(
x
/
np
.
sqrt
(
2
)))
place
=
fluid
.
CPUPlace
()
with
dg
.
guard
(
place
)
as
g
:
x_var
=
dg
.
to_variable
(
x
)
y_var
=
fluid
.
layers
.
gelu
(
x_var
)
y_test
=
y_var
.
numpy
()
self
.
assertTrue
(
np
.
allclose
(
y_ref
,
y_test
,
rtol
=
1e-05
,
atol
=
1e-08
))
def
_test_case1_gpu
(
self
):
x
=
np
.
random
.
uniform
(
-
1
,
1
,
size
=
(
11
,
17
)).
astype
(
np
.
float32
)
y_ref
=
0.5
*
x
*
(
1
+
erf
(
x
/
np
.
sqrt
(
2
)))
place
=
fluid
.
CUDAPlace
(
0
)
with
dg
.
guard
(
place
)
as
g
:
x_var
=
dg
.
to_variable
(
x
)
y_var
=
fluid
.
layers
.
gelu
(
x_var
)
y_test
=
y_var
.
numpy
()
self
.
assertTrue
(
np
.
allclose
(
y_ref
,
y_test
,
rtol
=
1e-05
,
atol
=
1e-08
))
def
test_cases
(
self
):
self
.
_test_case1_cpu
()
if
fluid
.
is_compiled_with_cuda
():
self
.
_test_case1_gpu
()
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录