Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b065112b
Unverified
提交
b065112b
authored
1月 28, 2025
作者:
Jesse Grabowski
提交者:
GitHub
1月 28, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add rewrite for `1 ** x = 1` (#1179)
上级
2f1d25a9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
51 行增加
和
5 行删除
+51
-5
math.py
pytensor/tensor/rewriting/math.py
+32
-5
test_math.py
tests/tensor/rewriting/test_math.py
+19
-0
没有找到文件。
pytensor/tensor/rewriting/math.py
浏览文件 @
b065112b
...
@@ -1905,13 +1905,40 @@ def local_reciprocal_canon(fgraph, node):
...
@@ -1905,13 +1905,40 @@ def local_reciprocal_canon(fgraph, node):
@register_canonicalize
@register_canonicalize
@node_rewriter
([
pt_pow
])
@node_rewriter
([
pt_pow
])
def
local_pow_canonicalize
(
fgraph
,
node
):
def
local_pow_canonicalize
(
fgraph
,
node
):
cst
=
get_underlying_scalar_constant_value
(
"""
Rewrites for exponential functions with straight-forward simplifications:
1. x ** 0 -> 1
2. x ** 1 -> x
3. 1 ** x -> 1
In all cases, the shape of the output is the result of broadcasting the shapes of the inputs.
"""
cst_base
=
get_underlying_scalar_constant_value
(
node
.
inputs
[
0
],
only_process_constants
=
True
,
raise_not_constant
=
False
)
cst_exponent
=
get_underlying_scalar_constant_value
(
node
.
inputs
[
1
],
only_process_constants
=
True
,
raise_not_constant
=
False
node
.
inputs
[
1
],
only_process_constants
=
True
,
raise_not_constant
=
False
)
)
if
cst
==
0
:
return
[
alloc_like
(
1
,
node
.
outputs
[
0
],
fgraph
)]
new_out
=
None
if
cst
==
1
:
return
[
alloc_like
(
node
.
inputs
[
0
],
node
.
outputs
[
0
],
fgraph
)]
if
cst_base
==
1
:
# 1 ** x = 1
new_out
=
broadcast_arrays
(
*
node
.
inputs
)[
0
]
elif
cst_exponent
==
0
:
# x ** 0 = 1
new_out
=
broadcast_arrays
(
ones_like
(
node
.
inputs
[
0
]),
node
.
inputs
[
1
])[
0
]
elif
cst_exponent
==
1
:
# x ** 1 = x
new_out
=
broadcast_arrays
(
*
node
.
inputs
)[
0
]
if
not
new_out
:
return
if
new_out
.
dtype
!=
node
.
out
.
dtype
:
new_out
=
cast
(
new_out
,
dtype
=
node
.
out
.
dtype
)
return
[
new_out
]
@register_specialize
@register_specialize
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
b065112b
...
@@ -4571,3 +4571,22 @@ def test_log_kv_stabilization():
...
@@ -4571,3 +4571,22 @@ def test_log_kv_stabilization():
out
.
eval
({
x
:
1000.0
},
mode
=
mode
),
out
.
eval
({
x
:
1000.0
},
mode
=
mode
),
-
1003.2180912984705
,
-
1003.2180912984705
,
)
)
@pytest.mark.parametrize
(
"shape"
,
[(),
(
4
,
5
,
6
)],
ids
=
[
"scalar"
,
"tensor"
])
def
test_pow_1_rewrite
(
shape
):
x
=
pt
.
tensor
(
"x"
,
shape
=
shape
)
z
=
1
**
x
assert
isinstance
(
z
.
owner
.
op
,
Elemwise
)
and
isinstance
(
z
.
owner
.
op
.
scalar_op
,
ps
.
basic
.
Pow
)
f
=
pytensor
.
function
([
x
],
z
)
assert
not
any
(
isinstance
(
node
.
op
,
Elemwise
)
and
isinstance
(
node
.
op
.
scalar_op
,
ps
.
basic
.
Pow
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()
)
x_val
=
np
.
random
.
random
(
shape
)
.
astype
(
config
.
floatX
)
np
.
testing
.
assert_allclose
(
z
.
eval
({
x
:
x_val
}),
f
(
x_val
))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论