From 612b81c54b478f2950e2ae2a3163cb3daa17485d Mon Sep 17 00:00:00 2001 From: ccrrong <101700995+ccrrong@users.noreply.github.com> Date: Wed, 30 Nov 2022 15:51:50 +0800 Subject: [PATCH] remove cos_sim (#48501) --- python/paddle/fluid/layers/nn.py | 38 ------------------- .../tests/book/test_recommender_system.py | 4 +- .../tests/unittests/dist_fleet_simnet_bow.py | 4 +- .../dygraph_to_static/simnet_dygraph_model.py | 2 +- .../fluid/tests/unittests/test_cos_sim_op.py | 21 ---------- .../tests/unittests/test_dist_fleet_ps.py | 4 +- .../tests/unittests/test_dist_fleet_ps11.py | 4 +- .../tests/unittests/test_dist_fleet_ps12.py | 4 +- .../tests/unittests/test_dist_fleet_ps13.py | 4 +- .../tests/unittests/test_dist_fleet_ps2.py | 4 +- .../tests/unittests/test_dist_fleet_ps3.py | 4 +- .../tests/unittests/test_dist_fleet_ps4.py | 4 +- .../tests/unittests/test_dist_fleet_ps5.py | 4 +- .../tests/unittests/test_dist_fleet_ps6.py | 4 +- 14 files changed, 24 insertions(+), 81 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 911ac5d74a..dbac69df68 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -67,7 +67,6 @@ __all__ = [ 'embedding', 'linear_chain_crf', 'crf_decoding', - 'cos_sim', 'conv2d', 'softmax', 'pool2d', @@ -1005,43 +1004,6 @@ def crf_decoding(input, param_attr, label=None, length=None): return viterbi_path -@templatedoc() -def cos_sim(X, Y): - """ - ${comment} - - Args: - X (Tensor): ${x_comment}. - Y (Tensor): ${y_comment}. - - Returns: - A Tensor representing the output of cosine(X, Y). - - Examples: - .. code-block:: python - - import paddle - - x = paddle.rand(shape=[3, 7], dtype='float32') - y = paddle.rand(shape=[1, 7], dtype='float32') - out = paddle.fluid.layers.cos_sim(x, y) - print(out) - - """ - check_variable_and_dtype(X, 'X', ['float32'], 'cos_sim') - check_variable_and_dtype(Y, 'Y', ['float32'], 'cos_sim') - helper = LayerHelper('cos_sim', **locals()) - out = helper.create_variable_for_type_inference(dtype=X.dtype) - xnorm = helper.create_variable_for_type_inference(dtype=X.dtype) - ynorm = helper.create_variable_for_type_inference(dtype=X.dtype) - helper.append_op( - type='cos_sim', - inputs={'X': [X], 'Y': [Y]}, - outputs={'Out': [out], 'XNorm': [xnorm], 'YNorm': [ynorm]}, - ) - return out - - @deprecated(since="2.0.0", update_to="paddle.nn.functional.dropout") def dropout( x, diff --git a/python/paddle/fluid/tests/book/test_recommender_system.py b/python/paddle/fluid/tests/book/test_recommender_system.py index c270c87b3c..3aacd377dc 100644 --- a/python/paddle/fluid/tests/book/test_recommender_system.py +++ b/python/paddle/fluid/tests/book/test_recommender_system.py @@ -161,7 +161,9 @@ def model(): mov_combined_features = get_mov_combined_features() # need cos sim - inference = layers.cos_sim(X=usr_combined_features, Y=mov_combined_features) + inference = paddle.nn.functional.cosine_similarity( + x1=usr_combined_features, x2=mov_combined_features + ) scale_infer = paddle.scale(x=inference, scale=5.0) label = layers.data(name='score', shape=[1], dtype='float32') diff --git a/python/paddle/fluid/tests/unittests/dist_fleet_simnet_bow.py b/python/paddle/fluid/tests/unittests/dist_fleet_simnet_bow.py index eb128f9be7..2c16fff90a 100644 --- a/python/paddle/fluid/tests/unittests/dist_fleet_simnet_bow.py +++ b/python/paddle/fluid/tests/unittests/dist_fleet_simnet_bow.py @@ -192,8 +192,8 @@ def train_network( ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py index 10e4b9d85e..f236faccdc 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/simnet_dygraph_model.py @@ -136,7 +136,7 @@ class CosSimLayer: """ operation """ - sim = fluid.layers.cos_sim(x, y) + sim = paddle.nn.functional.cosine_similarity(x, y) return sim diff --git a/python/paddle/fluid/tests/unittests/test_cos_sim_op.py b/python/paddle/fluid/tests/unittests/test_cos_sim_op.py index 15d1d1e75d..f9c761c9ee 100644 --- a/python/paddle/fluid/tests/unittests/test_cos_sim_op.py +++ b/python/paddle/fluid/tests/unittests/test_cos_sim_op.py @@ -17,9 +17,6 @@ import unittest import numpy as np from op_test import OpTest -import paddle.fluid as fluid -from paddle.fluid import Program, program_guard - class TestCosSimOp(OpTest): def setUp(self): @@ -121,23 +118,5 @@ class TestCosSimOp4(TestCosSimOp): } -class TestCosSimOpError(unittest.TestCase): - def test_errors(self): - with program_guard(Program(), Program()): - # the input of batch_norm must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() - ) - x2 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() - ) - self.assertRaises(TypeError, fluid.layers.cos_sim, x1, x2) - - # the input dtype of batch_norm must be float32 - x3 = fluid.layers.data(name='x3', shape=[3, 4, 5, 6], dtype="int32") - x4 = fluid.layers.data(name='x4', shape=[3, 4, 5, 6], dtype="int64") - self.assertRaises(TypeError, fluid.layers.cos_sim, x3, x4) - - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps.py index 685f8dd0e4..f97e1d6c3c 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps.py @@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps11.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps11.py index cea69f92e6..5e94a1949c 100755 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps11.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps11.py @@ -150,8 +150,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps12.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps12.py index f563354546..5793777738 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps12.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps12.py @@ -153,8 +153,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps13.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps13.py index 107488e771..a1eb88f8ff 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps13.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps13.py @@ -160,8 +160,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps2.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps2.py index 2afaa2bf47..609611bd24 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps2.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps2.py @@ -160,8 +160,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps3.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps3.py index 9cc5b947f6..a2192b16f3 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps3.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps3.py @@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps4.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps4.py index 751f0c70b8..d4197129df 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps4.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps4.py @@ -156,8 +156,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps5.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps5.py index 0b00a97a34..deb6d97cd4 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps5.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps5.py @@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps6.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps6.py index f90b08c3f3..581ea0e0bc 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_ps6.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_ps6.py @@ -156,8 +156,8 @@ class TestPSPassWithBow(unittest.TestCase): ), bias_attr=fluid.ParamAttr(name="__fc_b__"), ) - cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) - cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) + cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) + cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) # loss avg_cost = get_loss(cos_q_pt, cos_q_nt) # acc -- GitLab