未验证 提交 612b81c5 编写于 作者: C ccrrong 提交者: GitHub

remove cos_sim (#48501)

上级 e9b4c6e7
...@@ -67,7 +67,6 @@ __all__ = [ ...@@ -67,7 +67,6 @@ __all__ = [
'embedding', 'embedding',
'linear_chain_crf', 'linear_chain_crf',
'crf_decoding', 'crf_decoding',
'cos_sim',
'conv2d', 'conv2d',
'softmax', 'softmax',
'pool2d', 'pool2d',
...@@ -1005,43 +1004,6 @@ def crf_decoding(input, param_attr, label=None, length=None): ...@@ -1005,43 +1004,6 @@ def crf_decoding(input, param_attr, label=None, length=None):
return viterbi_path 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") @deprecated(since="2.0.0", update_to="paddle.nn.functional.dropout")
def dropout( def dropout(
x, x,
......
...@@ -161,7 +161,9 @@ def model(): ...@@ -161,7 +161,9 @@ def model():
mov_combined_features = get_mov_combined_features() mov_combined_features = get_mov_combined_features()
# need cos sim # 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) scale_infer = paddle.scale(x=inference, scale=5.0)
label = layers.data(name='score', shape=[1], dtype='float32') label = layers.data(name='score', shape=[1], dtype='float32')
......
...@@ -192,8 +192,8 @@ def train_network( ...@@ -192,8 +192,8 @@ def train_network(
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -136,7 +136,7 @@ class CosSimLayer: ...@@ -136,7 +136,7 @@ class CosSimLayer:
""" """
operation operation
""" """
sim = fluid.layers.cos_sim(x, y) sim = paddle.nn.functional.cosine_similarity(x, y)
return sim return sim
......
...@@ -17,9 +17,6 @@ import unittest ...@@ -17,9 +17,6 @@ import unittest
import numpy as np import numpy as np
from op_test import OpTest from op_test import OpTest
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
class TestCosSimOp(OpTest): class TestCosSimOp(OpTest):
def setUp(self): def setUp(self):
...@@ -121,23 +118,5 @@ class TestCosSimOp4(TestCosSimOp): ...@@ -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__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -150,8 +150,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -150,8 +150,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -153,8 +153,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -153,8 +153,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -160,8 +160,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -160,8 +160,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -160,8 +160,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -160,8 +160,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -156,8 +156,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -156,8 +156,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -162,8 +162,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
...@@ -156,8 +156,8 @@ class TestPSPassWithBow(unittest.TestCase): ...@@ -156,8 +156,8 @@ class TestPSPassWithBow(unittest.TestCase):
), ),
bias_attr=fluid.ParamAttr(name="__fc_b__"), bias_attr=fluid.ParamAttr(name="__fc_b__"),
) )
cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
# loss # loss
avg_cost = get_loss(cos_q_pt, cos_q_nt) avg_cost = get_loss(cos_q_pt, cos_q_nt)
# acc # acc
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册