提交 cf307d0d 编写于 作者: Q Qiyang Min 提交者: Jiabin Yang

Imperative fix ptb rnn place bug (#16632)

* Fix bug of gradient interface

* shrink transformer

* Right transformer

* Change from width-first backward to deep-first backward process

test=develop

* Reverse iterator op's input

test=develop

* Polish code

* Change the iteration direction in ingrads' map slots

test=develop

* Polish code

test=develop

* Add GPU place in static mode of ptb rnn

test=develop

* Polish code

test=develop
上级 044ae249
...@@ -16,6 +16,7 @@ from __future__ import print_function ...@@ -16,6 +16,7 @@ from __future__ import print_function
import unittest import unittest
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.dygraph.nn import Embedding from paddle.fluid.dygraph.nn import Embedding
import paddle.fluid.framework as framework import paddle.fluid.framework as framework
from paddle.fluid.optimizer import SGDOptimizer from paddle.fluid.optimizer import SGDOptimizer
...@@ -278,7 +279,8 @@ class TestDygraphPtbRnn(unittest.TestCase): ...@@ -278,7 +279,8 @@ class TestDygraphPtbRnn(unittest.TestCase):
num_steps=num_steps, num_steps=num_steps,
init_scale=init_scale) init_scale=init_scale)
exe = fluid.Executor(fluid.CPUPlace()) exe = fluid.Executor(fluid.CPUPlace(
) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0))
sgd = SGDOptimizer(learning_rate=1e-3) sgd = SGDOptimizer(learning_rate=1e-3)
x = fluid.layers.data( x = fluid.layers.data(
name="x", shape=[-1, num_steps, 1], dtype='int64') name="x", shape=[-1, num_steps, 1], dtype='int64')
...@@ -331,18 +333,16 @@ class TestDygraphPtbRnn(unittest.TestCase): ...@@ -331,18 +333,16 @@ class TestDygraphPtbRnn(unittest.TestCase):
for k in range(3, len(out)): for k in range(3, len(out)):
static_param_updated[static_param_name_list[k - static_param_updated[static_param_name_list[k -
3]] = out[k] 3]] = out[k]
self.assertTrue(np.allclose(static_loss_value, dy_loss._numpy()))
self.assertTrue(np.allclose(static_last_cell_value, last_cell._numpy())) self.assertTrue(np.array_equal(static_loss_value, dy_loss._numpy()))
self.assertTrue(
np.array_equal(static_last_cell_value, last_cell._numpy()))
self.assertTrue( self.assertTrue(
np.allclose(static_last_hidden_value, last_hidden._numpy())) np.array_equal(static_last_hidden_value, last_hidden._numpy()))
for key, value in six.iteritems(static_param_init): for key, value in six.iteritems(static_param_init):
# print("static_init name: {}, value {}".format(key, value)) self.assertTrue(np.array_equal(value, dy_param_init[key]))
# print("dy_init name: {}, value {}".format(key, dy_param_init[key]))
self.assertTrue(np.allclose(value, dy_param_init[key]))
for key, value in six.iteritems(static_param_updated): for key, value in six.iteritems(static_param_updated):
# print("static name: {}, value {}".format(key, value)) self.assertTrue(np.array_equal(value, dy_param_updated[key]))
# print("dy name: {}, value {}".format(key, dy_param_updated[key]))
self.assertTrue(np.allclose(value, dy_param_updated[key]))
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册