Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
870174e4
提交
870174e4
authored
6月 07, 2008
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
差异文件
merged
上级
ed634a9a
1f2d94f5
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
100 行增加
和
0 行删除
+100
-0
__init__.py
sandbox/__init__.py
+0
-0
wraplinker.py
sandbox/wraplinker.py
+100
-0
没有找到文件。
sandbox/__init__.py
0 → 100644
浏览文件 @
870174e4
sandbox/wraplinker.py
0 → 100644
浏览文件 @
870174e4
from
__future__
import
absolute_import
import
numpy
from
..gof.link
import
WrapLinker
from
..gradient
import
numeric_grad
class
Todo
(
Exception
):
"""todo"""
#WrapLinker wrappers
def
cmp_outputs
(
i
,
node
,
*
thunks
):
"""WrapLinker wrapper: raise an exception if outputs are different
numpy.ndarrays of floating point types are compared approximately, rather
than exactly.
"""
class
MisMatch
(
Exception
):
"""Output mismatch"""
#define a comparison function, which works for all the results in a graph
#TODO: consider factoring this out (and maybe passing args explicitly
# instead of by closure)
def
my_check_equal
(
x
,
y
):
x
,
y
=
x
[
0
],
y
[
0
]
if
type
(
x
)
!=
type
(
y
):
raise
MisMatch
(
"Output type mismatch"
,
(
x
,
y
))
if
hasattr
(
x
,
'dtype'
):
# was: isinstance(x,numpy.ndarray), which doesn't
# catch numpy.float64
if
x
.
dtype
!=
y
.
dtype
or
x
.
shape
!=
y
.
shape
:
raise
MisMatch
(
"ndarray type/shape."
,
(
x
,
y
))
if
str
(
x
.
dtype
)
.
startswith
(
'float'
):
assert
str
(
x
.
dtype
)
==
'float64'
#otherwise we need to adjust
#our constant below... but to what?
abs_rel_err
=
numeric_grad
.
abs_rel_err
(
x
,
y
)
max_abs_rel_err
=
numpy
.
max
(
abs_rel_err
)
if
max_abs_rel_err
>
1.0e-7
:
raise
MisMatch
(
'max_abs_rel_err exceeds tolerence'
,
(
max_abs_rel_err
,
x
,
y
))
elif
str
(
x
.
dtype
)
.
startswith
(
'complex'
):
raise
Todo
()
else
:
if
not
numpy
.
all
(
x
==
y
):
raise
MisMatch
else
:
print
'wtf??'
,
type
(
x
),
type
(
y
),
node
.
op
if
x
!=
y
:
print
'wow!! wtf??'
raise
MisMatch
(
"Output mismatch."
,
(
x
,
y
))
#loop over all the thunks
# ensure that the outputs from the first thunk match the outputs from
# all subsequent thunks
n_thunks
=
len
(
thunks
)
if
n_thunks
>
1
:
th0
=
thunks
[
0
]
for
th
in
thunks
[
1
:]:
for
out0
,
outN
in
zip
(
th0
.
outputs
,
th
.
outputs
):
my_check_equal
(
out0
[
0
],
outN
[
0
])
def
numpy_wrapper
(
f
):
def
wrapper
(
i
,
node
,
*
thunks
):
"""WrapLinker wrapper: raise an exception if a NaN is found in outputs
"""
for
thunk
in
thunks
:
for
output
in
thunk
.
outputs
:
if
hasattr
(
output
,
'dtype'
):
if
f
(
output
)):
raise
Exception
(
'uh oh'
,
(
thunk
,
output
))
return
wrapper
numpy_any_isinf
=
numpy_wrapper
(
lambda
a
:
numpy
.
any
(
numpy
.
isinf
(
a
)))
numpy_any_isnan
=
numpy_wrapper
(
lambda
a
:
numpy
.
any
(
numpy
.
isnan
(
a
)))
numpy_all_isfinite
=
numpy_wrapper
(
lambda
a
:
numpy
.
all
(
numpy
.
isfinite
(
a
)))
def
run_all
(
i
,
node
,
*
thunks
):
"""WrapLinker wrapper: run the thunks
"""
for
th
in
thunks
:
th
()
def
WrapLinkerMany
(
linkers
,
wrappers
):
""" Variant on WrapLinker that runs a series of wrapper functions instead of
just one.
"""
def
wrapper
(
*
args
):
for
f
in
wrappers
:
f
(
*
args
)
return
WrapLinker
(
linkers
,
wrapper
)
def
DualLinker
(
linkers
):
return
WrapLinkerMany
(
linkers
,
[
run_all
,
cmp_outputs
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论