|
19 | 19 | from op_test import OpTest |
20 | 20 | import paddle |
21 | 21 | import paddle.fluid as fluid |
| 22 | +import paddle.fluid.core as core |
22 | 23 | from paddle.fluid import Program, program_guard |
| 24 | +import gradient_checker |
| 25 | +from decorator_helper import prog_scope |
| 26 | +import paddle.fluid.layers as layers |
23 | 27 |
|
24 | 28 |
|
25 | 29 | class TestSignOp(OpTest): |
@@ -91,6 +95,80 @@ def test_static(self): |
91 | 95 | paddle.sign(input4) |
92 | 96 |
|
93 | 97 |
|
| 98 | +class TestSignDoubleGradCheck(unittest.TestCase): |
| 99 | + |
| 100 | + def sign_wrapper(self, x): |
| 101 | + return paddle.sign(x[0]) |
| 102 | + |
| 103 | + @prog_scope() |
| 104 | + def func(self, place): |
| 105 | + # the shape of input variable should be clearly specified, not inlcude -1. |
| 106 | + eps = 0.005 |
| 107 | + dtype = np.float32 |
| 108 | + |
| 109 | + data = layers.data('data', [1, 4], False, dtype) |
| 110 | + data.persistable = True |
| 111 | + out = paddle.sign(data) |
| 112 | + data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype) |
| 113 | + |
| 114 | + gradient_checker.double_grad_check([data], |
| 115 | + out, |
| 116 | + x_init=[data_arr], |
| 117 | + place=place, |
| 118 | + eps=eps) |
| 119 | + fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True}) |
| 120 | + gradient_checker.double_grad_check_for_dygraph(self.sign_wrapper, |
| 121 | + [data], |
| 122 | + out, |
| 123 | + x_init=[data_arr], |
| 124 | + place=place) |
| 125 | + |
| 126 | + def test_grad(self): |
| 127 | + paddle.enable_static() |
| 128 | + places = [fluid.CPUPlace()] |
| 129 | + if core.is_compiled_with_cuda(): |
| 130 | + places.append(fluid.CUDAPlace(0)) |
| 131 | + for p in places: |
| 132 | + self.func(p) |
| 133 | + |
| 134 | + |
| 135 | +class TestSignTripleGradCheck(unittest.TestCase): |
| 136 | + |
| 137 | + def sign_wrapper(self, x): |
| 138 | + return paddle.sign(x[0]) |
| 139 | + |
| 140 | + @prog_scope() |
| 141 | + def func(self, place): |
| 142 | + # the shape of input variable should be clearly specified, not inlcude -1. |
| 143 | + eps = 0.005 |
| 144 | + dtype = np.float32 |
| 145 | + |
| 146 | + data = layers.data('data', [1, 4], False, dtype) |
| 147 | + data.persistable = True |
| 148 | + out = paddle.sign(data) |
| 149 | + data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype) |
| 150 | + |
| 151 | + gradient_checker.triple_grad_check([data], |
| 152 | + out, |
| 153 | + x_init=[data_arr], |
| 154 | + place=place, |
| 155 | + eps=eps) |
| 156 | + fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True}) |
| 157 | + gradient_checker.triple_grad_check_for_dygraph(self.sign_wrapper, |
| 158 | + [data], |
| 159 | + out, |
| 160 | + x_init=[data_arr], |
| 161 | + place=place) |
| 162 | + |
| 163 | + def test_grad(self): |
| 164 | + paddle.enable_static() |
| 165 | + places = [fluid.CPUPlace()] |
| 166 | + if core.is_compiled_with_cuda(): |
| 167 | + places.append(fluid.CUDAPlace(0)) |
| 168 | + for p in places: |
| 169 | + self.func(p) |
| 170 | + |
| 171 | + |
94 | 172 | if __name__ == "__main__": |
95 | 173 | paddle.enable_static() |
96 | 174 | unittest.main() |
0 commit comments