diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 911ac5d74a339e5ba7aa63a5b37936ec0a56b613..dbac69df682720a79fb5dd34bdfa3505707d5d96 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 c270c87b3c2acc44dce6d24b9722890d40ac6ee6..3aacd377dc0c7ebb76fe299e503c35d20629cf17 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 eb128f9be75fa564b7bfd5997733f01cae08854a..2c16fff90a59f9eec6d65f9135709756772ff8f8 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 10e4b9d85ec1612340b0655810f668cf6d6305e9..f236faccdc2df8a78e40ab96f7522a8dcb1b4621 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 15d1d1e75df84cf2e702071560d5e98166c805d4..f9c761c9eedf3f06f78f39c44b039608ed03522b 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 685f8dd0e4c10c0d37c436dae51410a9239be1cc..f97e1d6c3cff2dd9654dc463e55825bcbb8496c9 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 cea69f92e69213c92c73001662db80d9e4b9262f..5e94a1949c551fe1d679ecd22c0bed7a9809bb74 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 f56335454640a3dfa9b8394c5ff816a32644ffa4..57937777386202c5f0df3010b9ff2942e5132341 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 107488e771f3c4cb0f75ffe5e836e3a002d50e15..a1eb88f8ff86b3eeb2a42ca8c5c9b20d674353ac 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 2afaa2bf4725701b1176c4a121e25679141c9811..609611bd24e609fe9653d035623d642a5f167f18 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 9cc5b947f67268b3a02cea146d19434af6d9ad85..a2192b16f3e28f8e8d02c52e22c87d4684a93731 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 751f0c70b8da2ba84bf346391bfcbbd5401e1edd..d4197129df1f9a10fa5aa8621deab3131e8bff57 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 0b00a97a3449211c1f28fd705eff83325a814578..deb6d97cd465949f6f4203ce8f22409389483d29 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 f90b08c3f3387f1ddeef0e0a7e27bf8a710ad3ff..581ea0e0bc039a7cce3bd4f8caed41cc945ce960 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