提交 3b861a43 编写于 作者: F Feiyu Chan 提交者: Yibing Liu

update example code, use fluid.data swish, gaussian_random, thresholded_relu,...

update example code, use fluid.data swish, gaussian_random, thresholded_relu, test=document_preview (#1487)

* fix example for thresholded relu, test=document_preview

* update example code for swish, thresholded_relu, gaussian_random in chinese doc, test=document_preview

* fix example code for thresholded_relu, test=document_preview

* fix ref for name, test=document_preview
上级 6149bad0
......@@ -5,18 +5,18 @@ gaussian_random
.. py:function:: paddle.fluid.layers.gaussian_random(shape, mean=0.0, std=1.0, seed=0, dtype='float32')
生成数据符合高斯随机分布的张量
生成数据符合高斯随机分布的 Tensor
参数:
- **shape** (Tuple[int] | List[int])- 生成张量的形状。
- **mean** (float)- 随机张量的均值,默认值为 0.0。
- **std** (float)- 随机张量的标准差,默认值为 1.0。
- **seed** (int)- 随机数种子,默认值为0。注:seed 设置为 0 表示使用系统的随机数种子。注意如果 seed 不为 0,则此算子每次将始终生成相同的随机数。
- **dtype** (np.dtype,core.VarDesc.VarType,str)- 输出张量的数据类型,可选值为 float32,float64。
- **shape** (Tuple[int] | List[int])- 生成 Tensor 的形状。
- **mean** (float)- 随机 Tensor 的均值,默认值为 0.0。
- **std** (float)- 随机 Tensor 的标准差,默认值为 1.0。
- **seed** (int)- 随机数种子,默认值为 0。注:seed 设置为 0 表示使用系统的随机数种子。注意如果 seed 不为 0,则此算子每次将始终生成相同的随机数。
- **dtype** (np.dtype | core.VarDesc.VarType | str)- 输出 Tensor 的数据类型,可选值为 float32,float64。
返回:
- 符合高斯分布的随机张量。形状为 shape,数据类型为 dtype。
- 符合高斯分布的随机 Tensor。形状为 shape,数据类型为 dtype。
返回类型:
......@@ -40,8 +40,8 @@ gaussian_random
exe.run(start)
x_np, = exe.run(main, feed={}, fetch_list=[x])
x_np
x_np
# array([[2.3060477, 2.676496 , 3.9911983],
# [0.9990833, 2.8675377, 2.2279181]], dtype=float32)
......@@ -53,11 +53,10 @@ gaussian_random
from paddle import fluid
import paddle.fluid.dygraph as dg
place = fluid.CPUPlace()
place = fluid.CPUPlace()
with dg.guard(place) as g:
x = fluid.layers.gaussian_random((2, 4), mean=2., dtype="float32", seed=10)
x_np = x.numpy()
x_np = x.numpy()
x_np
# array([[2.3060477 , 2.676496 , 3.9911983 , 0.9990833 ],
# [2.8675377 , 2.2279181 , 0.79029655, 2.8447366 ]], dtype=float32)
......
......@@ -11,12 +11,12 @@ swish
out = \frac{x}{1 + e^{- beta * x}}
参数:
- **x** (Variable) - 多维 Tensor,数据类型为 float32,float64。
- **x** (Variable) - 多维 Tensor 或 LoDTensor,数据类型为 float32,float64。
- **beta** (float) - Swish operator 的常量 beta,默认值为 1.0。
- **name** (str,可选) – 具体用法请参见 :ref:`cn_api_fluid_ParamAttr` ,一般无需设置,默认值为None。
- **name** (str,可选) – 具体用法请参见 :ref:`api_guide_Name` ,一般无需设置,默认值为None。
返回:
- Swish op 的结果,多维 Tensor。数据类型为 float32 或 float64 的 Tensor,数据类型以及形状和输入 x 一致。
- Swish op 的结果,多维 Tensor 或 LoDTensor。数据类型为 float32 或 float64,数据类型以及形状和输入 x 一致。
返回类型:
- Variable
......@@ -26,46 +26,47 @@ swish
.. code-block:: python
# 静态图使用
import numpy as np
from paddle import fluid
x = fluid.layers.data(name="x", shape=(3,), dtype="float32")
y = fluid.layers.swish(x, beta=2.0)
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([[-1.1239197 , 1.3391294 , 0.03921051],
# [ 1.1970421 , 0.02440812, 1.2055548 ]], dtype=float32)
y_np
# array([[-0.2756806 , 1.0610548 , 0.01998957],
# [ 0.9193261 , 0.01235299, 0.9276883 ]], dtype=float32)
# 静态图使用
import numpy as np
from paddle import fluid
x = fluid.data(name="x", shape=(-1, 3), dtype="float32")
y = fluid.layers.swish(x, beta=2.0)
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([[-1.1239197 , 1.3391294 , 0.03921051],
# [ 1.1970421 , 0.02440812, 1.2055548 ]], dtype=float32)
y_np
# array([[-0.2756806 , 1.0610548 , 0.01998957],
# [ 0.9193261 , 0.01235299, 0.9276883 ]], dtype=float32)
.. code-block:: python
# 动态图使用
import numpy as np
from paddle import fluid
import paddle.fluid.dygraph as dg
data = np.random.randn(2, 3).astype("float32")
with dg.guard(place) as g:
x = dg.to_variable(data)
y = fluid.layers.swish(x)
y_np = y.numpy()
data
# array([[-0.0816701 , 1.1603649 , -0.88325626],
# [ 0.7522361 , 1.0978601 , 0.12987892]], dtype=float32)
y_np
# array([[-0.03916847, 0.8835007 , -0.25835553],
# [ 0.51126915, 0.82324016, 0.06915068]], dtype=float32)
# 动态图使用
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.swish(x)
y_np = y.numpy()
data
# array([[-0.0816701 , 1.1603649 , -0.88325626],
# [ 0.7522361 , 1.0978601 , 0.12987892]], dtype=float32)
y_np
# array([[-0.03916847, 0.8835007 , -0.25835553],
# [ 0.51126915, 0.82324016, 0.06915068]], dtype=float32)
......@@ -15,11 +15,11 @@ thresholded_relu
\end{matrix}\right.
参数:
- **x** (Variable) -ThresholdedRelu 激活函数的输入,多维 Tensor,数据类型为 float32,float64。
- **x** (Variable) -ThresholdedRelu Op 的输入,多维 Tensor 或 LoDTensor,数据类型为 float32,float64。
- **threshold** (float,可选)-激活函数的 threshold 值,如 threshold 值为 None,则其值为 1.0。
返回:
- 多维 Tensor, 数据类型为 float32 或 float64, 和输入 x 的数据类型相同,形状和输入 x 相同。
- 多维 Tensor 或 LoDTensor, 数据类型为 float32 或 float64, 和输入 x 的数据类型相同,形状和输入 x 相同。
返回类型:
- Variable
......@@ -28,40 +28,39 @@ thresholded_relu
.. code-block:: python
# 静态图使用
import numpy as np
from paddle import fluid
x = fluid.data(name="x", shape=(-1, 3), dtype="float32")
y = fluid.layers.thresholded_relu(x, threshold=0.1)
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.21134382, -1.1805999 , 0.32876605],
# [-1.2210793 , -0.7365624 , 1.0013918 ]], dtype=float32)
y_np
# array([[ 0.21134382, -0. , 0.32876605],
# [-0. , -0. , 1.0013918 ]], dtype=float32)
# 静态图使用
import numpy as np
from paddle import fluid
x = fluid.layers.data(name="x", shape=(3,), dtype="float32")
y = fluid.layers.thresholded_relu(x, threshold=0.1)
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([[ 1.2734995 , 1.4534163 , 1.1058378 ],
# [ 0.84823716, 0.03892502, -0.80151445]], dtype=float32)
y_np
# array([[ 1.2734995 , 1.4534163 , 1.1058378 ],
# [ 0.84823716, 0. , -0. ]], dtype=float32)
.. code-block:: python
# 动态图使用
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()
place = fluid.CPUPlace()
with dg.guard(place) as g:
x = dg.to_variable(data)
y = fluid.layers.thresholded_relu(x, threshold=0.1)
......@@ -72,12 +71,3 @@ thresholded_relu
y_np
# array([[ 0.21134382, -0. , 0.32876605],
# [-0. , -0. , 1.0013918 ]], dtype=float32)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册