提交 f4630304 编写于 作者: Y yaoxuefeng

update demo code test=develop

上级 ffc147d8
...@@ -26,12 +26,13 @@ Flatten ...@@ -26,12 +26,13 @@ Flatten
.. code-block:: python .. code-block:: python
import paddle import paddle
from paddle.imperative import to_variable from paddle import to_variable
import numpy as np import numpy as np
inp_np = np.ones([5, 2, 3, 4]).astype('float32') inp_np = np.ones([5, 2, 3, 4]).astype('float32')
paddle.enable_imperative() paddle.disable_static()
inp_np = to_variable(inp_np) inp_np = to_variable(inp_np)
flatten = paddle.nn.Flatten(start_axis=1, stop_axis=2) flatten = paddle.nn.Flatten(start_axis=1, stop_axis=2)
flatten_res = flatten(inp_np) flatten_res = flatten(inp_np)
......
...@@ -37,14 +37,19 @@ addmm ...@@ -37,14 +37,19 @@ addmm
import numpy as np import numpy as np
import paddle import paddle
data_x = np.ones((2, 2)).astype(np.float32) data_x = np.ones((2, 2)).astype(np.float32)
data_y = np.ones((2, 2)).astype(np.float32) data_y = np.ones((2, 2)).astype(np.float32)
data_input = np.ones((2, 2)).astype(np.float32) data_input = np.ones((2, 2)).astype(np.float32)
paddle.enable_imperative()
x = paddle.imperative.to_variable(data_x) paddle.disable_static()
y = paddle.imperative.to_variable(data_y)
input = paddle.imperative.to_variable(data_input) x = paddle.to_variable(data_x)
y = paddle.to_variable(data_y)
input = paddle.to_variable(data_input)
out = paddle.tensor.addmm( input=input, x=x, y=y, beta=0.5, alpha=5.0 ) out = paddle.tensor.addmm( input=input, x=x, y=y, beta=0.5, alpha=5.0 )
print( out.numpy() ) print( out.numpy() )
# [[10.5 10.5] # [[10.5 10.5]
# [10.5 10.5]] # [10.5 10.5]]
...@@ -33,14 +33,16 @@ bmm ...@@ -33,14 +33,16 @@ bmm
.. code-block:: python .. code-block:: python
import paddle import paddle
# In imperative mode: # In imperative mode:
# size input1: (2, 2, 3) and input2: (2, 3, 2) # size input1: (2, 2, 3) and input2: (2, 3, 2)
input1 = np.array([[[1.0, 1.0, 1.0],[2.0, 2.0, 2.0]],[[3.0, 3.0, 3.0],[4.0, 4.0, 4.0]]]) input1 = np.array([[[1.0, 1.0, 1.0],[2.0, 2.0, 2.0]],[[3.0, 3.0, 3.0],[4.0, 4.0, 4.0]]])
input2 = np.array([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]],[[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]]) input2 = np.array([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]],[[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]])
paddle.enable_imperative()
paddle.disable_static()
x = paddle.imperative.to_variable(input1) x = paddle.to_variable(input1)
y = paddle.imperative.to_variable(input2) y = paddle.to_variable(input2)
out = paddle.bmm(x, y) out = paddle.bmm(x, y)
#output size: (2, 2, 2) #output size: (2, 2, 2)
#output value: #output value:
......
...@@ -58,12 +58,14 @@ flatten op 根据给定的start_axis 和 stop_axis 将连续的维度展平 ...@@ -58,12 +58,14 @@ flatten op 根据给定的start_axis 和 stop_axis 将连续的维度展平
import paddle import paddle
import numpy as np import numpy as np
paddle.enable_imperative()
paddle.disable_static()
image_shape=(2, 3, 4, 4) image_shape=(2, 3, 4, 4)
x = np.arange(image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3]).reshape(image_shape) / 100. x = np.arange(image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3]).reshape(image_shape) / 100.
x = x.astype('float32') x = x.astype('float32')
img = paddle.imperative.to_variable(x) img = paddle.to_variable(x)
out = paddle.flatten(img, start_axis=1, stop_axis=2) out = paddle.flatten(img, start_axis=1, stop_axis=2)
# out shape is [2, 12, 4] # out shape is [2, 12, 4]
......
...@@ -28,22 +28,27 @@ tril ...@@ -28,22 +28,27 @@ tril
import numpy as np import numpy as np
import paddle import paddle
data = np.arange(1, 13, dtype="int64").reshape(3,-1) data = np.arange(1, 13, dtype="int64").reshape(3,-1)
# array([[ 1, 2, 3, 4], # array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8], # [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]]) # [ 9, 10, 11, 12]])
paddle.enable_imperative()
x = paddle.imperative.to_variable(data) paddle.disable_static()
x = paddle.to_variable(data)
tril1 = paddle.tensor.tril(x) tril1 = paddle.tensor.tril(x)
# array([[ 1, 0, 0, 0], # array([[ 1, 0, 0, 0],
# [ 5, 6, 0, 0], # [ 5, 6, 0, 0],
# [ 9, 10, 11, 0]]) # [ 9, 10, 11, 0]])
# example 2, positive diagonal value # example 2, positive diagonal value
tril2 = paddle.tensor.tril(x, diagonal=2) tril2 = paddle.tensor.tril(x, diagonal=2)
# array([[ 1, 2, 3, 0], # array([[ 1, 2, 3, 0],
# [ 5, 6, 7, 8], # [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]]) # [ 9, 10, 11, 12]])
# example 3, negative diagonal value # example 3, negative diagonal value
tril3 = paddle.tensor.tril(x, diagonal=-1) tril3 = paddle.tensor.tril(x, diagonal=-1)
# array([[ 0, 0, 0, 0], # array([[ 0, 0, 0, 0],
......
...@@ -29,22 +29,27 @@ triu ...@@ -29,22 +29,27 @@ triu
import numpy as np import numpy as np
import paddle import paddle
data = np.arange(1, 13, dtype="int64").reshape(3,-1) data = np.arange(1, 13, dtype="int64").reshape(3,-1)
# array([[ 1, 2, 3, 4], # array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8], # [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]]) # [ 9, 10, 11, 12]])
paddle.enable_imperative()
paddle.disable_static()
# example 1, default diagonal # example 1, default diagonal
x = paddle.imperative.to_variable(data) x = paddle.to_variable(data)
triu1 = paddle.tensor.triu(x) triu1 = paddle.tensor.triu(x)
# array([[ 1, 2, 3, 4], # array([[ 1, 2, 3, 4],
# [ 0, 6, 7, 8], # [ 0, 6, 7, 8],
# [ 0, 0, 11, 12]]) # [ 0, 0, 11, 12]])
# example 2, positive diagonal value # example 2, positive diagonal value
triu2 = paddle.tensor.triu(x, diagonal=2) triu2 = paddle.tensor.triu(x, diagonal=2)
# array([[0, 0, 3, 4], # array([[0, 0, 3, 4],
# [0, 0, 0, 8], # [0, 0, 0, 8],
# [0, 0, 0, 0]]) # [0, 0, 0, 0]])
# example 3, negative diagonal value # example 3, negative diagonal value
triu3 = paddle.tensor.triu(x, diagonal=-1) triu3 = paddle.tensor.triu(x, diagonal=-1)
# array([[ 1, 2, 3, 4], # array([[ 1, 2, 3, 4],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册