Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e1528269
提交
e1528269
authored
7月 13, 2016
作者:
AdeB
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Log sum exp optimization for numerical stability
上级
da95bf92
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
0 行删除
+46
-0
opt.py
theano/tensor/opt.py
+46
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
e1528269
...
@@ -6085,6 +6085,52 @@ def local_log_add(node):
...
@@ -6085,6 +6085,52 @@ def local_log_add(node):
return
[
ret
]
return
[
ret
]
@register_stabilize
@register_specialize
@gof.local_optimizer
([
T
.
log
])
def
local_log_sum_exp
(
node
):
# log(sum_i(exp(x_i))) = x_max + log(sum_i(exp(x_i - x_max)))
if
node
.
op
!=
T
.
log
:
return
sum_node
=
node
.
inputs
[
0
]
.
owner
# If the sum has keepdims=True, there might be a dimshuffle
if
sum_node
and
isinstance
(
sum_node
.
op
,
T
.
DimShuffle
):
sum_node
=
sum_node
.
inputs
[
0
]
.
owner
if
not
sum_node
or
not
isinstance
(
sum_node
.
op
,
T
.
Sum
):
return
exp_node
,
axis
=
sum_node
.
inputs
[
0
]
.
owner
,
sum_node
.
op
.
axis
if
not
exp_node
and
exp_node
.
op
!=
T
.
exp
:
return
pre_exp
=
exp_node
.
inputs
[
0
]
# optimisation may have already been applied
if
(
pre_exp
.
owner
and
isinstance
(
pre_exp
.
owner
.
op
,
T
.
Elemwise
)
and
pre_exp
.
owner
.
op
.
scalar_op
==
scalar
.
sub
):
max_node
=
pre_exp
.
owner
.
inputs
[
1
]
.
owner
if
max_node
and
isinstance
(
max_node
.
op
,
T
.
DimShuffle
):
max_node
=
max_node
.
inputs
[
0
]
.
owner
if
not
isinstance
(
max_node
.
op
,
T
.
MaxAndArgmax
):
return
if
max_node
.
inputs
[
0
]
==
pre_exp
.
owner
.
inputs
[
0
]:
return
max_pre_keepdims
=
T
.
max
(
pre_exp
,
axis
=
axis
,
keepdims
=
True
)
ret
=
(
max_pre_keepdims
+
T
.
log
(
T
.
sum
(
T
.
exp
(
pre_exp
-
max_pre_keepdims
),
axis
=
axis
,
keepdims
=
True
)))
# Restore shape and broadcastable pattern
ret
=
T
.
reshape
(
ret
,
node
.
inputs
[
0
]
.
shape
)
ret
=
T
.
patternbroadcast
(
ret
,
node
.
inputs
[
0
]
.
broadcastable
)
return
[
ret
]
def
add_calculate
(
num
,
denum
,
aslist
=
False
,
out_type
=
None
):
def
add_calculate
(
num
,
denum
,
aslist
=
False
,
out_type
=
None
):
# TODO: make sure that this function and mul_calculate are similar
# TODO: make sure that this function and mul_calculate are similar
if
out_type
is
None
:
if
out_type
is
None
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论