Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
388805f9
提交
388805f9
authored
4月 17, 2017
作者:
Pascal Lamblin
提交者:
GitHub
4月 17, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5716 from nouiz/err
Better error message when an opt make an invalide replace.
上级
a5c029dc
db34bfe2
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
238 行增加
和
169 行删除
+238
-169
debugmode.py
theano/compile/debugmode.py
+3
-158
test_debugmode.py
theano/compile/tests/test_debugmode.py
+39
-6
configparser.py
theano/configparser.py
+1
-1
fg.py
theano/gof/fg.py
+16
-4
toolbox.py
theano/gof/toolbox.py
+178
-0
slinalg.py
theano/tensor/slinalg.py
+1
-0
没有找到文件。
theano/compile/debugmode.py
浏览文件 @
388805f9
...
...
@@ -153,166 +153,9 @@ class BadThunkOutput(DebugModeError):
return
ret
class
BadOptimization
(
DebugModeError
):
"""
Exception: some variable and its substitute take different runtime values.
"""
new_r
=
None
"""
A `Variable` instance that took a different value from `old_r`,
but which replaced `old_r`.
"""
old_r
=
None
"""
A `Variable` instance that was replaced by `new_r`.
"""
old_r_val
=
None
"""
The value computed for `old_r`.
"""
new_r_val
=
None
"""
The value computed for `new_r`.
"""
reason
=
None
"""
An object that indicates why old_r was turned into new_r.
Convention is that this is the name of the optimization that
requested the replacement.
"""
old_graph
=
""
"""
A multiline string representation of the graph leading to
old_r, at the time of the replacement.
"""
new_graph
=
""
"""
A multiline string representation of the graph leading to
new_r, at the time of the replacement.
"""
def
__init__
(
self
,
old_r
,
new_r
,
old_r_val
,
new_r_val
,
reason
,
old_graph
,
new_graph
):
super
(
BadOptimization
,
self
)
.
__init__
()
self
.
old_r
=
old_r
self
.
new_r
=
new_r
self
.
old_r_val
=
old_r_val
self
.
new_r_val
=
new_r_val
self
.
reason
=
reason
self
.
old_graph
=
old_graph
self
.
new_graph
=
new_graph
def
__str__
(
self
):
return
self
.
str_diagnostic
()
def
str_diagnostic
(
self
):
"""
Return a pretty multiline string representating the cause
of the exception.
"""
sio
=
StringIO
()
val_str_len_limit
=
800
print
(
"BadOptimization Error"
,
super
(
BadOptimization
,
self
)
.
__str__
(),
file
=
sio
)
print
(
" Variable: id"
,
id
(
self
.
new_r
),
self
.
new_r
,
file
=
sio
)
print
(
" Op"
,
self
.
new_r
.
owner
,
file
=
sio
)
print
(
" Value Type:"
,
type
(
self
.
new_r_val
),
file
=
sio
)
try
:
ssio
=
StringIO
()
print
(
" Old Value shape, dtype, strides:"
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
old_r_val
.
shape
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
old_r_val
.
dtype
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
old_r_val
.
strides
,
file
=
ssio
)
# only if all succeeds to we add anything to sio
print
(
ssio
.
getvalue
(),
file
=
sio
)
except
Exception
:
class
BadOptimization
(
DebugModeError
,
theano
.
gof
.
toolbox
.
BadOptimization
):
pass
str_old_r_val
=
str
(
self
.
old_r_val
)
if
len
(
str_old_r_val
)
>
val_str_len_limit
:
print
(
" Old Value: "
,
str
(
self
.
old_r_val
)[
:
val_str_len_limit
],
'...'
,
file
=
sio
)
else
:
print
(
" Old Value: "
,
str
(
self
.
old_r_val
),
file
=
sio
)
try
:
ssio
=
StringIO
()
print
(
" New Value shape, dtype, strides:"
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
new_r_val
.
shape
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
new_r_val
.
dtype
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
new_r_val
.
strides
,
file
=
ssio
)
# only if all succeeds to we add anything to sio
print
(
ssio
.
getvalue
(),
file
=
sio
)
except
Exception
:
pass
str_new_r_val
=
str
(
self
.
new_r_val
)
if
len
(
str_new_r_val
)
>
val_str_len_limit
:
print
(
" New Value: "
,
str
(
self
.
new_r_val
)[
:
val_str_len_limit
],
'...'
,
file
=
sio
)
else
:
print
(
" New Value: "
,
str
(
self
.
new_r_val
),
file
=
sio
)
try
:
ov
=
np
.
asarray
(
self
.
old_r_val
)
nv
=
np
.
asarray
(
self
.
new_r_val
)
ssio
=
StringIO
()
abs_diff
=
np
.
absolute
(
nv
-
ov
)
print
(
" Max Abs Diff: "
,
np
.
max
(
abs_diff
),
file
=
ssio
)
print
(
" Mean Abs Diff: "
,
np
.
mean
(
abs_diff
),
file
=
ssio
)
print
(
" Median Abs Diff: "
,
np
.
median
(
abs_diff
),
file
=
ssio
)
print
(
" Std Abs Diff: "
,
np
.
std
(
abs_diff
),
file
=
ssio
)
arg_max_val
=
np
.
argmax
(
abs_diff
)
values_at_max
=
(
nv
.
flatten
()[
arg_max_val
],
ov
.
flatten
()[
arg_max_val
])
print
(
" Value at Max Diff: "
,
values_at_max
,
file
=
ssio
)
# N.B. the maximum(..., 1e-8) protects against div by 0 when
# nv == ov == 0
reldiff
=
(
abs_diff
/
np
.
maximum
(
np
.
absolute
(
nv
)
+
np
.
absolute
(
ov
),
1e-8
))
print
(
" Max Rel Diff: "
,
np
.
max
(
reldiff
),
file
=
ssio
)
print
(
" Mean Rel Diff: "
,
np
.
mean
(
reldiff
),
file
=
ssio
)
print
(
" Median Rel Diff: "
,
np
.
median
(
reldiff
),
file
=
ssio
)
print
(
" Std Rel Diff: "
,
np
.
std
(
reldiff
),
file
=
ssio
)
arg_max_val
=
np
.
argmax
(
reldiff
)
values_at_max
=
(
nv
.
flatten
()[
arg_max_val
],
ov
.
flatten
()[
arg_max_val
])
print
(
" Value at Max Diff: "
,
values_at_max
,
file
=
ssio
)
# only if all succeeds to we add anything to sio
print
(
ssio
.
getvalue
(),
file
=
sio
)
except
Exception
:
pass
print
(
" Reason: "
,
str
(
self
.
reason
),
file
=
sio
)
print
(
" Old Graph:"
,
file
=
sio
)
print
(
self
.
old_graph
,
file
=
sio
)
print
(
" New Graph:"
,
file
=
sio
)
print
(
self
.
new_graph
,
file
=
sio
)
print
(
""
,
file
=
sio
)
print
(
"Hint: relax the tolerance by setting tensor.cmp_sloppy=1"
,
file
=
sio
)
print
(
" or even tensor.cmp_sloppy=2 for less-strict comparison"
,
file
=
sio
)
return
sio
.
getvalue
()
class
BadDestroyMap
(
DebugModeError
):
"""
...
...
@@ -1697,9 +1540,11 @@ class _VariableEquivalenceTracker(object):
r
,
debugprint
(
r
,
prefix
=
' '
,
depth
=
6
,
file
=
StringIO
(),
done
=
done
,
print_type
=
True
,
used_ids
=
used_ids
)
.
getvalue
(),
debugprint
(
new_r
,
prefix
=
' '
,
depth
=
6
,
file
=
StringIO
(),
done
=
done
,
print_type
=
True
,
used_ids
=
used_ids
)
.
getvalue
()))
self
.
replaced_by
[
r
]
.
append
((
reason
,
new_r
))
...
...
theano/compile/tests/test_debugmode.py
浏览文件 @
388805f9
from
__future__
import
absolute_import
,
print_function
,
division
from
nose.plugins.skip
import
SkipTest
import
sys
import
unittest
from
nose.plugins.skip
import
SkipTest
import
numpy
as
np
from
six
import
reraise
from
theano
import
config
from
theano
import
gof
import
theano
import
theano.tensor
from
theano.compat
import
exc_message
from
theano.compile
import
debugmode
import
theano.
compile
import
theano.
tensor
from
theano.tests
import
unittest_tools
as
utt
...
...
@@ -255,22 +256,54 @@ def test_badoptimization_opt_err():
inputs
[
-
1
]))
return
[
node
.
op
(
*
inputs
)]
return
False
@gof.local_optimizer
([
theano
.
tensor
.
add
])
def
insert_bad_dtype
(
node
):
if
node
.
op
==
theano
.
tensor
.
add
:
inputs
=
list
(
node
.
inputs
)
if
inputs
[
-
1
]
.
owner
is
None
:
return
[
node
.
outputs
[
0
]
.
astype
(
'float32'
)]
return
False
edb
=
gof
.
EquilibriumDB
()
edb
.
register
(
'insert_bigger_b_add'
,
insert_bigger_b_add
,
'all'
)
opt
=
edb
.
query
(
'+all'
)
edb2
=
gof
.
EquilibriumDB
()
edb2
.
register
(
'insert_bad_dtype'
,
insert_bad_dtype
,
'all'
)
opt2
=
edb2
.
query
(
'+all'
)
a
=
theano
.
tensor
.
dvector
()
b
=
theano
.
tensor
.
dvector
()
f
=
theano
.
function
([
a
,
b
],
a
+
b
,
mode
=
debugmode
.
DebugMode
(
optimizer
=
opt
))
try
:
f
([
1.0
,
2.0
,
3.0
],
[
2
,
3
,
4
],)
except
Exception
as
e
:
except
ValueError
as
e
:
assert
'insert_bigger_b_add'
in
exc_message
(
e
)
return
# TEST PASS
else
:
assert
False
# Test that opt that do an illegal change still get the error from gof.
try
:
with
theano
.
configparser
.
change_flags
(
on_opt_error
=
'raise'
):
f2
=
theano
.
function
([
a
,
b
],
a
+
b
,
mode
=
debugmode
.
DebugMode
(
optimizer
=
opt2
,
stability_patience
=
1
))
f2
([
1.0
,
2.0
,
3.0
],
[
2
,
3
,
4
],)
except
theano
.
gof
.
toolbox
.
BadOptimization
as
e
:
assert
'insert_bad_dtype'
in
str
(
e
)
# Test that we can reraise the error with an extended message
try
:
new_e
=
e
.
__class__
(
"TTT"
+
str
(
e
))
exc_type
,
exc_value
,
exc_trace
=
sys
.
exc_info
()
exc_value
=
new_e
reraise
(
e
.
__class__
,
exc_value
,
exc_trace
)
except
theano
.
gof
.
toolbox
.
BadOptimization
as
e
:
pass
else
:
assert
False
else
:
assert
False
...
...
theano/configparser.py
浏览文件 @
388805f9
...
...
@@ -105,7 +105,7 @@ class change_flags(object):
for
k
in
args
:
l
=
[
v
for
v
in
theano
.
configparser
.
_config_var_list
if
v
.
fullname
==
k
]
assert
len
(
l
)
==
1
assert
len
(
l
)
==
1
,
l
confs
[
k
]
=
l
[
0
]
self
.
confs
=
confs
self
.
new_vals
=
args
...
...
theano/gof/fg.py
浏览文件 @
388805f9
...
...
@@ -15,6 +15,7 @@ from theano.gof import toolbox
from
theano
import
config
from
six
import
iteritems
,
itervalues
from
six.moves
import
StringIO
from
theano.gof.utils
import
get_variable_trace_string
from
theano.misc.ordered_set
import
OrderedSet
NullType
=
None
...
...
@@ -468,10 +469,21 @@ class FunctionGraph(utils.object2):
new_r2
=
r
.
type
.
convert_variable
(
new_r
)
# We still make sure that the type converts correctly
if
new_r2
is
None
or
new_r2
.
type
!=
r
.
type
:
raise
TypeError
(
"The type of the replacement must be "
"compatible with the type of the original "
"Variable."
,
r
,
new_r
,
r
.
type
,
new_r
.
type
,
str
(
reason
))
done
=
dict
()
used_ids
=
dict
()
old
=
theano
.
compile
.
debugmode
.
debugprint
(
r
,
prefix
=
' '
,
depth
=
6
,
file
=
StringIO
(),
done
=
done
,
print_type
=
True
,
used_ids
=
used_ids
)
.
getvalue
()
new
=
theano
.
compile
.
debugmode
.
debugprint
(
new_r
,
prefix
=
' '
,
depth
=
6
,
file
=
StringIO
(),
done
=
done
,
print_type
=
True
,
used_ids
=
used_ids
)
.
getvalue
()
raise
toolbox
.
BadOptimization
(
r
,
new_r
,
None
,
None
,
str
(
reason
)
+
". The type of the replacement must be the same."
,
old
,
new
)
new_r
=
new_r2
if
r
not
in
self
.
variables
:
# this variable isn't in the graph... don't raise an
...
...
theano/gof/toolbox.py
浏览文件 @
388805f9
...
...
@@ -6,6 +6,9 @@ import sys
import
time
import
inspect
import
numpy
as
np
from
six.moves
import
StringIO
import
theano
from
theano
import
config
from
theano.gof
import
graph
...
...
@@ -33,6 +36,181 @@ class ReplacementDidntRemovedError(Exception):
pass
class
BadOptimization
(
Exception
):
"""
Exception: some variable and its substitute take different runtime values.
Note: If there is only 1 parameter and it is a string, we will use
it as the error message. This is needed when we catch, extend and
reraise an error.
"""
new_r
=
None
"""
A `Variable` instance that took a different value from `old_r`,
but which replaced `old_r`.
"""
old_r
=
None
"""
A `Variable` instance that was replaced by `new_r`.
"""
old_r_val
=
None
"""
The value computed for `old_r`.
"""
new_r_val
=
None
"""
The value computed for `new_r`.
"""
reason
=
None
"""
An object that indicates why old_r was turned into new_r.
Convention is that this is the name of the optimization that
requested the replacement.
"""
old_graph
=
""
"""
A multiline string representation of the graph leading to
old_r, at the time of the replacement.
"""
new_graph
=
""
"""
A multiline string representation of the graph leading to
new_r, at the time of the replacement.
"""
def
__init__
(
self
,
old_r
,
new_r
=
None
,
old_r_val
=
None
,
new_r_val
=
None
,
reason
=
None
,
old_graph
=
None
,
new_graph
=
None
):
super
(
BadOptimization
,
self
)
.
__init__
()
self
.
old_r
=
old_r
self
.
new_r
=
new_r
self
.
old_r_val
=
old_r_val
self
.
new_r_val
=
new_r_val
self
.
reason
=
reason
self
.
old_graph
=
old_graph
self
.
new_graph
=
new_graph
# To allow extending the error message of an existing error.
self
.
full_err
=
None
if
isinstance
(
old_r
,
str
):
assert
(
new_r
is
None
and
old_r_val
is
None
and
new_r_val
is
None
and
reason
is
None
and
old_graph
is
None
and
new_graph
is
None
)
self
.
full_err
=
old_r
def
__str__
(
self
):
return
self
.
str_diagnostic
()
def
str_diagnostic
(
self
):
"""
Return a pretty multiline string representating the cause
of the exception.
"""
# We have a pre-made message
if
getattr
(
self
,
'full_err'
,
None
)
is
not
None
:
return
self
.
full_err
sio
=
StringIO
()
val_str_len_limit
=
800
print
(
"BadOptimization Error"
,
super
(
BadOptimization
,
self
)
.
__str__
(),
file
=
sio
)
print
(
" Variable: id"
,
id
(
self
.
new_r
),
self
.
new_r
,
file
=
sio
)
print
(
" Op"
,
self
.
new_r
.
owner
,
file
=
sio
)
print
(
" Value Type:"
,
type
(
self
.
new_r_val
),
file
=
sio
)
try
:
ssio
=
StringIO
()
print
(
" Old Value shape, dtype, strides:"
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
old_r_val
.
shape
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
old_r_val
.
dtype
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
old_r_val
.
strides
,
file
=
ssio
)
# only if all succeeds to we add anything to sio
print
(
ssio
.
getvalue
(),
file
=
sio
)
except
Exception
:
pass
str_old_r_val
=
str
(
self
.
old_r_val
)
if
len
(
str_old_r_val
)
>
val_str_len_limit
:
print
(
" Old Value: "
,
str
(
self
.
old_r_val
)[
:
val_str_len_limit
],
'...'
,
file
=
sio
)
else
:
print
(
" Old Value: "
,
str
(
self
.
old_r_val
),
file
=
sio
)
try
:
ssio
=
StringIO
()
print
(
" New Value shape, dtype, strides:"
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
new_r_val
.
shape
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
new_r_val
.
dtype
,
end
=
' '
,
file
=
ssio
)
print
(
self
.
new_r_val
.
strides
,
file
=
ssio
)
# only if all succeeds to we add anything to sio
print
(
ssio
.
getvalue
(),
file
=
sio
)
except
Exception
:
pass
str_new_r_val
=
str
(
self
.
new_r_val
)
if
len
(
str_new_r_val
)
>
val_str_len_limit
:
print
(
" New Value: "
,
str
(
self
.
new_r_val
)[
:
val_str_len_limit
],
'...'
,
file
=
sio
)
else
:
print
(
" New Value: "
,
str
(
self
.
new_r_val
),
file
=
sio
)
try
:
ov
=
np
.
asarray
(
self
.
old_r_val
)
nv
=
np
.
asarray
(
self
.
new_r_val
)
ssio
=
StringIO
()
abs_diff
=
np
.
absolute
(
nv
-
ov
)
print
(
" Max Abs Diff: "
,
np
.
max
(
abs_diff
),
file
=
ssio
)
print
(
" Mean Abs Diff: "
,
np
.
mean
(
abs_diff
),
file
=
ssio
)
print
(
" Median Abs Diff: "
,
np
.
median
(
abs_diff
),
file
=
ssio
)
print
(
" Std Abs Diff: "
,
np
.
std
(
abs_diff
),
file
=
ssio
)
arg_max_val
=
np
.
argmax
(
abs_diff
)
values_at_max
=
(
nv
.
flatten
()[
arg_max_val
],
ov
.
flatten
()[
arg_max_val
])
print
(
" Value at Max Diff: "
,
values_at_max
,
file
=
ssio
)
# N.B. the maximum(..., 1e-8) protects against div by 0 when
# nv == ov == 0
reldiff
=
(
abs_diff
/
np
.
maximum
(
np
.
absolute
(
nv
)
+
np
.
absolute
(
ov
),
1e-8
))
print
(
" Max Rel Diff: "
,
np
.
max
(
reldiff
),
file
=
ssio
)
print
(
" Mean Rel Diff: "
,
np
.
mean
(
reldiff
),
file
=
ssio
)
print
(
" Median Rel Diff: "
,
np
.
median
(
reldiff
),
file
=
ssio
)
print
(
" Std Rel Diff: "
,
np
.
std
(
reldiff
),
file
=
ssio
)
arg_max_val
=
np
.
argmax
(
reldiff
)
values_at_max
=
(
nv
.
flatten
()[
arg_max_val
],
ov
.
flatten
()[
arg_max_val
])
print
(
" Value at Max Diff: "
,
values_at_max
,
file
=
ssio
)
# only if all succeeds to we add anything to sio
print
(
ssio
.
getvalue
(),
file
=
sio
)
except
Exception
:
pass
print
(
" Reason: "
,
str
(
self
.
reason
),
file
=
sio
)
print
(
" Old Graph:"
,
file
=
sio
)
print
(
self
.
old_graph
,
file
=
sio
)
print
(
" New Graph:"
,
file
=
sio
)
print
(
self
.
new_graph
,
file
=
sio
)
print
(
""
,
file
=
sio
)
print
(
"Hint: relax the tolerance by setting tensor.cmp_sloppy=1"
,
file
=
sio
)
print
(
" or even tensor.cmp_sloppy=2 for less-strict comparison"
,
file
=
sio
)
return
sio
.
getvalue
()
class
Feature
(
object
):
"""
Base class for FunctionGraph extensions.
...
...
theano/tensor/slinalg.py
浏览文件 @
388805f9
...
...
@@ -176,6 +176,7 @@ class Solve(Op):
"""
Solve a system of linear equations.
For on CPU and GPU.
"""
__props__
=
(
'A_structure'
,
'lower'
,
'overwrite_A'
,
'overwrite_b'
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论