25
25
26
26
27
27
class TestQrOp (OpTest ):
28
+
28
29
def setUp (self ):
29
30
paddle .enable_static ()
30
- np .random .seed (4 )
31
+ np .random .seed (7 )
31
32
self .op_type = "qr"
32
33
a , q , r = self .get_input_and_output ()
33
34
self .inputs = {"X" : a }
@@ -74,30 +75,37 @@ def test_check_output(self):
74
75
self .check_output ()
75
76
76
77
def test_check_grad_normal (self ):
77
- self .check_grad (['X' ], ['Q' , 'R' ])
78
+ self .check_grad (['X' ], ['Q' , 'R' ],
79
+ numeric_grad_delta = 1e-5 ,
80
+ max_relative_error = 1e-6 )
78
81
79
82
80
83
class TestQrOpCase1 (TestQrOp ):
84
+
81
85
def get_shape (self ):
82
86
return (10 , 12 )
83
87
84
88
85
89
class TestQrOpCase2 (TestQrOp ):
90
+
86
91
def get_shape (self ):
87
92
return (16 , 15 )
88
93
89
94
90
95
class TestQrOpCase3 (TestQrOp ):
96
+
91
97
def get_shape (self ):
92
98
return (2 , 12 , 16 )
93
99
94
100
95
101
class TestQrOpCase4 (TestQrOp ):
102
+
96
103
def get_shape (self ):
97
104
return (3 , 16 , 15 )
98
105
99
106
100
107
class TestQrOpCase5 (TestQrOp ):
108
+
101
109
def get_mode (self ):
102
110
return "complete"
103
111
@@ -106,6 +114,7 @@ def get_shape(self):
106
114
107
115
108
116
class TestQrOpCase6 (TestQrOp ):
117
+
109
118
def get_mode (self ):
110
119
return "complete"
111
120
@@ -114,8 +123,10 @@ def get_shape(self):
114
123
115
124
116
125
class TestQrAPI (unittest .TestCase ):
126
+
117
127
def test_dygraph (self ):
118
128
paddle .disable_static ()
129
+ np .random .seed (7 )
119
130
120
131
def run_qr_dygraph (shape , mode , dtype ):
121
132
if dtype == "float32" :
@@ -174,12 +185,13 @@ def run_qr_dygraph(shape, mode, dtype):
174
185
]
175
186
modes = ["reduced" , "complete" , "r" ]
176
187
dtypes = ["float32" , "float64" ]
177
- for tensor_shape , mode , dtype in itertools .product (tensor_shapes , modes ,
178
- dtypes ):
188
+ for tensor_shape , mode , dtype in itertools .product (
189
+ tensor_shapes , modes , dtypes ):
179
190
run_qr_dygraph (tensor_shape , mode , dtype )
180
191
181
192
def test_static (self ):
182
193
paddle .enable_static ()
194
+ np .random .seed (7 )
183
195
184
196
def run_qr_static (shape , mode , dtype ):
185
197
if dtype == "float32" :
@@ -216,29 +228,27 @@ def run_qr_static(shape, mode, dtype):
216
228
tmp_q , tmp_r = np .linalg .qr (a [coord ], mode = mode )
217
229
np_q [coord ] = tmp_q
218
230
np_r [coord ] = tmp_r
219
- x = paddle .fluid .data (
220
- name = "input" , shape = shape , dtype = dtype )
231
+ x = paddle .fluid .data (name = "input" ,
232
+ shape = shape ,
233
+ dtype = dtype )
221
234
if mode == "r" :
222
235
r = paddle .linalg .qr (x , mode = mode )
223
236
exe = fluid .Executor (place )
224
237
fetches = exe .run (fluid .default_main_program (),
225
238
feed = {"input" : a },
226
239
fetch_list = [r ])
227
- self .assertTrue (
228
- np .allclose (
229
- fetches [0 ], np_r , atol = 1e-5 ))
240
+ self .assertTrue (np .allclose (fetches [0 ], np_r ,
241
+ atol = 1e-5 ))
230
242
else :
231
243
q , r = paddle .linalg .qr (x , mode = mode )
232
244
exe = fluid .Executor (place )
233
245
fetches = exe .run (fluid .default_main_program (),
234
246
feed = {"input" : a },
235
247
fetch_list = [q , r ])
236
- self .assertTrue (
237
- np .allclose (
238
- fetches [0 ], np_q , atol = 1e-5 ))
239
- self .assertTrue (
240
- np .allclose (
241
- fetches [1 ], np_r , atol = 1e-5 ))
248
+ self .assertTrue (np .allclose (fetches [0 ], np_q ,
249
+ atol = 1e-5 ))
250
+ self .assertTrue (np .allclose (fetches [1 ], np_r ,
251
+ atol = 1e-5 ))
242
252
243
253
tensor_shapes = [
244
254
(3 , 5 ),
@@ -253,8 +263,8 @@ def run_qr_static(shape, mode, dtype):
253
263
]
254
264
modes = ["reduced" , "complete" , "r" ]
255
265
dtypes = ["float32" , "float64" ]
256
- for tensor_shape , mode , dtype in itertools .product (tensor_shapes , modes ,
257
- dtypes ):
266
+ for tensor_shape , mode , dtype in itertools .product (
267
+ tensor_shapes , modes , dtypes ):
258
268
run_qr_static (tensor_shape , mode , dtype )
259
269
260
270
0 commit comments