Skip to content

Commit

Permalink
fixed imperative module in doc example code (#26149)
Browse files Browse the repository at this point in the history
* fixed imperative module in doc example code

* fixed static module

* solve conflict
  • Loading branch information
pangyoki committed Aug 12, 2020
1 parent dbc88bb commit 13b80d9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 66 deletions.
88 changes: 44 additions & 44 deletions python/paddle/fluid/layers/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
import numpy as np
import paddle
import paddle.nn.functional as F
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = F.sigmoid(x)
print(out.numpy())
# [0.40131234 0.450166 0.52497919 0.57444252]
Expand All @@ -86,10 +86,10 @@
import numpy as np
import paddle
import paddle.nn.functional as F
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = F.logsigmoid(x)
print(out.numpy())
# [-0.91301525 -0.79813887 -0.64439666 -0.55435524]
Expand All @@ -102,10 +102,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.exp(x)
print(out.numpy())
# [0.67032005 0.81873075 1.10517092 1.34985881]
Expand All @@ -118,10 +118,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.tanh(x)
print(out.numpy())
# [-0.37994896 -0.19737532 0.09966799 0.29131261]
Expand All @@ -134,10 +134,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.atan(x)
print(out.numpy())
# [-0.38050638 -0.19739556 0.09966865 0.29145679]
Expand All @@ -151,10 +151,10 @@
import numpy as np
import paddle
import paddle.nn.functional as F
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = F.tanh_shrink(x)
print(out.numpy())
# [-0.02005104 -0.00262468 0.00033201 0.00868739]
Expand All @@ -167,10 +167,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([0.1, 0.2, 0.3, 0.4])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.sqrt(x)
print(out.numpy())
# [0.31622777 0.4472136 0.54772256 0.63245553]
Expand All @@ -183,10 +183,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([0.1, 0.2, 0.3, 0.4])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.rsqrt(x)
print(out.numpy())
# [3.16227766 2.23606798 1.82574186 1.58113883]
Expand All @@ -199,10 +199,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.abs(x)
print(out.numpy())
# [0.4 0.2 0.1 0.3]
Expand All @@ -215,10 +215,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.ceil(x)
print(out.numpy())
# [-0. -0. 1. 1.]
Expand All @@ -231,10 +231,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.floor(x)
print(out.numpy())
# [-1. -1. 0. 0.]
Expand All @@ -247,10 +247,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.cos(x)
print(out.numpy())
# [0.92106099 0.98006658 0.99500417 0.95533649]
Expand All @@ -263,10 +263,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.acos(x)
print(out.numpy())
# [1.98231317 1.77215425 1.47062891 1.26610367]
Expand All @@ -279,10 +279,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.sin(x)
print(out.numpy())
# [-0.38941834 -0.19866933 0.09983342 0.29552021]
Expand All @@ -295,10 +295,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.asin(x)
print(out.numpy())
# [-0.41151685 -0.20135792 0.10016742 0.30469265]
Expand All @@ -311,10 +311,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.cosh(x)
print(out.numpy())
# [1.08107237 1.02006676 1.00500417 1.04533851]
Expand All @@ -327,10 +327,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.sinh(x)
print(out.numpy())
# [-0.41075233 -0.201336 0.10016675 0.30452029]
Expand All @@ -343,10 +343,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.5, -0.2, 0.6, 1.5])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.round(x)
print(out.numpy())
# [-1. -0. 1. 2.]
Expand All @@ -359,10 +359,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.reciprocal(x)
print(out.numpy())
# [-2.5 -5. 10. 3.33333333]
Expand All @@ -375,10 +375,10 @@
import numpy as np
import paddle
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = paddle.square(x)
print(out.numpy())
# [0.16 0.04 0.01 0.09]
Expand All @@ -392,10 +392,10 @@
import numpy as np
import paddle
import paddle.nn.functional as F
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = F.softplus(x)
print(out.numpy())
# [0.51301525 0.59813887 0.74439666 0.85435524]
Expand All @@ -409,10 +409,10 @@
import numpy as np
import paddle
import paddle.nn.functional as F
paddle.enable_imperative()
paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.imperative.to_variable(x_data)
x = paddle.to_variable(x_data)
out = F.softsign(x)
print(out.numpy())
# [-0.28571429 -0.16666667 0.09090909 0.23076923]
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/test_addmm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ def test_api_error(self):
data_y = np.ones((2, 2)).astype(np.float32)
data_input = np.ones((2, 2)).astype(np.float32)
paddle.enable_imperative()
paddle.disable_static()
def test_error1():
data_x_wrong = np.ones((2, 3)).astype(np.float32)
x = paddle.imperative.to_variable(data_x_wrong)
y = paddle.imperative.to_variable(data_y)
input = paddle.imperative.to_variable(data_input)
x = paddle.to_variable(data_x_wrong)
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 )
self.assertRaises(ValueError, test_error1)
'''
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ def tril(x, diagonal=0, name=None):
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
paddle.enable_imperative()
paddle.disable_static()
x = paddle.imperative.to_variable(data)
x = paddle.to_variable(data)
tril1 = paddle.tensor.tril(x)
# array([[ 1, 0, 0, 0],
Expand Down Expand Up @@ -632,10 +632,10 @@ def triu(x, diagonal=0, name=None):
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
paddle.enable_imperative()
paddle.disable_static()
# example 1, default diagonal
x = paddle.imperative.to_variable(data)
x = paddle.to_variable(data)
triu1 = paddle.tensor.triu(x)
# array([[ 1, 2, 3, 4],
# [ 0, 6, 7, 8],
Expand Down
6 changes: 3 additions & 3 deletions python/paddle/tensor/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,10 @@ def bmm(x, y, name=None):
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]]])
paddle.enable_imperative()
paddle.disable_static()
x = paddle.imperative.to_variable(input1)
y = paddle.imperative.to_variable(input2)
x = paddle.to_variable(input1)
y = paddle.to_variable(input2)
out = paddle.bmm(x, y)
#output size: (2, 2, 2)
#output value:
Expand Down
10 changes: 5 additions & 5 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,10 @@ def stack(x, axis=0, name=None):
data2 = np.array([[3.0, 4.0]])
data3 = np.array([[5.0, 6.0]])
paddle.enable_imperative()
x1 = paddle.imperative.to_variable(data1)
x2 = paddle.imperative.to_variable(data2)
x3 = paddle.imperative.to_variable(data3)
paddle.disable_static()
x1 = paddle.to_variable(data1)
x2 = paddle.to_variable(data2)
x3 = paddle.to_variable(data3)
out = paddle.stack([x1, x2, x3], axis=0)
print(out.shape) # [3, 1, 2]
Expand Down Expand Up @@ -637,7 +637,7 @@ def unsqueeze(x, axis, name=None):
import paddle
paddle.enable_imperative()
paddle.disable_static()
x = paddle.rand([5, 10])
print(x.shape) # [5, 10]
Expand Down
12 changes: 6 additions & 6 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,11 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None):
data_y = np.ones((2, 2)).astype(np.float32)
data_input = np.ones((2, 2)).astype(np.float32)
paddle.enable_imperative()
paddle.disable_static()
x = paddle.imperative.to_variable(data_x)
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 )
Expand Down Expand Up @@ -1561,10 +1561,10 @@ def cumsum(x, axis=None, dtype=None, name=None):
.. code-block:: python
import paddle
from paddle.imperative import to_variable
from paddle import to_variable
import numpy as np
paddle.enable_imperative()
paddle.disable_static()
data_np = np.arange(12).reshape(3, 4)
data = to_variable(data_np)
Expand Down

0 comments on commit 13b80d9

Please sign in to comment.