Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f40a8a25
提交
f40a8a25
authored
11月 29, 2011
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #233 from delallea/sigm_opt_fix
Fixed optimization for exp(x) * sigmoid(-x)
上级
7d532774
c42a7494
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
124 行增加
和
9 行删除
+124
-9
sigm.py
theano/tensor/nnet/sigm.py
+0
-0
test_sigm.py
theano/tensor/nnet/tests/test_sigm.py
+124
-9
没有找到文件。
theano/tensor/nnet/sigm.py
浏览文件 @
f40a8a25
差异被折叠。
点击展开。
theano/tensor/nnet/tests/test_sigm.py
浏览文件 @
f40a8a25
...
@@ -7,7 +7,9 @@ from theano import tensor as T
...
@@ -7,7 +7,9 @@ from theano import tensor as T
from
theano
import
config
from
theano
import
config
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.nnet
import
sigmoid
,
sigmoid_inplace
,
softplus
,
tensor
from
theano.tensor.nnet
import
sigmoid
,
sigmoid_inplace
,
softplus
,
tensor
from
theano.tensor.nnet.sigm
import
register_local_1msigmoid
from
theano.tensor.nnet.sigm
import
(
compute_mul
,
parse_mul_tree
,
perform_sigm_times_exp
,
register_local_1msigmoid
,
simplify_mul
)
class
T_sigmoid
(
unittest
.
TestCase
):
class
T_sigmoid
(
unittest
.
TestCase
):
...
@@ -23,12 +25,29 @@ class T_softplus(unittest.TestCase):
...
@@ -23,12 +25,29 @@ class T_softplus(unittest.TestCase):
utt
.
verify_grad
(
softplus
,
[
numpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
softplus
,
[
numpy
.
random
.
rand
(
3
,
4
)])
class
T_sigmoid_opts
(
unittest
.
TestCase
):
class
T_sigmoid_opts
(
unittest
.
TestCase
):
def
test_exp_over_1_plus_exp
(
self
):
def
get_mode
(
self
,
excluding
=
[]):
"""
Return appropriate mode for the tests.
:param excluding: List of optimizations to exclude.
:return: The current default mode unless the `config.mode` option is
set to 'FAST_COMPILE' (in which case it is replaced by the 'FAST_RUN'
mode), without the optimizations specified in `excluding`.
"""
m
=
theano
.
config
.
mode
m
=
theano
.
config
.
mode
if
m
==
'FAST_COMPILE'
:
if
m
==
'FAST_COMPILE'
:
m
=
'FAST_RUN'
mode
=
theano
.
compile
.
mode
.
get_mode
(
'FAST_RUN'
)
m
=
theano
.
compile
.
mode
.
get_mode
(
m
)
else
:
m
=
m
.
excluding
(
'local_elemwise_fusion'
)
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
excluding
:
return
mode
.
excluding
(
*
excluding
)
else
:
return
mode
def
test_exp_over_1_plus_exp
(
self
):
m
=
self
.
get_mode
(
excluding
=
[
'local_elemwise_fusion'
])
x
=
T
.
dvector
()
x
=
T
.
dvector
()
...
@@ -60,10 +79,7 @@ class T_sigmoid_opts(unittest.TestCase):
...
@@ -60,10 +79,7 @@ class T_sigmoid_opts(unittest.TestCase):
if
not
register_local_1msigmoid
:
if
not
register_local_1msigmoid
:
return
return
m
=
theano
.
config
.
mode
m
=
self
.
get_mode
()
if
m
==
'FAST_COMPILE'
:
m
=
'FAST_RUN'
x
=
T
.
fmatrix
()
x
=
T
.
fmatrix
()
# tests exp_over_1_plus_exp
# tests exp_over_1_plus_exp
...
@@ -77,6 +93,80 @@ class T_sigmoid_opts(unittest.TestCase):
...
@@ -77,6 +93,80 @@ class T_sigmoid_opts(unittest.TestCase):
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
tensor
.
neg
,
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
tensor
.
neg
,
sigmoid_inplace
]
sigmoid_inplace
]
def
test_local_sigm_times_exp
(
self
):
"""
Test the `local_sigm_times_exp` optimization.
exp(x) * sigm(-x) -> sigm(x)
exp(-x) * sigm(x) -> sigm(-x)
"""
def
match
(
func
,
ops
):
#print [node.op.scalar_op for node in func.maker.env.toposort()]
assert
[
node
.
op
for
node
in
func
.
maker
.
env
.
toposort
()]
==
ops
m
=
self
.
get_mode
(
excluding
=
[
'local_elemwise_fusion'
,
'inplace'
])
x
,
y
=
tensor
.
vectors
(
'x'
,
'y'
)
f
=
theano
.
function
([
x
],
sigmoid
(
-
x
)
*
tensor
.
exp
(
x
),
mode
=
m
)
theano
.
printing
.
debugprint
(
f
)
match
(
f
,
[
sigmoid
])
f
=
theano
.
function
([
x
],
sigmoid
(
x
)
*
tensor
.
exp
(
-
x
),
mode
=
m
)
theano
.
printing
.
debugprint
(
f
)
match
(
f
,
[
tensor
.
neg
,
sigmoid
])
f
=
theano
.
function
([
x
],
-
(
-
(
-
(
sigmoid
(
x
))))
*
tensor
.
exp
(
-
x
),
mode
=
m
)
theano
.
printing
.
debugprint
(
f
)
match
(
f
,
[
tensor
.
neg
,
sigmoid
,
tensor
.
neg
])
f
=
theano
.
function
(
[
x
,
y
],
(
sigmoid
(
x
)
*
sigmoid
(
-
y
)
*
-
tensor
.
exp
(
-
x
)
*
tensor
.
exp
(
x
*
y
)
*
tensor
.
exp
(
y
)),
mode
=
m
)
theano
.
printing
.
debugprint
(
f
)
match
(
f
,
[
sigmoid
,
tensor
.
mul
,
tensor
.
neg
,
tensor
.
exp
,
sigmoid
,
tensor
.
mul
,
tensor
.
neg
])
def
test_perform_sigm_times_exp
(
self
):
"""
Test the core function doing the `sigm_times_exp` optimization.
It is easier to test different graph scenarios this way than by
compiling a theano function.
"""
x
,
y
,
z
,
t
=
tensor
.
vectors
(
'x'
,
'y'
,
'z'
,
't'
)
exp
=
tensor
.
exp
def
ok
(
expr1
,
expr2
):
trees
=
[
parse_mul_tree
(
e
)
for
e
in
(
expr1
,
expr2
)]
perform_sigm_times_exp
(
trees
[
0
])
trees
[
0
]
=
simplify_mul
(
trees
[
0
])
# TODO Ideally we would do a full comparison without `str`. However
# the implementation of `__eq__` in Variables is not currently
# appropriate for this. So for now we use this limited technique,
# but it could be improved on.
good
=
str
(
trees
[
0
])
==
str
(
trees
[
1
])
if
not
good
:
print
trees
[
0
]
print
trees
[
1
]
print
'***'
theano
.
printing
.
debugprint
(
compute_mul
(
trees
[
0
]))
print
'***'
theano
.
printing
.
debugprint
(
compute_mul
(
trees
[
1
]))
assert
good
ok
(
sigmoid
(
x
)
*
exp
(
-
x
),
sigmoid
(
-
x
))
ok
(
-
x
*
sigmoid
(
x
)
*
(
y
*
(
-
1
*
z
)
*
exp
(
-
x
)),
-
x
*
sigmoid
(
-
x
)
*
(
y
*
(
-
1
*
z
)))
ok
(
-
sigmoid
(
-
x
)
*
(
exp
(
y
)
*
(
-
exp
(
-
z
)
*
3
*
-
exp
(
x
))
*
(
y
*
2
*
(
-
sigmoid
(
-
y
)
*
(
z
+
t
)
*
exp
(
z
))
*
sigmoid
(
z
)))
*
-
sigmoid
(
x
),
sigmoid
(
x
)
*
(
-
sigmoid
(
y
)
*
(
-
sigmoid
(
-
z
)
*
3
)
*
(
y
*
2
*
((
z
+
t
)
*
exp
(
z
))))
*
-
sigmoid
(
x
))
ok
(
exp
(
-
x
)
*
-
exp
(
-
x
)
*
(
-
sigmoid
(
x
)
*
-
sigmoid
(
x
)),
-
sigmoid
(
-
x
)
*
sigmoid
(
-
x
))
ok
(
-
exp
(
x
)
*
-
sigmoid
(
-
x
)
*
-
exp
(
-
x
),
-
sigmoid
(
-
x
))
class
T_softplus_opts
(
unittest
.
TestCase
):
class
T_softplus_opts
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -123,3 +213,28 @@ class T_softplus_opts(unittest.TestCase):
...
@@ -123,3 +213,28 @@ class T_softplus_opts(unittest.TestCase):
assert
len
(
topo
)
==
1
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
f
(
numpy
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
))
f
(
numpy
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
))
class
T_sigmoid_utils
(
unittest
.
TestCase
):
"""
Test utility functions found in 'sigm.py'.
"""
def
test_compute_mul
(
self
):
x
,
y
,
z
=
tensor
.
vectors
(
'x'
,
'y'
,
'z'
)
tree
=
(
x
*
y
)
*
-
z
mul_tree
=
parse_mul_tree
(
tree
)
# Note that we do not test the reverse identity, i.e.
# compute_mul(parse_mul_tree(tree)) == tree
# because Theano currently lacks an easy way to compare variables.
assert
parse_mul_tree
(
compute_mul
(
mul_tree
))
==
mul_tree
def
test_parse_mul_tree
(
self
):
x
,
y
,
z
=
tensor
.
vectors
(
'x'
,
'y'
,
'z'
)
assert
parse_mul_tree
(
x
*
y
)
==
[
False
,
[[
False
,
x
],
[
False
,
y
]]]
assert
parse_mul_tree
(
-
(
x
*
y
))
==
[
True
,
[[
False
,
x
],
[
False
,
y
]]]
assert
parse_mul_tree
(
-
x
*
y
)
==
[
False
,
[[
True
,
x
],
[
False
,
y
]]]
assert
parse_mul_tree
(
-
x
)
==
[
True
,
x
]
assert
parse_mul_tree
((
x
*
y
)
*
-
z
)
==
[
False
,
[[
False
,
[[
False
,
x
],
[
False
,
y
]]],
[
True
,
z
]]]
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论