diff --git a/framework/api/distribution/apibase.py b/framework/api/distribution/apibase.py index 58079a8213..3581c64e94 100644 --- a/framework/api/distribution/apibase.py +++ b/framework/api/distribution/apibase.py @@ -501,7 +501,7 @@ def compute_grad(self, res, data=None, **kwargs): shape = v.numpy().shape for i in range(len(v.numpy().flatten())): g = self._get_sigle_grad(v, i, k) - grad.append(g[0]) + grad.append(g.item()) self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) elif isinstance(v, (list, tuple)) and isinstance(v[0], paddle.Tensor): @@ -512,7 +512,7 @@ def compute_grad(self, res, data=None, **kwargs): shape = v[n].shape for i in range(len(v[n].flatten())): g = self._get_sigle_grad(v[n], i, k, n) - grad.append(g[0]) + grad.append(g.item()) self.kwargs[k][n] = v[n] tmp.append(np.array(grad).reshape(shape)) numeric_grad[k] = tmp @@ -532,7 +532,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/framework/api/fft/apibase.py b/framework/api/fft/apibase.py index 00ed9bca7f..3f9e8a7014 100755 --- a/framework/api/fft/apibase.py +++ b/framework/api/fft/apibase.py @@ -502,7 +502,7 @@ def compute_grad(self, res, data=None, **kwargs): loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap # print("-----> {}".format(g)) - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) @@ -521,7 +521,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/framework/api/incubate/apibase.py b/framework/api/incubate/apibase.py index 14f44ea531..4b1c5924fb 100755 --- a/framework/api/incubate/apibase.py +++ b/framework/api/incubate/apibase.py @@ -501,7 +501,7 @@ def compute_grad(self, res, data=None, **kwargs): shape = v.numpy().shape for i in range(len(v.numpy().flatten())): g = self._get_sigle_grad(v, i, k) - grad.append(g[0]) + grad.append(g.item()) self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) elif isinstance(v, (list, tuple)) and isinstance(v[0], paddle.Tensor): @@ -512,7 +512,7 @@ def compute_grad(self, res, data=None, **kwargs): shape = v[n].shape for i in range(len(v[n].flatten())): g = self._get_sigle_grad(v[n], i, k, n) - grad.append(g[0]) + grad.append(g.item()) self.kwargs[k][n] = v[n] tmp.append(np.array(grad).reshape(shape)) numeric_grad[k] = tmp @@ -532,7 +532,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/framework/api/linalg/apibase.py b/framework/api/linalg/apibase.py index 75843cdc8f..9e2571598c 100755 --- a/framework/api/linalg/apibase.py +++ b/framework/api/linalg/apibase.py @@ -498,7 +498,7 @@ def compute_grad(self, res, data=None, **kwargs): loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap # print("-----> {}".format(g)) - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) @@ -517,7 +517,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/framework/api/linalg/test_cond.py b/framework/api/linalg/test_cond.py index 77291c3855..e536c29490 100644 --- a/framework/api/linalg/test_cond.py +++ b/framework/api/linalg/test_cond.py @@ -37,7 +37,7 @@ def test_cond_base(): base """ x = np.array([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) - res = np.array([np.linalg.cond(x)]) + res = np.linalg.cond(x) obj.base(res=res, x=x) @@ -48,7 +48,7 @@ def test_cond0(): """ x = randtool("float", -2, 4, [3, 4]) # x = np.random.rand(3, 4) - res = np.array([np.linalg.cond(x)]) + res = np.linalg.cond(x) # print(res) obj.run(res=res, x=x) @@ -59,7 +59,7 @@ def test_cond1(): m!=n, p=-2 """ x = randtool("float", -2, 4, [6, 4]) - res = np.array([np.linalg.cond(x, p=-2)]) + res = np.linalg.cond(x, p=-2) obj.run(res=res, x=x, p=-2) @@ -70,7 +70,7 @@ def test_cond2(): x: multiple dimension """ x = randtool("float", -20, 40, [6, 2, 4, 3, 4]) - res = np.array(np.linalg.cond(x)) + res = np.linalg.cond(x) obj.run(res=res, x=x) @@ -80,7 +80,7 @@ def test_cond3(): x: n*n; p=2 """ x = randtool("float", -20, 40, [4, 4]) - res = np.array([np.linalg.cond(x)]) + res = np.linalg.cond(x) obj.run(res=res, x=x) @@ -90,7 +90,7 @@ def test_cond4(): x: n*n; p=-2 """ x = randtool("float", -2, 40, [4, 4]) - res = np.array([np.linalg.cond(x, p=-2)]) + res = np.linalg.cond(x, p=-2) obj.run(res=res, x=x, p=-2) @@ -100,7 +100,7 @@ def test_cond5(): x: n*n; p=-2 """ x = randtool("float", -2, 40, [4, 4]) - res = np.array([np.linalg.cond(x, p=-2)]) + res = np.linalg.cond(x, p=-2) obj.run(res=res, x=x, p=-2) @@ -130,7 +130,7 @@ def test_cond8(): x: n*n; p=1 """ x = randtool("float", -2, 40, [4, 4]) - res = np.array([np.linalg.cond(x, p=1)]) + res = np.linalg.cond(x, p=1) obj.run(res=res, x=x, p=1) @@ -140,7 +140,7 @@ def test_cond9(): x: n*n; p=-1 """ x = randtool("float", -4, 4, [4, 2, 4, 4]) - res = np.array(np.linalg.cond(x, p=-1)) + res = np.linalg.cond(x, p=-1) obj.run(res=res, x=x, p=-1) @@ -150,7 +150,7 @@ def test_cond10(): x: n*n; p=inf """ x = randtool("float", -4, 4, [4, 2, 4, 4]) - res = np.array(np.linalg.cond(x, p=np.inf)) + res = np.linalg.cond(x, p=np.inf) obj.run(res=res, x=x, p=np.inf) @@ -160,5 +160,5 @@ def test_cond11(): x: n*n; p=-inf """ x = randtool("float", -4, 4, [4, 2, 4, 4]) - res = np.array(np.linalg.cond(x, p=-np.inf)) + res = np.linalg.cond(x, p=-np.inf) obj.run(res=res, x=x, p=-np.inf) diff --git a/framework/api/linalg/test_multi_dot.py b/framework/api/linalg/test_multi_dot.py index ac409c4e67..57a38bf70e 100644 --- a/framework/api/linalg/test_multi_dot.py +++ b/framework/api/linalg/test_multi_dot.py @@ -98,7 +98,7 @@ def numerical_grad(**kwargs): kwargs[k] = tmp loss_delta = cal_loss(**kwargs) g = (loss_delta - loss) / gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) diff --git a/framework/api/linalg/test_norm.py b/framework/api/linalg/test_norm.py index 2147ee286e..71343ec591 100755 --- a/framework/api/linalg/test_norm.py +++ b/framework/api/linalg/test_norm.py @@ -91,7 +91,7 @@ def test_norm_base(): base """ x = randtool("float", -10, 10, [2, 3, 4]) - res = [28.43878906] + res = np.array(28.43878906) obj.base(res=res, x=x) @@ -102,7 +102,7 @@ def test_norm(): """ np.random.seed(33) x = randtool("float", -10, 10, [3, 3, 3]) - res = [31.5736] + res = np.array(31.5736) obj.run(res=res, x=x, axis=None) @@ -222,7 +222,7 @@ def test_norm10(): x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) pord = "fro" axis = None - res = [16.8819] + res = np.array(16.8819) obj.run(res=res, x=x, axis=axis, p=pord) @@ -234,7 +234,7 @@ def test_norm11(): x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(3, 3) pord = "fro" axis = None - res = [16.8819] + res = np.array(16.8819) obj.run(res=res, x=x, axis=axis, p=pord) diff --git a/framework/api/loss/apibase.py b/framework/api/loss/apibase.py index 37ae7cafa8..7bb20bf836 100755 --- a/framework/api/loss/apibase.py +++ b/framework/api/loss/apibase.py @@ -502,7 +502,7 @@ def compute_grad(self, res, data=None, **kwargs): loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap # print("-----> {}".format(g)) - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) @@ -521,7 +521,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/framework/api/nn/apibase.py b/framework/api/nn/apibase.py index 6a9b1d0a9e..41f46d08e5 100755 --- a/framework/api/nn/apibase.py +++ b/framework/api/nn/apibase.py @@ -502,7 +502,7 @@ def compute_grad(self, res, data=None, **kwargs): loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap # print("-----> {}".format(g)) - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) @@ -521,7 +521,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/framework/api/paddlebase/apibase.py b/framework/api/paddlebase/apibase.py index 1ab2f09f75..aff9a28913 100755 --- a/framework/api/paddlebase/apibase.py +++ b/framework/api/paddlebase/apibase.py @@ -502,7 +502,7 @@ def compute_grad(self, res, data=None, **kwargs): loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap # print("-----> {}".format(g)) - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) @@ -521,7 +521,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/framework/api/paddlebase/test_all.py b/framework/api/paddlebase/test_all.py index 729c74ba49..ec307f9c1c 100644 --- a/framework/api/paddlebase/test_all.py +++ b/framework/api/paddlebase/test_all.py @@ -37,7 +37,7 @@ def test_all0(): default """ x = np.random.randint(-4, 4, (10,)) - res = np.array([np.all(x)]) + res = np.all(x) obj.base(res=res, x=x) @@ -47,7 +47,7 @@ def test_all1(): x: 2d-tensor """ x = np.random.randint(-4, 4, (10, 10)) - res = np.array([np.all(x)]) + res = np.all(x) obj.base(res=res, x=x) @@ -57,7 +57,7 @@ def test_all2(): x: 3d-tensor """ x = np.random.randint(-4, 4, (3, 4, 2)) - res = np.array([np.all(x)]) + res = np.all(x) obj.base(res=res, x=x) @@ -67,7 +67,7 @@ def test_all3(): x: 4d-tensor """ x = np.random.randint(-4, 4, (2, 4, 4, 2)) - res = np.array([np.all(x)]) + res = np.all(x) obj.base(res=res, x=x) diff --git a/framework/api/paddlebase/test_any.py b/framework/api/paddlebase/test_any.py index b4da059d40..a8262b7d98 100644 --- a/framework/api/paddlebase/test_any.py +++ b/framework/api/paddlebase/test_any.py @@ -37,7 +37,7 @@ def test_any0(): default """ x = np.random.randint(-4, 4, (10,)) - res = np.array([np.any(x)]) + res = np.any(x) obj.base(res=res, x=x) @@ -47,7 +47,7 @@ def test_any1(): x: 2d-tensor """ x = np.random.randint(-4, 4, (10, 10)) - res = np.array([np.any(x)]) + res = np.any(x) obj.base(res=res, x=x) @@ -57,7 +57,7 @@ def test_any2(): x: 3d-tensor """ x = np.random.randint(-4, 4, (3, 4, 2)) - res = np.array([np.any(x)]) + res = np.any(x) obj.base(res=res, x=x) @@ -67,7 +67,7 @@ def test_any3(): x: 4d-tensor """ x = np.random.randint(-4, 4, (2, 4, 4, 2)) - res = np.array([np.any(x)]) + res = np.any(x) obj.base(res=res, x=x) diff --git a/framework/api/paddlebase/test_max.py b/framework/api/paddlebase/test_max.py index 42aaeece81..d97b63cac0 100644 --- a/framework/api/paddlebase/test_max.py +++ b/framework/api/paddlebase/test_max.py @@ -37,7 +37,7 @@ def test_max_base(): max_base """ x_data = np.arange(6).reshape(2, 3).astype(np.float32) - res = np.array([5]) + res = np.max(x_data) obj.base(res=res, x=x_data) @@ -47,7 +47,7 @@ def test_max_2D_tensor(): max_2D_tensor """ x_data = np.arange(6).reshape(2, 3).astype(np.float32) - res = np.array([5]) + res = np.max(x_data) obj.run(res=res, x=x_data) @@ -112,5 +112,5 @@ def test_max_1(): special input """ x_data = np.array([[-1.00595951, -0.20009832], [-0.35623679, -0.95880121]]) - res = np.array([-0.20009832]) + res = np.max(x_data) obj.run(res=res, x=x_data, axis=[-2, 1], keepdim=False) diff --git a/framework/api/paddlebase/test_min.py b/framework/api/paddlebase/test_min.py index 52350dceb1..7e36015cef 100644 --- a/framework/api/paddlebase/test_min.py +++ b/framework/api/paddlebase/test_min.py @@ -37,7 +37,7 @@ def test_min_base(): min_base """ x_data = np.arange(6).reshape(2, 3).astype(np.float32) - res = np.array([0]) + res = np.min(x_data) obj.base(res=res, x=x_data) @@ -47,7 +47,7 @@ def test_min_2D_tensor(): min_2D_tensor """ x_data = np.arange(6).reshape(2, 3).astype(np.float32) - res = np.array([0]) + res = np.min(x_data) obj.run(res=res, x=x_data) @@ -99,7 +99,7 @@ def test_min_1(): special input """ x_data = np.array([[-1.00595951, -0.20009832], [-0.35623679, -0.95880121]]) - res = np.array([-1.00595951]) + res = np.min(x_data) obj.run(res=res, x=x_data, axis=[-2, 1], keepdim=False) diff --git a/framework/api/paddlebase/test_prod.py b/framework/api/paddlebase/test_prod.py index c9972b9af8..17bd06e72f 100644 --- a/framework/api/paddlebase/test_prod.py +++ b/framework/api/paddlebase/test_prod.py @@ -34,7 +34,7 @@ def test_prod_base(): axis=None """ x = np.array([[0.8, 0.4], [0.7, 0.9]]) - res = [np.prod(x)] + res = np.prod(x) obj.base(res=res, x=x) @@ -56,7 +56,7 @@ def test_prod2(): """ x = np.array([[0.8, 0.4], [0.7, 0.9]]) axis = 3 - obj.exception(mode="c", etype="InvalidArgumentError", x=x, axis=axis) + obj.exception(mode="c", etype="InvalidArgument", x=x, axis=axis) @pytest.mark.api_base_prod_parameters @@ -77,7 +77,7 @@ def test_prod4(): """ x = np.array([[0.8, 0.4], [0.7, 0.9]]) axis = [0, 1] - res = [0.2016] + res = np.array(0.2016) obj.run(res=res, x=x, axis=axis) @@ -88,7 +88,7 @@ def test_prod5(): """ x = np.array([[0.8, 0.4], [0.7, 0.9]]) axis = (0, 1) - res = [0.2016] + res = np.array(0.2016) obj.run(res=res, x=x, axis=axis) @@ -98,7 +98,7 @@ def test_prod6(): dtype=float32 """ x = np.array([[0.8, 0.4], [0.7, 0.9]]) - res = [np.prod(x)] + res = np.prod(x) obj.base(res=res, x=x, dtype="float32") @@ -134,7 +134,7 @@ def test_prod8(): dtype=int64 """ x = np.array([[8, 4], [7, 9]]) - res = [np.prod(x)] + res = np.prod(x) obj1.base(res=res, x=x, dtype="int64") @@ -144,7 +144,7 @@ def test_prod9(): input is int32, int64 """ x = np.array([[3, 5], [6, 2]]) - res = [np.prod(x)] + res = np.prod(x) obj1.run(res=res, x=x) diff --git a/models/PaddleScience/api/apibase.py b/models/PaddleScience/api/apibase.py index 5078663ca6..4d15c7b5f0 100644 --- a/models/PaddleScience/api/apibase.py +++ b/models/PaddleScience/api/apibase.py @@ -504,7 +504,7 @@ def compute_grad(self, res, data=None, **kwargs): loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap # print("-----> {}".format(g)) - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) @@ -523,7 +523,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape) diff --git a/tools/test/test-tools/tool-test-op-correctness/tpaddle/cases/apibase.py b/tools/test/test-tools/tool-test-op-correctness/tpaddle/cases/apibase.py index c920d00c5e..f61a292ac8 100755 --- a/tools/test/test-tools/tool-test-op-correctness/tpaddle/cases/apibase.py +++ b/tools/test/test-tools/tool-test-op-correctness/tpaddle/cases/apibase.py @@ -475,7 +475,7 @@ def compute_grad(self, res, data=None, **kwargs): loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap # print("-----> {}".format(g)) - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.kwargs[k] = v numeric_grad[k] = np.array(grad).reshape(shape) @@ -493,7 +493,7 @@ def compute_grad(self, res, data=None, **kwargs): self.data.stop_gradient = False loss_delta = self._numeric_grad() g = (loss_delta - loss) / self.gap - grad.append(g[0]) + grad.append(g.item()) # recover v to self.kwargs self.data = data numeric_grad["data"] = np.array(grad).reshape(shape)