Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2865e546
提交
2865e546
authored
9月 12, 2015
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update from code review
上级
cd442e56
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
29 行增加
和
21 行删除
+29
-21
nanguardmode.py
theano/compile/nanguardmode.py
+29
-21
没有找到文件。
theano/compile/nanguardmode.py
浏览文件 @
2865e546
from
__future__
import
print_function
import
collections
import
collections
import
logging
import
logging
from
six.moves
import
StringIO
import
numpy
as
np
import
numpy
as
np
import
theano
import
theano
...
@@ -25,7 +27,7 @@ AddConfigVar('NanGuardMode.big_is_error',
...
@@ -25,7 +27,7 @@ AddConfigVar('NanGuardMode.big_is_error',
in_c_key
=
False
)
in_c_key
=
False
)
AddConfigVar
(
'NanGuardMode.action'
,
AddConfigVar
(
'NanGuardMode.action'
,
"What NanGuardMode do
when it find
a problem"
,
"What NanGuardMode do
es when it finds
a problem"
,
EnumStr
(
'raise'
,
'warn'
,
'pdb'
),
EnumStr
(
'raise'
,
'warn'
,
'pdb'
),
in_c_key
=
False
)
in_c_key
=
False
)
...
@@ -60,14 +62,15 @@ def flatten(l):
...
@@ -60,14 +62,15 @@ def flatten(l):
return
rval
return
rval
def
contains_nan
(
arr
,
n
d
=
None
):
def
contains_nan
(
arr
,
n
ode
=
None
):
"""
"""
Test whether a numpy.ndarray contains any `np.nan` values.
Test whether a numpy.ndarray contains any `np.nan` values.
Parameters
Parameters
----------
----------
arr : np.ndarray or output of any Theano op
arr : np.ndarray or output of any Theano op
nd : if the output of an Theano op, the node associated to it
node : None or an Apply instance.
If arr is the output of a Theano op, the node associated to it.
Returns
Returns
-------
-------
...
@@ -91,7 +94,7 @@ def contains_nan(arr, nd=None):
...
@@ -91,7 +94,7 @@ def contains_nan(arr, nd=None):
elif
cuda
.
cuda_available
and
isinstance
(
arr
,
cuda
.
CudaNdarray
):
elif
cuda
.
cuda_available
and
isinstance
(
arr
,
cuda
.
CudaNdarray
):
if
(
hasattr
(
theano
.
sandbox
,
'rng_mrg'
)
and
if
(
hasattr
(
theano
.
sandbox
,
'rng_mrg'
)
and
isinstance
(
isinstance
(
n
d
.
op
,
n
ode
.
op
,
# It store ints in float container
# It store ints in float container
theano
.
sandbox
.
rng_mrg
.
GPU_mrg_uniform
)):
theano
.
sandbox
.
rng_mrg
.
GPU_mrg_uniform
)):
return
False
return
False
...
@@ -102,14 +105,16 @@ def contains_nan(arr, nd=None):
...
@@ -102,14 +105,16 @@ def contains_nan(arr, nd=None):
return
np
.
isnan
(
np
.
min
(
arr
))
return
np
.
isnan
(
np
.
min
(
arr
))
def
contains_inf
(
arr
,
n
d
=
None
):
def
contains_inf
(
arr
,
n
ode
=
None
):
"""
"""
Test whether a numpy.ndarray contains any `np.inf` values.
Test whether a numpy.ndarray contains any `np.inf` values.
Parameters
Parameters
----------
----------
arr : np.ndarray or output of any Theano op
arr : np.ndarray or output of any Theano op
nd : if the output of an Theano op, the node associated to it
node : None or an Apply instance.
If the output of a Theano op, the node associated to it.
Returns
Returns
-------
-------
contains_inf : bool
contains_inf : bool
...
@@ -133,7 +138,7 @@ def contains_inf(arr, nd=None):
...
@@ -133,7 +138,7 @@ def contains_inf(arr, nd=None):
elif
cuda
.
cuda_available
and
isinstance
(
arr
,
cuda
.
CudaNdarray
):
elif
cuda
.
cuda_available
and
isinstance
(
arr
,
cuda
.
CudaNdarray
):
if
(
hasattr
(
theano
.
sandbox
,
'rng_mrg'
)
and
if
(
hasattr
(
theano
.
sandbox
,
'rng_mrg'
)
and
isinstance
(
isinstance
(
n
d
.
op
,
n
ode
.
op
,
# It store ints in float container
# It store ints in float container
theano
.
sandbox
.
rng_mrg
.
GPU_mrg_uniform
)):
theano
.
sandbox
.
rng_mrg
.
GPU_mrg_uniform
)):
return
False
return
False
...
@@ -247,13 +252,14 @@ class NanGuardMode(Mode):
...
@@ -247,13 +252,14 @@ class NanGuardMode(Mode):
"""
"""
error
=
False
error
=
False
sio
=
StringIO
()
if
nan_is_error
:
if
nan_is_error
:
if
contains_nan
(
var
,
nd
):
if
contains_nan
(
var
,
nd
):
logger
.
error
(
'NaN detected'
)
print
(
'NaN detected'
,
file
=
sio
)
error
=
True
error
=
True
if
inf_is_error
:
if
inf_is_error
:
if
contains_inf
(
var
,
nd
):
if
contains_inf
(
var
,
nd
):
logger
.
error
(
'Inf detected'
)
print
(
'Inf detected'
,
file
=
sio
)
error
=
True
error
=
True
if
big_is_error
:
if
big_is_error
:
err
=
False
err
=
False
...
@@ -268,27 +274,29 @@ class NanGuardMode(Mode):
...
@@ -268,27 +274,29 @@ class NanGuardMode(Mode):
else
:
else
:
err
=
(
np
.
abs
(
var
)
.
max
()
>
1e10
)
err
=
(
np
.
abs
(
var
)
.
max
()
>
1e10
)
if
err
:
if
err
:
logger
.
error
(
'Big value detected'
)
print
(
'Big value detected'
,
file
=
sio
)
error
=
True
error
=
True
if
error
:
if
error
:
if
not
is_input
:
if
not
is_input
:
logger
.
error
(
"NanGuardMode found an error in the"
print
(
"NanGuardMode found an error in the"
" output of a node in this variable:"
)
" output of a node in this variable:"
,
file
=
sio
)
logger
.
error
(
theano
.
printing
.
debugprint
(
nd
,
file
=
'str'
)
)
print
(
theano
.
printing
.
debugprint
(
nd
,
file
=
'str'
),
file
=
sio
)
else
:
else
:
logger
.
error
(
"NanGuardMode found an error in the"
print
(
"NanGuardMode found an error in the"
" input
%
d of this node."
)
" input
%
d of this node."
,
file
=
sio
)
logger
.
error
(
'Node:'
)
print
(
'Node:'
,
file
=
sio
)
logger
.
error
(
nd
)
print
(
nd
,
file
=
sio
)
logger
.
error
(
"The input variable that cause problem:"
)
print
(
"The input variable that cause problem:"
,
file
=
sio
)
logger
.
error
(
theano
.
printing
.
debugprint
(
nd
,
file
=
'str'
))
print
(
theano
.
printing
.
debugprint
(
nd
,
file
=
'str'
),
file
=
sio
)
msg
=
sio
.
getvalue
()
if
config
.
NanGuardMode
.
action
==
'raise'
:
if
config
.
NanGuardMode
.
action
==
'raise'
:
assert
False
raise
AssertionError
(
msg
)
elif
config
.
NanGuardMode
.
action
==
'pdb'
:
elif
config
.
NanGuardMode
.
action
==
'pdb'
:
print
(
msg
)
import
pdb
import
pdb
pdb
.
set_trace
()
pdb
.
set_trace
()
elif
config
.
NanGuardMode
.
action
==
'warn'
:
elif
config
.
NanGuardMode
.
action
==
'warn'
:
pass
# already printed
logger
.
error
(
msg
)
def
nan_check
(
i
,
node
,
fn
):
def
nan_check
(
i
,
node
,
fn
):
"""
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论