Add unified RNN APIs (#26588)
* Add RNN related apis in paddl.nn test=develop * new rnn api, cell almost done * add new progresses in rnn APIs for 2.0 * refine rnn APIs and docstrings. * add unittets * disable gpu tests when paddle is not compiled with cuda support * remove unnecessary imports * fix docstring * add to no_sample wlist * backport to python2 to avoid yield from * add **kwargs, fix typos * update docstrings for birnn * rename argument for SimpleRNN and SimpleRNNCell, fix sample code * add default value for initial_states in fluid.layers.birnn Co-authored-by: guosheng <guosheng@baidu.com>revert-26856-strategy_example2
parent
f311d3c1cf
commit
f4083010a7
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,6 @@
|
||||
file(GLOB TEST_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py")
|
||||
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
|
||||
|
||||
foreach(TEST_OP ${TEST_OPS})
|
||||
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
|
||||
endforeach(TEST_OP)
|
@ -0,0 +1,13 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
@ -0,0 +1,51 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import paddle
|
||||
import numpy as np
|
||||
|
||||
|
||||
def convert_params_for_cell(np_cell, paddle_cell):
|
||||
state = np_cell.parameters
|
||||
for k, v in paddle_cell.named_parameters():
|
||||
v.set_value(state[k])
|
||||
|
||||
|
||||
def convert_params_for_cell_static(np_cell, paddle_cell, place):
|
||||
state = np_cell.parameters
|
||||
for k, v in paddle_cell.named_parameters():
|
||||
scope = paddle.static.global_scope()
|
||||
tensor = scope.find_var(v.name).get_tensor()
|
||||
tensor.set(state[k], place)
|
||||
|
||||
|
||||
def convert_params_for_net(np_net, paddle_net):
|
||||
for np_layer, paddle_layer in zip(np_net, paddle_net):
|
||||
if hasattr(np_layer, "cell"):
|
||||
convert_params_for_cell(np_layer.cell, paddle_layer.cell)
|
||||
else:
|
||||
convert_params_for_cell(np_layer.cell_fw, paddle_layer.cell_fw)
|
||||
convert_params_for_cell(np_layer.cell_bw, paddle_layer.cell_bw)
|
||||
|
||||
|
||||
def convert_params_for_net_static(np_net, paddle_net, place):
|
||||
for np_layer, paddle_layer in zip(np_net, paddle_net):
|
||||
if hasattr(np_layer, "cell"):
|
||||
convert_params_for_cell_static(np_layer.cell, paddle_layer.cell,
|
||||
place)
|
||||
else:
|
||||
convert_params_for_cell_static(np_layer.cell_fw,
|
||||
paddle_layer.cell_fw, place)
|
||||
convert_params_for_cell_static(np_layer.cell_bw,
|
||||
paddle_layer.cell_bw, place)
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,166 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import paddle
|
||||
paddle.framework.set_default_dtype("float64")
|
||||
|
||||
import numpy as np
|
||||
import unittest
|
||||
|
||||
from rnn_numpy import SimpleRNNCell, LSTMCell, GRUCell
|
||||
from convert import convert_params_for_cell
|
||||
|
||||
|
||||
class TestSimpleRNNCell(unittest.TestCase):
|
||||
def __init__(self, bias=True, place="cpu"):
|
||||
super(TestSimpleRNNCell, self).__init__(methodName="runTest")
|
||||
self.bias = bias
|
||||
self.place = paddle.CPUPlace() if place == "cpu" \
|
||||
else paddle.CUDAPlace(0)
|
||||
|
||||
def setUp(self):
|
||||
paddle.disable_static(self.place)
|
||||
rnn1 = SimpleRNNCell(16, 32, bias=self.bias)
|
||||
rnn2 = paddle.nn.SimpleRNNCell(
|
||||
16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias)
|
||||
convert_params_for_cell(rnn1, rnn2)
|
||||
|
||||
self.rnn1 = rnn1
|
||||
self.rnn2 = rnn2
|
||||
|
||||
def test_with_initial_state(self):
|
||||
rnn1 = self.rnn1
|
||||
rnn2 = self.rnn2
|
||||
|
||||
x = np.random.randn(4, 16)
|
||||
prev_h = np.random.randn(4, 32)
|
||||
|
||||
y1, h1 = rnn1(x, prev_h)
|
||||
y2, h2 = rnn2(paddle.to_variable(x), paddle.to_variable(prev_h))
|
||||
np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
|
||||
def test_with_zero_state(self):
|
||||
rnn1 = self.rnn1
|
||||
rnn2 = self.rnn2
|
||||
|
||||
x = np.random.randn(4, 16)
|
||||
|
||||
y1, h1 = rnn1(x)
|
||||
y2, h2 = rnn2(paddle.to_variable(x))
|
||||
np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
|
||||
def runTest(self):
|
||||
self.test_with_initial_state()
|
||||
self.test_with_zero_state()
|
||||
|
||||
|
||||
class TestGRUCell(unittest.TestCase):
|
||||
def __init__(self, bias=True, place="cpu"):
|
||||
super(TestGRUCell, self).__init__(methodName="runTest")
|
||||
self.bias = bias
|
||||
self.place = paddle.CPUPlace() if place == "cpu" \
|
||||
else paddle.CUDAPlace(0)
|
||||
|
||||
def setUp(self):
|
||||
paddle.disable_static(self.place)
|
||||
rnn1 = GRUCell(16, 32, bias=self.bias)
|
||||
rnn2 = paddle.nn.GRUCell(
|
||||
16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias)
|
||||
convert_params_for_cell(rnn1, rnn2)
|
||||
|
||||
self.rnn1 = rnn1
|
||||
self.rnn2 = rnn2
|
||||
|
||||
def test_with_initial_state(self):
|
||||
rnn1 = self.rnn1
|
||||
rnn2 = self.rnn2
|
||||
|
||||
x = np.random.randn(4, 16)
|
||||
prev_h = np.random.randn(4, 32)
|
||||
|
||||
y1, h1 = rnn1(x, prev_h)
|
||||
y2, h2 = rnn2(paddle.to_variable(x), paddle.to_variable(prev_h))
|
||||
np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
|
||||
def test_with_zero_state(self):
|
||||
rnn1 = self.rnn1
|
||||
rnn2 = self.rnn2
|
||||
|
||||
x = np.random.randn(4, 16)
|
||||
|
||||
y1, h1 = rnn1(x)
|
||||
y2, h2 = rnn2(paddle.to_variable(x))
|
||||
np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
|
||||
def runTest(self):
|
||||
self.test_with_initial_state()
|
||||
self.test_with_zero_state()
|
||||
|
||||
|
||||
class TestLSTMCell(unittest.TestCase):
|
||||
def __init__(self, bias=True, place="cpu"):
|
||||
super(TestLSTMCell, self).__init__(methodName="runTest")
|
||||
self.bias = bias
|
||||
self.place = paddle.CPUPlace() if place == "cpu" \
|
||||
else paddle.CUDAPlace(0)
|
||||
|
||||
def setUp(self):
|
||||
rnn1 = LSTMCell(16, 32, bias=self.bias)
|
||||
rnn2 = paddle.nn.LSTMCell(
|
||||
16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias)
|
||||
convert_params_for_cell(rnn1, rnn2)
|
||||
|
||||
self.rnn1 = rnn1
|
||||
self.rnn2 = rnn2
|
||||
|
||||
def test_with_initial_state(self):
|
||||
rnn1 = self.rnn1
|
||||
rnn2 = self.rnn2
|
||||
|
||||
x = np.random.randn(4, 16)
|
||||
prev_h = np.random.randn(4, 32)
|
||||
prev_c = np.random.randn(4, 32)
|
||||
|
||||
y1, (h1, c1) = rnn1(x, (prev_h, prev_c))
|
||||
y2, (h2, c2) = rnn2(
|
||||
paddle.to_variable(x),
|
||||
(paddle.to_variable(prev_h), paddle.to_variable(prev_c)))
|
||||
np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
np.testing.assert_allclose(c1, c2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
|
||||
def test_with_zero_state(self):
|
||||
rnn1 = self.rnn1
|
||||
rnn2 = self.rnn2
|
||||
|
||||
x = np.random.randn(4, 16)
|
||||
|
||||
y1, (h1, c1) = rnn1(x)
|
||||
y2, (h2, c2) = rnn2(paddle.to_variable(x))
|
||||
np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
np.testing.assert_allclose(c1, c2.numpy(), atol=1e-8, rtol=1e-5)
|
||||
|
||||
def runTest(self):
|
||||
self.test_with_initial_state()
|
||||
self.test_with_zero_state()
|
||||
|
||||
|
||||
def load_tests(loader, tests, pattern):
|
||||
suite = unittest.TestSuite()
|
||||
devices = ["cpu", "gpu"] if paddle.fluid.is_compiled_with_cuda() \
|
||||
else ["cpu"]
|
||||
for bias in [True, False]:
|
||||
for device in devices:
|
||||
for test_class in [TestSimpleRNNCell, TestGRUCell, TestLSTMCell]:
|
||||
suite.addTest(test_class(bias, device))
|
||||
return suite
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue