Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a9472ea7
提交
a9472ea7
authored
8月 22, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #873 from goodfeli/stabilize_log_softmax
added an optimization to stabilize log softmax
上级
72a7214a
856fdd09
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
39 行增加
和
0 行删除
+39
-0
nnet.py
theano/tensor/nnet/nnet.py
+20
-0
test_opt.py
theano/tensor/tests/test_opt.py
+19
-0
没有找到文件。
theano/tensor/nnet/nnet.py
浏览文件 @
a9472ea7
...
@@ -1729,3 +1729,23 @@ class Prepend_scalar_to_each_row(gof.Op):
...
@@ -1729,3 +1729,23 @@ class Prepend_scalar_to_each_row(gof.Op):
prepend_scalar_to_each_row
=
Prepend_scalar_to_each_row
()
prepend_scalar_to_each_row
=
Prepend_scalar_to_each_row
()
prepend_0_to_each_row
=
Prepend_scalar_constant_to_each_row
(
0.
)
prepend_0_to_each_row
=
Prepend_scalar_constant_to_each_row
(
0.
)
prepend_1_to_each_row
=
Prepend_scalar_constant_to_each_row
(
1.
)
prepend_1_to_each_row
=
Prepend_scalar_constant_to_each_row
(
1.
)
#numerically stabilize log softmax (X)
# as X-X.max(axis=1).dimshuffle(0,'x') - log(exp(X-X.max(axis=1).dimshuffle(0,'x')).sum(axis=1)).dimshuffle(0,'x)
def
make_out_pattern
(
X
):
stabilized_X
=
X
-
X
.
max
(
axis
=
1
)
.
dimshuffle
(
0
,
'x'
)
out_var
=
stabilized_X
-
tensor
.
log
(
tensor
.
exp
(
stabilized_X
)
.
sum
(
axis
=
1
))
.
dimshuffle
(
0
,
'x'
)
#tell DEBUG_MODE that it's OK if the original graph produced NaN and the optimized graph does not
out_var
.
values_eq_approx
=
out_var
.
type
.
values_eq_approx_remove_nan
return
out_var
local_log_softmax
=
gof
.
PatternSub
(
in_pattern
=
(
tensor
.
log
,
(
softmax
,
'x'
)),
out_pattern
=
(
make_out_pattern
,
'x'
),
allow_multiple_clients
=
True
)
#don't do register_stabilize, this is to make local_log_softmax run
#only after another more specific optimization that stabilizes cross entropy
#opt.register_stabilize(local_log_softmax, name = 'local_log_softmax')
opt
.
register_specialize
(
local_log_softmax
,
name
=
'local_log_softmax'
)
theano/tensor/tests/test_opt.py
浏览文件 @
a9472ea7
...
@@ -132,6 +132,25 @@ class test_dimshuffle_lift(unittest.TestCase):
...
@@ -132,6 +132,25 @@ class test_dimshuffle_lift(unittest.TestCase):
"{x,0,1}(y)), z)]"
),
str
(
g
))
"{x,0,1}(y)), z)]"
),
str
(
g
))
def
test_stabilize_log_softmax
():
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
mode
.
including
(
'local_log_softmax'
)
x
=
matrix
()
y
=
theano
.
tensor
.
nnet
.
softmax
(
x
)
z
=
theano
.
tensor
.
log
(
y
)
f
=
function
([
x
],
z
)
#check that the softmax has been optimized out
for
node
in
f
.
maker
.
fgraph
.
toposort
():
assert
not
isinstance
(
node
.
op
,
y
.
owner
.
op
.
__class__
)
#call the function so debug mode can verify the optimized
#version matches the unoptimized version
rng
=
numpy
.
random
.
RandomState
([
2012
,
8
,
22
])
f
(
numpy
.
cast
[
config
.
floatX
](
rng
.
randn
(
2
,
3
)))
def
test_add_canonizer_problem0
():
def
test_add_canonizer_problem0
():
n_segments
=
10
n_segments
=
10
label
=
lscalar
(
'label'
)
label
=
lscalar
(
'label'
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论