Skip to content

Commit ddd729a

Browse files
committed
add logaddexp
1 parent 4f74656 commit ddd729a

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

python/paddle/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
from .tensor.math import add # noqa: F401
259259
from .tensor.math import subtract # noqa: F401
260260
from .tensor.math import logsumexp # noqa: F401
261+
from .tensor.math import logaddexp # noqa: F401
261262
from .tensor.math import inverse # noqa: F401
262263
from .tensor.math import log1p # noqa: F401
263264
from .tensor.math import erf # noqa: F401
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
import numpy as np
18+
19+
import paddle
20+
21+
22+
def ref_logaddexp_old(x, y):
23+
y = np.broadcast_to(y, x.shape)
24+
out = np.log(1 + np.exp(np.minimum(x, y) - np.maximum(x, y))) + np.maximum(
25+
x, y
26+
)
27+
return out
28+
29+
30+
def ref_logaddexp(x, y):
31+
return np.logaddexp(x, y)
32+
33+
34+
class TestLogsumexpAPI(unittest.TestCase):
35+
def setUp(self):
36+
self.place = (
37+
paddle.CUDAPlace(0)
38+
if paddle.fluid.core.is_compiled_with_cuda()
39+
else paddle.CPUPlace()
40+
)
41+
42+
def api_case(self):
43+
self.x = np.random.uniform(-1, 1, self.xshape).astype(self.dtype)
44+
self.y = np.random.uniform(-1, 1, self.yshape).astype(self.dtype)
45+
out_ref = ref_logaddexp(self.x, self.y)
46+
47+
# paddle.disable_static(self.place)
48+
x = paddle.to_tensor(self.x)
49+
y = paddle.to_tensor(self.y)
50+
out = paddle.logaddexp(x, y)
51+
np.testing.assert_allclose(out.numpy(), out_ref, atol=1e-06)
52+
53+
def test_api(self):
54+
self.xshape = [1, 2, 3, 4]
55+
self.yshape = [1, 2, 3, 4]
56+
self.dtype = np.float64
57+
self.api_case()
58+
59+
def test_api_broadcast(self):
60+
self.xshape = [1, 2, 3, 4]
61+
self.yshape = [1, 2, 3, 1]
62+
self.dtype = np.float32
63+
self.api_case()
64+
65+
def test_api_bigdata(self):
66+
self.xshape = [10, 200, 300]
67+
self.yshape = [10, 200, 300]
68+
self.dtype = np.float32
69+
self.api_case()
70+
71+
72+
if __name__ == '__main__':
73+
unittest.main()

python/paddle/tensor/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
from .math import subtract_ # noqa: F401
202202
from .math import atan2 # noqa: F401
203203
from .math import logsumexp # noqa: F401
204+
from .math import logaddexp # noqa: F401
204205
from .math import inverse # noqa: F401
205206
from .math import log2 # noqa: F401
206207
from .math import log10 # noqa: F401

python/paddle/tensor/math.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,64 @@ def add_(x, y, name=None):
627627
return _C_ops.add_(x, y)
628628

629629

630+
def logaddexp(x, y, name=None):
631+
"""
632+
Elementwise LogAddExp Operator.
633+
Add of exponentiations of the inputs
634+
The equation is:
635+
636+
.. math::
637+
638+
Out=log(X.exp()+Y.exp())
639+
640+
$X$ the tensor of any dimension.
641+
$Y$ the tensor whose dimensions must be less than or equal to the dimensions of $X$.
642+
643+
There are two cases for this operator:
644+
645+
1. The shape of $Y$ is the same with $X$.
646+
2. The shape of $Y$ is a continuous subsequence of $X$.
647+
648+
For case 2:
649+
650+
1. Broadcast $Y$ to match the shape of $X$, where axis is the start dimension index for broadcasting $Y$ onto $X$.
651+
2. If $axis$ is -1 (default), $axis$=rank($X$)-rank($Y$).
652+
3. The trailing dimensions of size 1 for $Y$ will be ignored for the consideration of subsequence, such as shape($Y$) = (2, 1) => (2).
653+
654+
For example:
655+
656+
.. code-block:: python
657+
658+
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
659+
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
660+
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
661+
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
662+
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
663+
shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
664+
665+
Args:
666+
x (Tensor): Tensor or LoDTensor of any dimensions. Its dtype should be float32, float64, float16.
667+
y (Tensor): Tensor or LoDTensor of any dimensions. Its dtype should be float32, float64, float16.
668+
name (string, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
669+
670+
Returns:
671+
N-D Tensor. A location into which the result is stored. It's dimension equals with x.
672+
673+
Examples:
674+
675+
.. code-block:: python
676+
677+
import paddle
678+
679+
x = paddle.to_tensor([-1, -2, -3], 'float64')
680+
y = paddle.to_tensor([-1], 'float64')
681+
z = paddle.logaddexp(x, y)
682+
print(z) # [-0.30685282, -0.68673831, -0.87307199]
683+
"""
684+
685+
return paddle.log1p(paddle.exp(-paddle.abs(x - y))) + paddle.maximum(x, y)
686+
687+
630688
def subtract(x, y, name=None):
631689
"""
632690
Substract two tensors element-wise. The equation is:

0 commit comments

Comments
 (0)