Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions python/paddle/tensor/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,8 +1776,7 @@ def cov(x, rowvar=True, ddof=True, fweights=None, aweights=None, name=None):
norm_factor = w_sum - (w * aweights).sum() / w_sum
else:
norm_factor = w_sum - ddof
if norm_factor <= 0:
norm_factor = paddle.to_tensor(0, dtype=nx.dtype)
norm_factor = paddle.clip(norm_factor, min=0)
nx = nx - avg.unsqueeze(1)
xxt = paddle.mm(nx, nx_w.t().conj())
cov = paddle.divide(xxt, norm_factor).squeeze()
Expand Down
15 changes: 8 additions & 7 deletions test/legacy_test/test_zero_dim_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5275,28 +5275,29 @@ def test_multi_dot(self):
self.assertEqual(res[2].shape, (4, 5))
self.assertEqual(res[3].shape, (5,))

@test_with_pir_api
@prog_scope()
def test_cov(self):
xt_1 = paddle.randn((12,))
xt_1.stop_gradient = False

out = paddle.linalg.cov(xt_1)
paddle.static.append_backward(out)

_, xt_1_grad = paddle.static.append_backward(
out, parameter_list=[xt_1]
)[0]
prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[out, xt_1.grad_name])
res = self.exe.run(prog, fetch_list=[out, xt_1_grad])
self.assertEqual(res[0].shape, ())
self.assertEqual(res[1].shape, (12,))

@test_with_pir_api
@prog_scope()
def test_corrcoef(self):
x = paddle.randn((12,))
x.stop_gradient = False
out = paddle.linalg.corrcoef(x)
paddle.static.append_backward(out)

_, x_grad = paddle.static.append_backward(out, parameter_list=[x])[0]
prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[out, x.grad_name])
res = self.exe.run(prog, fetch_list=[out, x_grad])
self.assertEqual(res[0].shape, ())
self.assertEqual(res[1].shape, (12,))

Expand Down