未验证 提交 8188d83b 编写于 作者: W wawltor 提交者: GitHub

Fix some sample codes in api2.0,include zeros, pow, mul, ones_like, zeros_like

In those ops, some api has execute run error, fix those op, and format some sample code.
上级 7c3fa5ff
...@@ -207,7 +207,7 @@ def ones(shape, dtype=None, out=None, device=None): ...@@ -207,7 +207,7 @@ def ones(shape, dtype=None, out=None, device=None):
import paddle import paddle
data = paddle.ones(shape=[3, 2], dtype='float32') # [[1., 1.], [1., 1.], [1., 1.]] data = paddle.ones(shape=[3, 2], dtype='float32') # [[1., 1.], [1., 1.], [1., 1.]]
data = paddle.ones(shape=[2, 2], dtype='float32', device='cpu') # [[1., 1.], [1., 0.]] data = paddle.ones(shape=[2, 2], dtype='float32', device='cpu') # [[1., 1.], [1., 1.]]
""" """
check_dtype(dtype, 'create data type', check_dtype(dtype, 'create data type',
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
...@@ -247,7 +247,7 @@ def ones_like(input, dtype=None, device=None, name=None): ...@@ -247,7 +247,7 @@ def ones_like(input, dtype=None, device=None, name=None):
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
x = fluid.layers.data(name='x', dtype='float32', shape=[3], append_batch_size=False) x = fluid.data(name='x', dtype='float32', shape=[3])
data = paddle.ones_like(x) # data=[1.0, 1.0, 1.0] data = paddle.ones_like(x) # data=[1.0, 1.0, 1.0]
data1 = paddle.ones_like(input=x, device="gpu") data1=[1.0, 1.0. 1.0] data1 = paddle.ones_like(input=x, device="gpu") data1=[1.0, 1.0. 1.0]
...@@ -354,9 +354,9 @@ def zeros_like(input, dtype=None, device=None, name=None): ...@@ -354,9 +354,9 @@ def zeros_like(input, dtype=None, device=None, name=None):
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
x = fluid.layers.data(name='x', dtype='float32', shape=[3], append_batch_size=False) x = fluid.data(name='x', dtype='float32', shape=[3])
data = paddle.ones_like(x) # data=[1.0, 1.0, 1.0] data = paddle.ones_like(x) # data=[1.0, 1.0, 1.0]
data1 = paddle.ones_like(input=x, device="gpu") data1=[1.0, 1.0. 1.0] data1 = paddle.ones_like(input=x, device="gpu") #data1=[1.0, 1.0. 1.0]
""" """
......
...@@ -96,6 +96,7 @@ def matmul(x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None): ...@@ -96,6 +96,7 @@ def matmul(x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None):
# x: [M], y: [N] # x: [M], y: [N]
# paddle.matmul(x, y, True, True) # out: [M, N] # paddle.matmul(x, y, True, True) # out: [M, N]
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
x = fluid.data(name='x', shape=[2, 3], dtype='float32') x = fluid.data(name='x', shape=[2, 3], dtype='float32')
y = fluid.data(name='y', shape=[3, 2], dtype='float32') y = fluid.data(name='y', shape=[3, 2], dtype='float32')
......
...@@ -168,18 +168,19 @@ def pow(input, exponent, out=None, name=None): ...@@ -168,18 +168,19 @@ def pow(input, exponent, out=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid
x = paddle.fluid.data(name="x", shape=[32,32], dtype="float32") x = fluid.data(name="x", shape=[32,32], dtype="float32")
# example 1: argument exponent is float # example 1: argument exponent is float
res = paddle.fluid.data(name="output", shape=[32,32], dtype="float32") res = fluid.data(name="output", shape=[32,32], dtype="float32")
y_1 = paddle.pow(x, 2.0, out=res) y_1 = paddle.pow(x, 2.0, out=res)
# y_1 is x^{2.0} # y_1 is x^{2.0}
# example 2: argument exponent is Variable # example 2: argument exponent is Variable
exponet_tensor = fluid.layers.fill_constant([1], "float32", 3.0) exponet_tensor = fluid.layers.fill_constant([1], "float32", 3.0)
res = paddle.fluid.data(name="output", shape=[32,32], dtype="float32") res = fluid.data(name="output", shape=[32,32], dtype="float32")
y_2 = paddle.pow(x, exponent_tensor, out=res) y_2 = paddle.pow(x, exponet_tensor, out=res)
# y_2 is x^{3.0} # y_2 is x^{3.0}
""" """
helper = LayerHelper('pow', **locals()) helper = LayerHelper('pow', **locals())
...@@ -254,10 +255,11 @@ def mul(x, y, x_num_col_dims=1, y_num_col_dims=1, out=None, name=None): ...@@ -254,10 +255,11 @@ def mul(x, y, x_num_col_dims=1, y_num_col_dims=1, out=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
dataX = paddle.fluid.data(name="dataX", append_batch_size = False, shape=[2, 5], dtype="float32") import paddle.fluid as fluid
dataY = paddle.fluid.data(name="dataY", append_batch_size = False, shape=[5, 3], dtype="float32") dataX = fluid.data(name="dataX", shape=[2, 5], dtype="float32")
dataY = fluid.data(name="dataY", shape=[5, 3], dtype="float32")
res = paddle.fluid.data(name="output", append_batch_size = False, shape=[2, 3], dtype="float32") res = fluid.data(name="output", shape=[2, 3], dtype="float32")
output = paddle.mul(dataX, dataY, output = paddle.mul(dataX, dataY,
x_num_col_dims = 1, x_num_col_dims = 1,
y_num_col_dims = 1, y_num_col_dims = 1,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册