diff --git a/python/paddle/fluid/tests/unittests/test_cosine_similarity_api.py b/python/paddle/fluid/tests/unittests/test_cosine_similarity_api.py index fc4715c1ab7f37f691fcab853a37fcf4b9057adb..1e25613fa63da440f71f23841095f153e61735e9 100644 --- a/python/paddle/fluid/tests/unittests/test_cosine_similarity_api.py +++ b/python/paddle/fluid/tests/unittests/test_cosine_similarity_api.py @@ -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() diff --git a/python/paddle/nn/layer/common.py b/python/paddle/nn/layer/common.py index c4823298f203537cc3f7397c009a635c6f22683b..abe6d572604942c92aa8a7d384f8fdff3b5a5815 100644 --- a/python/paddle/nn/layer/common.py +++ b/python/paddle/nn/layer/common.py @@ -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)