From 26f466229888d517cafda8776556bacb290f171c Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Sun, 3 Apr 2022 00:12:09 +0800 Subject: [PATCH] add test --- .../unittests/test_sparse_activation_op.py | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py index df13ae4e4b7ff..fb82b76057d39 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py @@ -30,11 +30,33 @@ def test_sparse_relu(self): sparse_dim = 2 sparse_coo_x = dense_x.to_sparse_coo(sparse_dim) #TODO(zhangkaihuo): change to test the corresponding API: paddle.sparse.relu(sparse_coo_x) - sparse_act_out = _C_ops.final_state_sparse_relu(sparse_coo_x) + sparse_act_out = _C_ops.final_state_sparse_coo_relu(sparse_coo_x) correct_result = [0, 2, 0, 4, 5] actual_result = sparse_act_out.non_zero_elements().numpy() assert np.array_equal(correct_result, actual_result) + def test_sparse_coo_sqrt(self): + with _test_eager_guard(): + x = [[0, 4, 0, 2], [0, 0, 16, 0]] + dense_x = paddle.to_tensor(x, dtype='float32') + sparse_dim = 2 + sparse_coo_x = dense_x.to_sparse_coo(sparse_dim) + sparse_act_out = _C_ops.final_state_sparse_coo_sqrt(sparse_coo_x) + correct_result = [2, np.sqrt(2), 4] + actual_result = sparse_act_out.non_zero_elements().numpy() + assert np.array_equal(correct_result, actual_result) + + def test_sparse_csr_sqrt(self): + with _test_eager_guard(): + x = [[0, 4, 0, 2], [0, 0, 0, 0], [0, 0, 16, 0]] + dense_x = paddle.to_tensor(x, dtype='float32') + sparse_dim = 2 + sparse_coo_x = dense_x.to_sparse_csr(sparse_dim) + sparse_act_out = _C_ops.final_state_sparse_csr_sqrt(sparse_coo_x) + correct_result = [2, np.sqrt(2), 4] + actual_result = sparse_act_out.non_zero_elements().numpy() + assert np.array_equal(correct_result, actual_result) + if __name__ == "__main__": unittest.main()