1818from numpy .linalg import multi_dot
1919from op_test import OpTest
2020import paddle
21- from paddle .fluid import Program , program_guard
22- import paddle .fluid as fluid
2321
2422paddle .enable_static ()
2523
@@ -196,44 +194,46 @@ def get_inputs_and_outputs(self):
196194#####python API test#######
197195class TestMultiDotOpError (unittest .TestCase ):
198196 def test_errors (self ):
199- with program_guard (Program (), Program ()):
197+ with paddle .static .program_guard (paddle .static .Program (),
198+ paddle .static .Program ()):
200199 # The inputs type of multi_dot must be list matrix.
201200 input1 = 12
202201 self .assertRaises (TypeError , paddle .multi_dot , [input1 , input1 ])
203202
204203 # The inputs dtype of multi_dot must be float64, float64 or float16.
205- input2 = fluid . layers .data (
204+ input2 = paddle . static .data (
206205 name = 'input2' , shape = [10 , 10 ], dtype = "int32" )
207206 self .assertRaises (TypeError , paddle .multi_dot , [input2 , input2 ])
208207
209208 # the number of tensor must be larger than 1
210- x0 = fluid .data (name = 'x0' , shape = [3 , 2 ], dtype = "float64" )
209+ x0 = paddle . static .data (name = 'x0' , shape = [3 , 2 ], dtype = "float64" )
211210 self .assertRaises (ValueError , paddle .multi_dot , [x0 ])
212211
213212 #the first tensor must be 1D or 2D
214- x1 = fluid .data (name = 'x1' , shape = [3 , 2 , 3 ], dtype = "float64" )
215- x2 = fluid .data (name = 'x2' , shape = [3 , 2 ], dtype = "float64" )
213+ x1 = paddle . static .data (name = 'x1' , shape = [3 , 2 , 3 ], dtype = "float64" )
214+ x2 = paddle . static .data (name = 'x2' , shape = [3 , 2 ], dtype = "float64" )
216215 self .assertRaises (ValueError , paddle .multi_dot , [x1 , x2 ])
217216
218217 #the last tensor must be 1D or 2D
219- x3 = fluid .data (name = 'x3' , shape = [3 , 2 ], dtype = "float64" )
220- x4 = fluid .data (name = 'x4' , shape = [3 , 2 , 2 ], dtype = "float64" )
218+ x3 = paddle . static .data (name = 'x3' , shape = [3 , 2 ], dtype = "float64" )
219+ x4 = paddle . static .data (name = 'x4' , shape = [3 , 2 , 2 ], dtype = "float64" )
221220 self .assertRaises (ValueError , paddle .multi_dot , [x3 , x4 ])
222221
223222 #the tensor must be 2D, except first and last tensor
224- x5 = fluid .data (name = 'x5' , shape = [3 , 2 ], dtype = "float64" )
225- x6 = fluid .data (name = 'x6' , shape = [2 ], dtype = "float64" )
226- x7 = fluid .data (name = 'x7' , shape = [2 , 2 ], dtype = "float64" )
223+ x5 = paddle . static .data (name = 'x5' , shape = [3 , 2 ], dtype = "float64" )
224+ x6 = paddle . static .data (name = 'x6' , shape = [2 ], dtype = "float64" )
225+ x7 = paddle . static .data (name = 'x7' , shape = [2 , 2 ], dtype = "float64" )
227226 self .assertRaises (ValueError , paddle .multi_dot , [x5 , x6 , x7 ])
228227
229228
230229class APITestMultiDot (unittest .TestCase ):
231230 def test_out (self ):
232- with fluid .program_guard (fluid .Program ()):
233- x0 = fluid .data (name = 'x0' , shape = [3 , 2 ], dtype = "float64" )
234- x1 = fluid .data (name = 'x1' , shape = [2 , 3 ], dtype = 'float64' )
231+ paddle .enable_static ()
232+ with paddle .static .program_guard (paddle .static .Program ()):
233+ x0 = paddle .static .data (name = 'x0' , shape = [3 , 2 ], dtype = "float64" )
234+ x1 = paddle .static .data (name = 'x1' , shape = [2 , 3 ], dtype = 'float64' )
235235 result = paddle .multi_dot ([x0 , x1 ])
236- exe = fluid . Executor (fluid .CPUPlace ())
236+ exe = paddle . static . Executor (paddle .CPUPlace ())
237237 data1 = np .random .rand (3 , 2 ).astype ("float64" )
238238 data2 = np .random .rand (2 , 3 ).astype ("float64" )
239239 np_res = exe .run (feed = {'x0' : data1 ,
@@ -248,14 +248,14 @@ def test_out(self):
248248 {}\n {}, check diff!" .format (np_res , expected_result ))
249249
250250 def test_dygraph_without_out (self ):
251- device = fluid . CPUPlace ()
252- with fluid . dygraph . guard ( device ):
253- input_array1 = np .random .rand (3 , 4 ).astype ("float64" )
254- input_array2 = np .random .rand (4 , 3 ).astype ("float64" )
255- data1 = fluid . dygraph . to_variable (input_array1 )
256- data2 = fluid . dygraph . to_variable (input_array2 )
257- out = paddle .multi_dot ([data1 , data2 ])
258- expected_result = np .linalg .multi_dot ([input_array1 , input_array2 ])
251+ paddle . disable_static ()
252+ device = paddle . CPUPlace ()
253+ input_array1 = np .random .rand (3 , 4 ).astype ("float64" )
254+ input_array2 = np .random .rand (4 , 3 ).astype ("float64" )
255+ data1 = paddle . to_tensor (input_array1 )
256+ data2 = paddle . to_tensor (input_array2 )
257+ out = paddle .multi_dot ([data1 , data2 ])
258+ expected_result = np .linalg .multi_dot ([input_array1 , input_array2 ])
259259 self .assertTrue (np .allclose (expected_result , out .numpy ()))
260260
261261
0 commit comments