|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import textwrap |
16 | 15 | import unittest |
17 | 16 |
|
18 | 17 | import numpy as np |
19 | 18 |
|
20 | 19 | import paddle |
| 20 | +from paddle import base |
21 | 21 |
|
22 | 22 |
|
23 | 23 | class TestPaddleRavel(unittest.TestCase): |
24 | 24 | def setUp(self): |
25 | | - self.data_np = np.array([[1, 2, 3], [4, 5, 6]], dtype='float32') |
26 | | - self.data_flat = self.data_np.ravel() |
27 | | - |
28 | | - def test_case_1(self): |
29 | | - paddle_code = textwrap.dedent( |
30 | | - """ |
31 | | - import paddle |
32 | | - x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') |
33 | | - result = paddle.ravel(x) |
34 | | - """ |
35 | | - ) |
36 | | - x = paddle.to_tensor(self.data_np) |
37 | | - result = paddle.ravel(x) |
38 | | - np.testing.assert_array_equal(result.numpy(), self.data_flat) |
39 | | - self.assertEqual(result.shape, [6]) |
40 | | - |
41 | | - def test_case_2(self): |
42 | | - paddle_code = textwrap.dedent( |
43 | | - """ |
44 | | - import paddle |
45 | | - x = paddle.to_tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype='int64') |
46 | | - result = paddle.ravel(x) |
47 | | - """ |
48 | | - ) |
49 | | - x = paddle.to_tensor( |
50 | | - [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype='int64' |
51 | | - ) |
52 | | - result = paddle.ravel(x) |
53 | | - np.testing.assert_array_equal(result.numpy(), np.ravel(x.numpy())) |
54 | | - self.assertEqual(list(result.shape), [8]) |
55 | | - |
56 | | - def test_case_3(self): |
57 | | - paddle_code = textwrap.dedent( |
58 | | - """ |
59 | | - import paddle |
60 | | - x = paddle.to_tensor(42, dtype='float32') |
61 | | - result = paddle.ravel(x) |
62 | | - """ |
63 | | - ) |
64 | | - x = paddle.to_tensor(42, dtype='float32') |
65 | | - result = paddle.ravel(x) |
66 | | - np.testing.assert_array_equal(result.numpy(), np.ravel(x.numpy())) |
67 | | - self.assertEqual(list(result.shape), [1]) |
68 | | - |
69 | | - def test_case_4(self): |
70 | | - paddle_code = textwrap.dedent( |
71 | | - """ |
72 | | - import paddle |
73 | | - x = paddle.to_tensor([], dtype='float32') |
74 | | - result = paddle.ravel(x) |
75 | | - """ |
76 | | - ) |
77 | | - x = paddle.to_tensor([], dtype='float32') |
78 | | - result = paddle.ravel(x) |
79 | | - np.testing.assert_array_equal(result.numpy(), np.ravel(x.numpy())) |
80 | | - self.assertEqual(list(result.shape), [0]) |
81 | | - |
82 | | - def test_case_5(self): |
83 | | - paddle_code = textwrap.dedent( |
84 | | - """ |
85 | | - import paddle |
86 | | - x = paddle.to_tensor([10, 20, 30], dtype='int32') |
87 | | - result = paddle.ravel(x) |
88 | | - """ |
89 | | - ) |
90 | | - x = paddle.to_tensor([10, 20, 30], dtype='int32') |
91 | | - result = paddle.ravel(x) |
92 | | - np.testing.assert_array_equal(result.numpy(), np.ravel(x.numpy())) |
93 | | - self.assertEqual(list(result.shape), [3]) |
| 25 | + self.input_np = np.array([[1, 2, 3], [4, 5, 6]], dtype="float32") |
| 26 | + self.input_shape = self.input_np.shape |
| 27 | + self.input_dtype = "float32" |
| 28 | + self.op_static = lambda x: paddle.ravel(x) |
| 29 | + self.op_dygraph = lambda x: paddle.ravel(x) |
| 30 | + self.expected = lambda x: x.flatten() |
| 31 | + self.places = [None, paddle.CPUPlace()] |
| 32 | + |
| 33 | + def check_static_result(self, place): |
| 34 | + paddle.enable_static() |
| 35 | + main_prog = paddle.static.Program() |
| 36 | + startup_prog = paddle.static.Program() |
| 37 | + with paddle.static.program_guard(main_prog, startup_prog): |
| 38 | + input_name = 'input' |
| 39 | + input_var = paddle.static.data( |
| 40 | + name=input_name, shape=self.input_shape, dtype=self.input_dtype |
| 41 | + ) |
| 42 | + res = self.op_static(input_var) |
| 43 | + exe = base.Executor(place) |
| 44 | + fetches = exe.run( |
| 45 | + main_prog, |
| 46 | + feed={input_name: self.input_np}, |
| 47 | + fetch_list=[res], |
| 48 | + ) |
| 49 | + expect = ( |
| 50 | + self.expected(self.input_np) |
| 51 | + if callable(self.expected) |
| 52 | + else self.expected |
| 53 | + ) |
| 54 | + np.testing.assert_allclose(fetches[0], expect, rtol=1e-05) |
| 55 | + |
| 56 | + def test_static(self): |
| 57 | + for place in self.places: |
| 58 | + self.check_static_result(place=place) |
| 59 | + |
| 60 | + def check_dygraph_result(self, place): |
| 61 | + with base.dygraph.guard(place): |
| 62 | + input = paddle.to_tensor(self.input_np, stop_gradient=False) |
| 63 | + result = self.op_dygraph(input) |
| 64 | + expect = ( |
| 65 | + self.expected(self.input_np) |
| 66 | + if callable(self.expected) |
| 67 | + else self.expected |
| 68 | + ) |
| 69 | + # check forward |
| 70 | + np.testing.assert_allclose(result.numpy(), expect, rtol=1e-05) |
| 71 | + |
| 72 | + # check backward |
| 73 | + paddle.autograd.backward([result]) |
| 74 | + np.testing.assert_allclose( |
| 75 | + input.grad.numpy(), np.ones_like(self.input_np), rtol=1e-05 |
| 76 | + ) |
| 77 | + |
| 78 | + def test_dygraph(self): |
| 79 | + for place in self.places: |
| 80 | + self.check_dygraph_result(place=place) |
| 81 | + |
| 82 | + |
| 83 | +class TestPaddleRavel_case1(TestPaddleRavel): |
| 84 | + def setUp(self): |
| 85 | + # check Ravel 1d |
| 86 | + self.input_np = np.array([7, 8, 9], dtype="float32") |
| 87 | + self.input_shape = self.input_np.shape |
| 88 | + self.input_dtype = "float32" |
| 89 | + self.op_static = lambda x: paddle.ravel(x) |
| 90 | + self.op_dygraph = lambda x: paddle.ravel(x) |
| 91 | + self.expected = lambda x: x.flatten() |
| 92 | + self.places = [None, paddle.CPUPlace()] |
| 93 | + |
| 94 | + |
| 95 | +class TestPaddleRavel_case2(TestPaddleRavel): |
| 96 | + def setUp(self): |
| 97 | + # check Ravel 3d |
| 98 | + self.input_np = np.arange(24, dtype="float32").reshape(2, 3, 4) |
| 99 | + self.input_shape = self.input_np.shape |
| 100 | + self.input_dtype = "float32" |
| 101 | + self.op_static = lambda x: paddle.ravel(x) |
| 102 | + self.op_dygraph = lambda x: paddle.ravel(x) |
| 103 | + self.expected = lambda x: x.flatten() |
| 104 | + self.places = [None, paddle.CPUPlace()] |
| 105 | + |
| 106 | + |
| 107 | +class TestPaddleRavel_case3(TestPaddleRavel): |
| 108 | + def setUp(self): |
| 109 | + # check Ravel 0d (scalar) |
| 110 | + self.input_np = np.array(5.0, dtype="float32") # 标量 |
| 111 | + self.input_shape = self.input_np.shape |
| 112 | + self.input_dtype = "float32" |
| 113 | + self.op_static = lambda x: paddle.ravel(x) |
| 114 | + self.op_dygraph = lambda x: paddle.ravel(x) |
| 115 | + self.expected = lambda x: x.flatten() |
| 116 | + self.places = [None, paddle.CPUPlace()] |
| 117 | + |
| 118 | + |
| 119 | +class TestPaddleRavel_case4(TestPaddleRavel): |
| 120 | + def setUp(self): |
| 121 | + # check Ravel empty array |
| 122 | + self.input_np = np.array([], dtype="float32").reshape(0, 3) |
| 123 | + self.input_shape = self.input_np.shape |
| 124 | + self.input_dtype = "float32" |
| 125 | + self.op_static = lambda x: paddle.ravel(x) |
| 126 | + self.op_dygraph = lambda x: paddle.ravel(x) |
| 127 | + self.expected = lambda x: x.flatten() |
| 128 | + self.places = [None, paddle.CPUPlace()] |
94 | 129 |
|
95 | 130 |
|
96 | 131 | if __name__ == "__main__": |
|
0 commit comments