Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d0a9488a
提交
d0a9488a
authored
8月 09, 2021
作者:
Ricardo
提交者:
Ricardo Vieira
8月 19, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cast output of `local_func_inv` and `local_exp_log` to float when needed
上级
43ed9011
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
33 行增加
和
2 行删除
+33
-2
math_opt.py
aesara/tensor/math_opt.py
+12
-2
test_math_opt.py
tests/tensor/test_math_opt.py
+21
-0
没有找到文件。
aesara/tensor/math_opt.py
浏览文件 @
d0a9488a
...
@@ -225,7 +225,12 @@ def local_func_inv(fgraph, node):
...
@@ -225,7 +225,12 @@ def local_func_inv(fgraph, node):
if
is_inverse_pair
(
node_op
,
prev_op
,
inv_pair
):
if
is_inverse_pair
(
node_op
,
prev_op
,
inv_pair
):
# We don't need to copy stack trace, because the optimization
# We don't need to copy stack trace, because the optimization
# is trivial and maintains the earlier stack trace
# is trivial and maintains the earlier stack trace
return
x
.
owner
.
inputs
ottype
=
node
.
out
.
dtype
inp
=
x
.
owner
.
inputs
[
0
]
# Functions may have casted integer input to float
if
inp
.
dtype
!=
ottype
:
inp
=
cast
(
inp
,
ottype
)
return
[
inp
]
return
return
...
@@ -246,7 +251,12 @@ def local_exp_log(fgraph, node):
...
@@ -246,7 +251,12 @@ def local_exp_log(fgraph, node):
# Case for log(exp(x))
# Case for log(exp(x))
if
isinstance
(
prev_op
,
aes
.
Exp
)
and
isinstance
(
node_op
,
aes
.
Log
):
if
isinstance
(
prev_op
,
aes
.
Exp
)
and
isinstance
(
node_op
,
aes
.
Log
):
return
x
.
owner
.
inputs
new_out
=
x
.
owner
.
inputs
[
0
]
old_out
=
node
.
outputs
[
0
]
# Exp may have casted integer input to float
if
new_out
.
dtype
!=
old_out
.
dtype
:
new_out
=
cast
(
new_out
,
old_out
.
dtype
)
return
[
new_out
]
# Case for exp(softplus(x)) aka exp(log1pexp)
# Case for exp(softplus(x)) aka exp(log1pexp)
if
isinstance
(
prev_op
,
aes_math
.
Softplus
)
and
isinstance
(
node_op
,
aes
.
Exp
):
if
isinstance
(
prev_op
,
aes_math
.
Softplus
)
and
isinstance
(
node_op
,
aes
.
Exp
):
...
...
tests/tensor/test_math_opt.py
浏览文件 @
d0a9488a
...
@@ -2488,6 +2488,16 @@ class TestFuncInverse:
...
@@ -2488,6 +2488,16 @@ class TestFuncInverse:
self
.
assert_func_pair_optimized
(
rad2deg
,
rad2deg
,
dx
,
should_copy
=
False
)
self
.
assert_func_pair_optimized
(
rad2deg
,
rad2deg
,
dx
,
should_copy
=
False
)
self
.
assert_func_pair_optimized
(
rad2deg
,
cosh
,
dx
,
should_copy
=
False
)
self
.
assert_func_pair_optimized
(
rad2deg
,
cosh
,
dx
,
should_copy
=
False
)
def
test_integer_upcast
(
self
):
"""
All invertible methods (except for `Neg`) can upgrade their input to float.
Here we test that the rewrite works with just one pair of methods
"""
x
=
ivector
(
"x"
)
f
=
function
([
x
],
deg2rad
(
rad2deg
(
x
)),
mode
=
self
.
mode
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
class
TestExpLog
:
class
TestExpLog
:
def
setup_method
(
self
):
def
setup_method
(
self
):
...
@@ -2512,6 +2522,17 @@ class TestExpLog:
...
@@ -2512,6 +2522,17 @@ class TestExpLog:
assert
len
(
ops_graph
)
==
0
assert
len
(
ops_graph
)
==
0
np
.
testing
.
assert_array_equal
(
f
(
data
),
data
)
np
.
testing
.
assert_array_equal
(
f
(
data
),
data
)
def
test_log_exp_integer_upcast
(
self
):
x
=
ivector
(
"x"
)
f
=
function
([
x
],
log
(
exp
(
x
)),
mode
=
self
.
mode
)
ops_graph
=
[
node
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
Elemwise
)
and
isinstance
(
node
.
op
.
scalar_op
,
(
aes
.
Log
,
aes
.
Exp
))
]
assert
len
(
ops_graph
)
==
0
def
test_exp_log
(
self
):
def
test_exp_log
(
self
):
# exp(log(x)) -> switch(x >= 0, x, nan)
# exp(log(x)) -> switch(x >= 0, x, nan)
data_valid
=
np
.
random
.
random
((
4
,
3
))
.
astype
(
"float32"
)
data_valid
=
np
.
random
.
random
((
4
,
3
))
.
astype
(
"float32"
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论