class TestTraceAPICase(unittest.TestCase): def test_case1(self): case = np.random.randn(2, 20, 2, 3).astype('float32') data1 = fluid.data(name='data1', shape=[2, 20, 2, 3], dtype='float32') out1 = tensor.trace(data1) out2 = tensor.trace(data1, offset=-5, dim1=1, dim2=-1) place = core.CPUPlace() exe = fluid.Executor(place) results = exe.run(fluid.default_main_program(), feed={"data1": case}, fetch_list=[out1, out2], return_numpy=True) target1 = np.trace(case) target2 = np.trace(case, offset=-5, axis1=1, axis2=-1) self.assertTrue(np.allclose(results[0], target1)) self.assertTrue(np.allclose(results[1], target2))
def test_basic_api(self): for dtype in self._dtypes: input = rand([ 2, 20, 2, 3 ]).astype(dtype) + 1j * rand([2, 20, 2, 3]).astype(dtype) for place in self._places: with dg.guard(place): var_x = dg.to_variable(input) result = tensor.trace(var_x, offset=1, axis1=0, axis2=2).numpy() target = np.trace(input, offset=1, axis1=0, axis2=2) self.assertTrue(np.allclose(result, target))