未验证 提交 0d1a62e9 编写于 作者: L littletomatodonkey 提交者: GitHub

test=develop rename CosSim interface (#26522)

上级 dbf232a9
......@@ -116,6 +116,25 @@ class TestCosineSimilarityAPI(unittest.TestCase):
self.assertTrue(np.allclose(y.numpy(), np_out))
def test_dygraph_4(self):
paddle.disable_static()
shape1 = [23, 12, 1]
shape2 = [23, 1, 10]
axis = 2
eps = 1e-6
np.random.seed(1)
np_x1 = np.random.rand(*shape1).astype(np.float32)
np_x2 = np.random.rand(*shape2).astype(np.float32)
np_out = self._get_numpy_out(np_x1, np_x2, axis=axis, eps=eps)
cos_sim_func = nn.CosineSimilarity(axis=axis, eps=eps)
tesnor_x1 = paddle.to_variable(np_x1)
tesnor_x2 = paddle.to_variable(np_x2)
y = cos_sim_func(tesnor_x1, tesnor_x2)
self.assertTrue(np.allclose(y.numpy(), np_out))
if __name__ == '__main__':
unittest.main()
......@@ -913,10 +913,10 @@ class ReplicationPad3d(layers.Layer):
class CosineSimilarity(layers.Layer):
"""
This interface is used to compute cosine similarity between x1 and x2 along dim.
This interface is used to compute cosine similarity between x1 and x2 along axis.
Parameters:
dim (int): Dimension of vectors to compute cosine similarity. Default is 1.
axis (int): Dimension of vectors to compute cosine similarity. Default is 1.
eps(float): Small value to avoid division by zero. Default is 1e-8.
Returns:
None
......@@ -933,7 +933,7 @@ class CosineSimilarity(layers.Layer):
[0.9098952 0.15715368 0.8671125 0.3156102 ]
[0.4427798 0.54136837 0.5276275 0.32394758]
[0.3769419 0.8535014 0.48041078 0.9256797 ]]
dim = 1
axis = 1
eps = 1e-8
Out: [0.5275037 0.8368967 0.75037485 0.9245899]
......@@ -951,16 +951,16 @@ class CosineSimilarity(layers.Layer):
x1 = paddle.to_tensor(x1)
x2 = paddle.to_tensor(x2)
cos_sim_func = nn.CosineSimilarity(dim=0)
cos_sim_func = nn.CosineSimilarity(axis=0)
result = cos_sim_func(x1, x2)
print(result.numpy())
# [0.99806249 0.9817672 0.94987036]
"""
def __init__(self, dim=1, eps=1e-8):
def __init__(self, axis=1, eps=1e-8):
super(CosineSimilarity, self).__init__()
self._dim = dim
self._axis = axis
self._eps = eps
def forward(self, x1, x2):
return F.cosine_similarity(x1, x2, dim=self._dim, eps=self._eps)
return F.cosine_similarity(x1, x2, axis=self._axis, eps=self._eps)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册