Skip to content

Commit

Permalink
Merge pull request #5 from levi131/lml/code_refine
Browse files Browse the repository at this point in the history
polish test_gradients_and_minimize
  • Loading branch information
levi131 authored May 17, 2022
2 parents d797c21 + dcad8f3 commit 9596344
Showing 1 changed file with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ def matmul(x, y):

x = np.random.rand(5, 5)
y = np.random.rand(5, 5)
disable_prim()
origs = matmul(x, y)
enable_prim()
prims = matmul(x, y)
disable_prim()
origs = matmul(x, y)
for orig, prim in zip(origs, prims):
np.testing.assert_allclose(orig, prim)

Expand All @@ -66,7 +65,7 @@ def matmul_second_order(x, y):
static_y.stop_gradient = False
z = paddle.matmul(static_x, static_x)
x_grad, = paddle.static.gradients([z], [static_x])
xx_grad, = paddle.static.gradients(x_grad, [static_x])
xx_grad, = paddle.static.gradients([x_grad], [static_x])
if prim_enabled():
prim2orig(main.block(0))
exe = paddle.static.Executor()
Expand All @@ -78,11 +77,10 @@ def matmul_second_order(x, y):

x = np.random.rand(5, 5)
y = np.random.rand(5, 5)
disable_prim()
origs = matmul_second_order(x, y)
enable_prim()
prims = matmul_second_order(x, y)
disable_prim()
origs = matmul_second_order(x, y)
for orig, prim in zip(origs, prims):
np.testing.assert_allclose(orig, prim)

Expand Down Expand Up @@ -151,6 +149,7 @@ def test_fourth_order(self):

class TestMinimize(unittest.TestCase):
def model(self, x, w, bias, opt):
paddle.seed(0)
place = paddle.CPUPlace()
if paddle.device.is_compiled_with_cuda():
place = paddle.CUDAPlace(0)
Expand All @@ -170,7 +169,6 @@ def model(self, x, w, bias, opt):
_, grads = opt.minimize(loss)
if prim_enabled():
prim2orig(main.block(0))
paddle.seed(0)
exe.run(startup)
grads = exe.run(main,
feed={'x': x,
Expand All @@ -183,23 +181,21 @@ def test_adam(self):
x = np.random.rand(2, 20)
w = np.random.rand(20, 2)
bias = np.random.rand(2)
disable_prim()
orig_grads = self.model(x, w, bias, paddle.optimizer.Adam(0.01))
enable_prim()
prim_grads = self.model(x, w, bias, paddle.optimizer.Adam(0.01))
disable_prim()
orig_grads = self.model(x, w, bias, paddle.optimizer.Adam(0.01))
for orig, prim in zip(orig_grads, prim_grads):
np.testing.assert_allclose(orig, prim)

def test_sgd(self):
x = np.random.rand(2, 20)
w = np.random.rand(20, 2)
bias = np.random.rand(2)
disable_prim()
orig_grads = self.model(x, w, bias, paddle.optimizer.SGD(0.01))
enable_prim()
prim_grads = self.model(x, w, bias, paddle.optimizer.SGD(0.01))
disable_prim()
orig_grads = self.model(x, w, bias, paddle.optimizer.SGD(0.01))
for orig, prim in zip(orig_grads, prim_grads):
np.testing.assert_allclose(orig, prim)

Expand Down

1 comment on commit 9596344

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on 9596344 May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕵️ CI failures summary

🔍 PR: #5 Commit ID: 9596344 contains failed CI.

🔹 Failed: PR-CI-APPROVAL

Unknown Failed
Unknown Failed

🔹 Failed: PR-CI-Windows

Unknown Failed
Unknown Failed

Please sign in to comment.