|
20 | 20 | import unittest |
21 | 21 |
|
22 | 22 |
|
23 | | -class TestL1Loss(unittest.TestCase): |
24 | | - def test_L1Loss_mean(self): |
25 | | - input_np = np.random.random(size=(10, 1)).astype(np.float32) |
26 | | - label_np = np.random.random(size=(10, 1)).astype(np.float32) |
27 | | - prog = fluid.Program() |
28 | | - startup_prog = fluid.Program() |
29 | | - place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda( |
30 | | - ) else fluid.CPUPlace() |
31 | | - with fluid.program_guard(prog, startup_prog): |
32 | | - input = fluid.layers.data( |
33 | | - name='input', shape=[10, 1], dtype='float32') |
34 | | - label = fluid.layers.data( |
35 | | - name='label', shape=[10, 1], dtype='float32') |
36 | | - l1_loss = paddle.nn.loss.L1Loss() |
37 | | - ret = l1_loss(input, label) |
38 | | - |
39 | | - exe = fluid.Executor(place) |
40 | | - static_result = exe.run( |
41 | | - prog, |
42 | | - feed={"input": input_np, |
43 | | - "label": label_np}, |
44 | | - fetch_list=[ret]) |
45 | | - |
46 | | - with fluid.dygraph.guard(): |
47 | | - l1_loss = paddle.nn.loss.L1Loss() |
48 | | - dy_ret = l1_loss( |
49 | | - fluid.dygraph.to_variable(input_np), |
50 | | - fluid.dygraph.to_variable(label_np)) |
51 | | - dy_result = dy_ret.numpy() |
52 | | - |
53 | | - expected = np.mean(np.abs(input_np - label_np)) |
54 | | - self.assertTrue(np.allclose(static_result, expected)) |
55 | | - self.assertTrue(np.allclose(static_result, dy_result)) |
56 | | - self.assertTrue(np.allclose(dy_result, expected)) |
| 23 | +class TestFunctionalL1Loss(unittest.TestCase): |
| 24 | + def setUp(self): |
| 25 | + self.input_np = np.random.random(size=(10, 10, 5)).astype(np.float32) |
| 26 | + self.label_np = np.random.random(size=(10, 10, 5)).astype(np.float32) |
| 27 | + |
| 28 | + def run_imperative(self): |
| 29 | + input = paddle.imperative.to_variable(self.input_np) |
| 30 | + label = paddle.imperative.to_variable(self.label_np) |
| 31 | + dy_result = paddle.nn.functional.l1_loss(input, label) |
| 32 | + expected = np.mean(np.abs(self.input_np - self.label_np)) |
| 33 | + self.assertTrue(np.allclose(dy_result.numpy(), expected)) |
57 | 34 | self.assertTrue(dy_result.shape, [1]) |
58 | 35 |
|
59 | | - def test_L1Loss_sum(self): |
60 | | - input_np = np.random.random(size=(10, 10, 5)).astype(np.float32) |
61 | | - label_np = np.random.random(size=(10, 10, 5)).astype(np.float32) |
62 | | - prog = fluid.Program() |
63 | | - startup_prog = fluid.Program() |
64 | | - place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda( |
65 | | - ) else fluid.CPUPlace() |
66 | | - with fluid.program_guard(prog, startup_prog): |
67 | | - input = fluid.layers.data( |
| 36 | + dy_result = paddle.nn.functional.l1_loss(input, label, reduction='sum') |
| 37 | + expected = np.sum(np.abs(self.input_np - self.label_np)) |
| 38 | + self.assertTrue(np.allclose(dy_result.numpy(), expected)) |
| 39 | + self.assertTrue(dy_result.shape, [1]) |
| 40 | + |
| 41 | + dy_result = paddle.nn.functional.l1_loss(input, label, reduction='none') |
| 42 | + expected = np.abs(self.input_np - self.label_np) |
| 43 | + self.assertTrue(np.allclose(dy_result.numpy(), expected)) |
| 44 | + self.assertTrue(dy_result.shape, [10, 10, 5]) |
| 45 | + |
| 46 | + def run_static(self, use_gpu=False): |
| 47 | + input = paddle.data(name='input', shape=[10, 10, 5], dtype='float32') |
| 48 | + label = paddle.data(name='label', shape=[10, 10, 5], dtype='float32') |
| 49 | + result0 = paddle.nn.functional.l1_loss(input, label) |
| 50 | + result1 = paddle.nn.functional.l1_loss(input, label, reduction='sum') |
| 51 | + result2 = paddle.nn.functional.l1_loss(input, label, reduction='none') |
| 52 | + y = paddle.nn.functional.l1_loss(input, label, name='aaa') |
| 53 | + |
| 54 | + place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() |
| 55 | + exe = fluid.Executor(place) |
| 56 | + exe.run(fluid.default_startup_program()) |
| 57 | + static_result = exe.run( |
| 58 | + feed={"input": self.input_np, |
| 59 | + "label": self.label_np}, |
| 60 | + fetch_list=[result0, result1, result2]) |
| 61 | + |
| 62 | + expected = np.mean(np.abs(self.input_np - self.label_np)) |
| 63 | + self.assertTrue(np.allclose(static_result[0], expected)) |
| 64 | + expected = np.sum(np.abs(self.input_np - self.label_np)) |
| 65 | + self.assertTrue(np.allclose(static_result[1], expected)) |
| 66 | + expected = np.abs(self.input_np - self.label_np) |
| 67 | + self.assertTrue(np.allclose(static_result[2], expected)) |
| 68 | + |
| 69 | + self.assertTrue('aaa' in y.name) |
| 70 | + |
| 71 | + def test_cpu(self): |
| 72 | + with paddle.imperative.guard(paddle.fluid.CPUPlace()): |
| 73 | + self.run_imperative() |
| 74 | + |
| 75 | + with fluid.program_guard(fluid.Program()): |
| 76 | + self.run_static() |
| 77 | + |
| 78 | + def test_gpu(self): |
| 79 | + if not fluid.core.is_compiled_with_cuda(): |
| 80 | + return |
| 81 | + |
| 82 | + with paddle.imperative.guard(paddle.fluid.CUDAPlace(0)): |
| 83 | + self.run_imperative() |
| 84 | + |
| 85 | + with fluid.program_guard(fluid.Program()): |
| 86 | + self.run_static(use_gpu=True) |
| 87 | + |
| 88 | + # test case the raise message |
| 89 | + def test_errors(self): |
| 90 | + def test_value_error(): |
| 91 | + input = paddle.data( |
68 | 92 | name='input', shape=[10, 10, 5], dtype='float32') |
69 | | - label = fluid.layers.data( |
| 93 | + label = paddle.data( |
70 | 94 | name='label', shape=[10, 10, 5], dtype='float32') |
71 | | - l1_loss = paddle.nn.loss.L1Loss(reduction='sum') |
72 | | - ret = l1_loss(input, label) |
73 | | - |
74 | | - exe = fluid.Executor(place) |
75 | | - static_result = exe.run( |
76 | | - prog, |
77 | | - feed={"input": input_np, |
78 | | - "label": label_np}, |
79 | | - fetch_list=[ret]) |
80 | | - |
81 | | - with fluid.dygraph.guard(): |
82 | | - l1_loss = paddle.nn.loss.L1Loss(reduction='sum') |
83 | | - dy_ret = l1_loss( |
84 | | - fluid.dygraph.to_variable(input_np), |
85 | | - fluid.dygraph.to_variable(label_np)) |
86 | | - dy_result = dy_ret.numpy() |
87 | | - |
88 | | - expected = np.sum(np.abs(input_np - label_np)) |
89 | | - self.assertTrue(np.allclose(static_result, expected)) |
90 | | - self.assertTrue(np.allclose(static_result, dy_result)) |
91 | | - self.assertTrue(np.allclose(dy_result, expected)) |
| 95 | + loss = paddle.nn.functional.l1_loss( |
| 96 | + input, label, reduction='reduce_mean') |
| 97 | + |
| 98 | + self.assertRaises(ValueError, test_value_error) |
| 99 | + |
| 100 | + |
| 101 | +class TestClassL1Loss(unittest.TestCase): |
| 102 | + def setUp(self): |
| 103 | + self.input_np = np.random.random(size=(10, 10, 5)).astype(np.float32) |
| 104 | + self.label_np = np.random.random(size=(10, 10, 5)).astype(np.float32) |
| 105 | + |
| 106 | + def run_imperative(self): |
| 107 | + input = paddle.imperative.to_variable(self.input_np) |
| 108 | + label = paddle.imperative.to_variable(self.label_np) |
| 109 | + l1_loss = paddle.nn.loss.L1Loss() |
| 110 | + dy_result = l1_loss(input, label) |
| 111 | + expected = np.mean(np.abs(self.input_np - self.label_np)) |
| 112 | + self.assertTrue(np.allclose(dy_result.numpy(), expected)) |
| 113 | + self.assertTrue(dy_result.shape, [1]) |
| 114 | + |
| 115 | + l1_loss = paddle.nn.loss.L1Loss(reduction='sum') |
| 116 | + dy_result = l1_loss(input, label) |
| 117 | + expected = np.sum(np.abs(self.input_np - self.label_np)) |
| 118 | + self.assertTrue(np.allclose(dy_result.numpy(), expected)) |
92 | 119 | self.assertTrue(dy_result.shape, [1]) |
93 | 120 |
|
94 | | - def test_L1Loss_none(self): |
95 | | - input_np = np.random.random(size=(10, 5)).astype(np.float32) |
96 | | - label_np = np.random.random(size=(10, 5)).astype(np.float32) |
97 | | - prog = fluid.Program() |
98 | | - startup_prog = fluid.Program() |
99 | | - place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda( |
100 | | - ) else fluid.CPUPlace() |
101 | | - with fluid.program_guard(prog, startup_prog): |
102 | | - input = fluid.layers.data( |
103 | | - name='input', shape=[10, 5], dtype='float32') |
104 | | - label = fluid.layers.data( |
105 | | - name='label', shape=[10, 5], dtype='float32') |
106 | | - l1_loss = paddle.nn.loss.L1Loss(reduction='none') |
107 | | - ret = l1_loss(input, label) |
108 | | - |
109 | | - exe = fluid.Executor(place) |
110 | | - static_result = exe.run( |
111 | | - prog, |
112 | | - feed={"input": input_np, |
113 | | - "label": label_np}, |
114 | | - fetch_list=[ret]) |
115 | | - |
116 | | - with fluid.dygraph.guard(): |
117 | | - l1_loss = paddle.nn.loss.L1Loss(reduction='none') |
118 | | - dy_ret = l1_loss( |
119 | | - fluid.dygraph.to_variable(input_np), |
120 | | - fluid.dygraph.to_variable(label_np)) |
121 | | - dy_result = dy_ret.numpy() |
122 | | - |
123 | | - expected = np.abs(input_np - label_np) |
124 | | - self.assertTrue(np.allclose(static_result, expected)) |
125 | | - self.assertTrue(np.allclose(static_result, dy_result)) |
126 | | - self.assertTrue(np.allclose(dy_result, expected)) |
127 | | - self.assertTrue(dy_result.shape, input.shape) |
| 121 | + l1_loss = paddle.nn.loss.L1Loss(reduction='none') |
| 122 | + dy_result = l1_loss(input, label) |
| 123 | + expected = np.abs(self.input_np - self.label_np) |
| 124 | + self.assertTrue(np.allclose(dy_result.numpy(), expected)) |
| 125 | + self.assertTrue(dy_result.shape, [10, 10, 5]) |
| 126 | + |
| 127 | + def run_static(self, use_gpu=False): |
| 128 | + input = paddle.data(name='input', shape=[10, 10, 5], dtype='float32') |
| 129 | + label = paddle.data(name='label', shape=[10, 10, 5], dtype='float32') |
| 130 | + l1_loss = paddle.nn.loss.L1Loss() |
| 131 | + result0 = l1_loss(input, label) |
| 132 | + l1_loss = paddle.nn.loss.L1Loss(reduction='sum') |
| 133 | + result1 = l1_loss(input, label) |
| 134 | + l1_loss = paddle.nn.loss.L1Loss(reduction='none') |
| 135 | + result2 = l1_loss(input, label) |
| 136 | + l1_loss = paddle.nn.loss.L1Loss(name='aaa') |
| 137 | + result3 = l1_loss(input, label) |
| 138 | + |
| 139 | + place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() |
| 140 | + exe = fluid.Executor(place) |
| 141 | + exe.run(fluid.default_startup_program()) |
| 142 | + static_result = exe.run( |
| 143 | + feed={"input": self.input_np, |
| 144 | + "label": self.label_np}, |
| 145 | + fetch_list=[result0, result1, result2]) |
| 146 | + |
| 147 | + expected = np.mean(np.abs(self.input_np - self.label_np)) |
| 148 | + self.assertTrue(np.allclose(static_result[0], expected)) |
| 149 | + expected = np.sum(np.abs(self.input_np - self.label_np)) |
| 150 | + self.assertTrue(np.allclose(static_result[1], expected)) |
| 151 | + expected = np.abs(self.input_np - self.label_np) |
| 152 | + self.assertTrue(np.allclose(static_result[2], expected)) |
| 153 | + self.assertTrue('aaa' in result3.name) |
| 154 | + |
| 155 | + def test_cpu(self): |
| 156 | + with paddle.imperative.guard(paddle.fluid.CPUPlace()): |
| 157 | + self.run_imperative() |
| 158 | + |
| 159 | + with fluid.program_guard(fluid.Program()): |
| 160 | + self.run_static() |
| 161 | + |
| 162 | + def test_gpu(self): |
| 163 | + if not fluid.core.is_compiled_with_cuda(): |
| 164 | + return |
| 165 | + |
| 166 | + with paddle.imperative.guard(paddle.fluid.CUDAPlace(0)): |
| 167 | + self.run_imperative() |
| 168 | + |
| 169 | + with fluid.program_guard(fluid.Program()): |
| 170 | + self.run_static(use_gpu=True) |
| 171 | + |
| 172 | + # test case the raise message |
| 173 | + def test_errors(self): |
| 174 | + def test_value_error(): |
| 175 | + loss = paddle.nn.loss.L1Loss(reduction="reduce_mean") |
| 176 | + |
| 177 | + self.assertRaises(ValueError, test_value_error) |
128 | 178 |
|
129 | 179 |
|
130 | 180 | if __name__ == "__main__": |
|
0 commit comments