Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b9ea6dfb
提交
b9ea6dfb
authored
6月 06, 2025
作者:
Luca Citi
提交者:
Ricardo Vieira
7月 30, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add rewrite for `softplus(log(x)) -> log1p(x)`
上级
75b8ee9b
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
29 行增加
和
1 行删除
+29
-1
math.py
pytensor/tensor/rewriting/math.py
+8
-1
test_math.py
tests/tensor/rewriting/test_math.py
+21
-0
没有找到文件。
pytensor/tensor/rewriting/math.py
浏览文件 @
b9ea6dfb
...
@@ -576,7 +576,7 @@ def local_log_sqrt(fgraph, node):
...
@@ -576,7 +576,7 @@ def local_log_sqrt(fgraph, node):
@register_specialize
@register_specialize
@node_rewriter
([
exp
,
expm1
])
@node_rewriter
([
exp
,
expm1
,
softplus
])
def
local_exp_log_nan_switch
(
fgraph
,
node
):
def
local_exp_log_nan_switch
(
fgraph
,
node
):
# Rewrites of the kind exp(log...(x)) that require a `nan` switch
# Rewrites of the kind exp(log...(x)) that require a `nan` switch
x
=
node
.
inputs
[
0
]
x
=
node
.
inputs
[
0
]
...
@@ -629,6 +629,13 @@ def local_exp_log_nan_switch(fgraph, node):
...
@@ -629,6 +629,13 @@ def local_exp_log_nan_switch(fgraph, node):
new_out
=
switch
(
le
(
x
,
0
),
neg
(
exp
(
x
)),
np
.
asarray
(
np
.
nan
,
old_out
.
dtype
))
new_out
=
switch
(
le
(
x
,
0
),
neg
(
exp
(
x
)),
np
.
asarray
(
np
.
nan
,
old_out
.
dtype
))
return
[
new_out
]
return
[
new_out
]
# Case for softplus(log(x)) -> log1p(x)
if
isinstance
(
prev_op
,
ps
.
Log
)
and
isinstance
(
node_op
,
ps_math
.
Softplus
):
x
=
x
.
owner
.
inputs
[
0
]
old_out
=
node
.
outputs
[
0
]
new_out
=
switch
(
ge
(
x
,
0
),
log1p
(
x
),
np
.
asarray
(
np
.
nan
,
old_out
.
dtype
))
return
[
new_out
]
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
b9ea6dfb
...
@@ -1968,6 +1968,27 @@ class TestExpLog:
...
@@ -1968,6 +1968,27 @@ class TestExpLog:
decimal
=
6
,
decimal
=
6
,
)
)
def
test_softplus_log
(
self
):
# softplus(log(x)) -> log1p(x)
data_valid
=
np
.
random
.
random
((
4
,
3
))
.
astype
(
"float32"
)
*
2
data_valid
[
0
,
0
]
=
0
# edge case
data_invalid
=
data_valid
-
2
x
=
fmatrix
()
f
=
function
([
x
],
softplus
(
log
(
x
)),
mode
=
self
.
mode
)
graph
=
f
.
maker
.
fgraph
.
toposort
()
ops_graph
=
[
node
for
node
in
graph
if
isinstance
(
node
.
op
,
Elemwise
)
and
isinstance
(
node
.
op
.
scalar_op
,
ps
.
Log
|
ps
.
Exp
|
ps
.
Softplus
)
]
assert
len
(
ops_graph
)
==
0
expected
=
np
.
log1p
(
data_valid
)
np
.
testing
.
assert_almost_equal
(
f
(
data_valid
),
expected
)
assert
np
.
all
(
np
.
isnan
(
f
(
data_invalid
)))
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
[
"nested_expression"
,
"expected_switches"
],
[
"nested_expression"
,
"expected_switches"
],
[
[
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论