Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ecd6a1e9
提交
ecd6a1e9
authored
6月 21, 2021
作者:
Ricardo
提交者:
Brandon T. Willard
6月 21, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add `logsumexp`
上级
89cfea01
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
63 行增加
和
0 行删除
+63
-0
math.py
aesara/tensor/math.py
+29
-0
test_math.py
tests/tensor/test_math.py
+34
-0
没有找到文件。
aesara/tensor/math.py
浏览文件 @
ecd6a1e9
...
@@ -2764,6 +2764,34 @@ def power(x, y):
...
@@ -2764,6 +2764,34 @@ def power(x, y):
return
x
**
y
return
x
**
y
def
logsumexp
(
x
,
axis
=
None
,
keepdims
=
False
):
"""Compute the log of the sum of exponentials of input elements.
See ``scipy.special.logsumexp``.
Parameters
----------
x : symbolic tensor
Input
axis : None or int or tuple of ints, optional
Axis or axes over which the sum is taken. By default axis is None,
and all elements are summed.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the
result as dimensions with size one. With this option, the result will
broadcast correctly against the original array.
Returns
-------
tensor
"""
return
log
(
sum
(
exp
(
x
),
axis
=
axis
,
keepdims
=
keepdims
))
__all__
=
[
__all__
=
[
"max_and_argmax"
,
"max_and_argmax"
,
"max"
,
"max"
,
...
@@ -2885,4 +2913,5 @@ __all__ = [
...
@@ -2885,4 +2913,5 @@ __all__ = [
"all"
,
"all"
,
"ptp"
,
"ptp"
,
"power"
,
"power"
,
"logsumexp"
,
]
]
tests/tensor/test_math.py
浏览文件 @
ecd6a1e9
...
@@ -9,6 +9,7 @@ from itertools import product
...
@@ -9,6 +9,7 @@ from itertools import product
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
from
numpy.testing
import
assert_array_equal
from
numpy.testing
import
assert_array_equal
from
scipy.special
import
logsumexp
as
scipy_logsumexp
import
aesara.scalar
as
aes
import
aesara.scalar
as
aes
from
aesara.compile.debugmode
import
DebugMode
from
aesara.compile.debugmode
import
DebugMode
...
@@ -75,6 +76,7 @@ from aesara.tensor.math import (
...
@@ -75,6 +76,7 @@ from aesara.tensor.math import (
log1p
,
log1p
,
log2
,
log2
,
log10
,
log10
,
logsumexp
,
max
,
max
,
max_and_argmax
,
max_and_argmax
,
maximum
,
maximum
,
...
@@ -3273,3 +3275,35 @@ def test_tanh_grad_broadcast():
...
@@ -3273,3 +3275,35 @@ def test_tanh_grad_broadcast():
grad
(
tanh
(
x
)
.
sum
(),
x
)
grad
(
tanh
(
x
)
.
sum
(),
x
)
grad
(
tanh
(
x
+
y
)
.
sum
(),
y
)
grad
(
tanh
(
x
+
y
)
.
sum
(),
y
)
grad
(
tanh
(
x
+
y
)
.
sum
(),
[
x
,
y
])
grad
(
tanh
(
x
+
y
)
.
sum
(),
[
x
,
y
])
@pytest.mark.parametrize
(
[
"shape"
,
"axis"
],
[
((
1
,),
0
),
((
3
,),
0
),
((
3
,
4
),
None
),
((
3
,
4
),
0
),
((
3
,
4
),
1
),
((
3
,
4
,
5
),
None
),
((
3
,
3
,
5
),
0
),
((
3
,
4
,
5
),
1
),
((
3
,
4
,
5
),
2
),
],
)
@pytest.mark.parametrize
(
"keepdims"
,
[
True
,
False
],
)
def
test_logsumexp
(
shape
,
axis
,
keepdims
):
scipy_inp
=
np
.
zeros
(
shape
)
scipy_out
=
scipy_logsumexp
(
scipy_inp
,
axis
=
axis
,
keepdims
=
keepdims
)
aesara_inp
=
as_tensor_variable
(
scipy_inp
)
f
=
function
([],
logsumexp
(
aesara_inp
,
axis
=
axis
,
keepdims
=
keepdims
))
aesara_out
=
f
()
np
.
testing
.
assert_array_almost_equal
(
aesara_out
,
scipy_out
,
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论