未验证 提交 7165f484 编写于 作者: Z Zhong Hui 提交者: GitHub

change api name eps to epsilon for the pair_distance

change api name eps to epsilon for the pair_distance 
上级 dea41da7
...@@ -20,11 +20,11 @@ import numpy as np ...@@ -20,11 +20,11 @@ import numpy as np
import unittest import unittest
def pairwise_distance(x, y, p=2.0, eps=1e-6, keepdim=False): def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False):
return np.linalg.norm(x - y, ord=p, axis=1, keepdims=keepdim) return np.linalg.norm(x - y, ord=p, axis=1, keepdims=keepdim)
def test_static(x_np, y_np, p=2.0, eps=1e-6, keepdim=False): def test_static(x_np, y_np, p=2.0, epsilon=1e-6, keepdim=False):
prog = paddle.static.Program() prog = paddle.static.Program()
startup_prog = paddle.static.Program() startup_prog = paddle.static.Program()
...@@ -35,7 +35,7 @@ def test_static(x_np, y_np, p=2.0, eps=1e-6, keepdim=False): ...@@ -35,7 +35,7 @@ def test_static(x_np, y_np, p=2.0, eps=1e-6, keepdim=False):
x = paddle.data(name='x', shape=x_np.shape, dtype=x_np.dtype) x = paddle.data(name='x', shape=x_np.shape, dtype=x_np.dtype)
y = paddle.data(name='y', shape=y_np.shape, dtype=x_np.dtype) y = paddle.data(name='y', shape=y_np.shape, dtype=x_np.dtype)
dist = paddle.nn.layer.distance.PairwiseDistance( dist = paddle.nn.layer.distance.PairwiseDistance(
p=p, eps=eps, keepdim=keepdim) p=p, epsilon=epsilon, keepdim=keepdim)
distance = dist(x, y) distance = dist(x, y)
exe = paddle.static.Executor(place) exe = paddle.static.Executor(place)
static_ret = exe.run(prog, static_ret = exe.run(prog,
...@@ -46,12 +46,12 @@ def test_static(x_np, y_np, p=2.0, eps=1e-6, keepdim=False): ...@@ -46,12 +46,12 @@ def test_static(x_np, y_np, p=2.0, eps=1e-6, keepdim=False):
return static_ret return static_ret
def test_dygraph(x_np, y_np, p=2.0, eps=1e-6, keepdim=False): def test_dygraph(x_np, y_np, p=2.0, epsilon=1e-6, keepdim=False):
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(x_np) x = paddle.to_variable(x_np)
y = paddle.to_variable(y_np) y = paddle.to_variable(y_np)
dist = paddle.nn.layer.distance.PairwiseDistance( dist = paddle.nn.layer.distance.PairwiseDistance(
p=p, eps=eps, keepdim=keepdim) p=p, epsilon=epsilon, keepdim=keepdim)
distance = dist(x, y) distance = dist(x, y)
dygraph_ret = distance.numpy() dygraph_ret = distance.numpy()
paddle.enable_static() paddle.enable_static()
......
...@@ -34,7 +34,7 @@ class PairwiseDistance(layers.Layer): ...@@ -34,7 +34,7 @@ class PairwiseDistance(layers.Layer):
Parameters: Parameters:
p (float): The order of norm. The default value is 2. p (float): The order of norm. The default value is 2.
eps (float, optional): Add small value to avoid division by zero, epsilon (float, optional): Add small value to avoid division by zero,
default value is 1e-6. default value is 1e-6.
keepdim (bool, optional): Whether to reserve the reduced dimension keepdim (bool, optional): Whether to reserve the reduced dimension
in the output Tensor. The result tensor is one dimension less than in the output Tensor. The result tensor is one dimension less than
...@@ -66,21 +66,21 @@ class PairwiseDistance(layers.Layer): ...@@ -66,21 +66,21 @@ class PairwiseDistance(layers.Layer):
""" """
def __init__(self, p=2., eps=1e-6, keepdim=False, name=None): def __init__(self, p=2., epsilon=1e-6, keepdim=False, name=None):
super(PairwiseDistance, self).__init__() super(PairwiseDistance, self).__init__()
self.p = p self.p = p
self.eps = eps self.epsilon = epsilon
self.keepdim = keepdim self.keepdim = keepdim
self.name = name self.name = name
check_type(self.p, 'porder', (float, int), 'PairwiseDistance') check_type(self.p, 'porder', (float, int), 'PairwiseDistance')
check_type(self.eps, 'epsilon', (float), 'PairwiseDistance') check_type(self.epsilon, 'epsilon', (float), 'PairwiseDistance')
check_type(self.keepdim, 'keepdim', (bool), 'PairwiseDistance') check_type(self.keepdim, 'keepdim', (bool), 'PairwiseDistance')
def forward(self, x, y): def forward(self, x, y):
if in_dygraph_mode(): if in_dygraph_mode():
sub = core.ops.elementwise_sub(x, y) sub = core.ops.elementwise_sub(x, y)
return core.ops.p_norm(sub, 'axis', 1, 'porder', self.p, 'keepdim', return core.ops.p_norm(sub, 'axis', 1, 'porder', self.p, 'keepdim',
self.keepdim, 'epsilon', self.eps) self.keepdim, 'epsilon', self.epsilon)
check_variable_and_dtype(x, 'x', ['float32', 'float64'], check_variable_and_dtype(x, 'x', ['float32', 'float64'],
'PairwiseDistance') 'PairwiseDistance')
...@@ -88,15 +88,14 @@ class PairwiseDistance(layers.Layer): ...@@ -88,15 +88,14 @@ class PairwiseDistance(layers.Layer):
'PairwiseDistance') 'PairwiseDistance')
sub = paddle.elementwise_sub(x, y) sub = paddle.elementwise_sub(x, y)
helper = LayerHelper("p_norm", name=self.name) helper = LayerHelper("PairwiseDistance", name=self.name)
attrs = { attrs = {
'axis': 1, 'axis': 1,
'porder': self.p, 'porder': self.p,
'keepdim': self.keepdim, 'keepdim': self.keepdim,
'epsilon': self.eps, 'epsilon': self.epsilon,
} }
out = helper.create_variable_for_type_inference( out = helper.create_variable_for_type_inference(dtype=x.dtype)
dtype=self._helper.input_dtype(x))
helper.append_op( helper.append_op(
type='p_norm', inputs={'X': sub}, outputs={'Out': out}, attrs=attrs) type='p_norm', inputs={'X': sub}, outputs={'Out': out}, attrs=attrs)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册