Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
44c3f742
提交
44c3f742
authored
5月 17, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
numeric_grad - refactoring and commenting, added new tolerance defaults with explanation in code
上级
1adbf0a3
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
55 行增加
和
7 行删除
+55
-7
basic.py
theano/tensor/basic.py
+55
-7
没有找到文件。
theano/tensor/basic.py
浏览文件 @
44c3f742
...
@@ -3811,8 +3811,34 @@ def grad(cost, wrt, g_cost=None, consider_constant=[], warn_type=False):
...
@@ -3811,8 +3811,34 @@ def grad(cost, wrt, g_cost=None, consider_constant=[], warn_type=False):
class
numeric_grad
:
class
numeric_grad
:
"""WRITEME"""
"""WRITEME"""
# Note on step sizes and tolerances:
#
# There is a relationship between the step size and the function value and the measurement
# error that is incurred due to rounding. The finite difference we measure is
# delta = f(x0) - f(x0+eps)
#
# For maximum precision, f should be close to zero.
# For every power of 2 that f departs from zero, we lose a bit of precision in delta.
#
# Even in this case of maximum accuracy, there is a tradeoff between stepsize and
# measurement error.
# Taking small steps allows us to measure large derivatives accuractly, but longer steps
# are required to measure small derivatives accurately. However longer steps introduce
# bias into our measurement in general for non-linear functions.
#
# It would be interesting to have a version of numeric grad that used an adaptive stepsize.
#
# For now, we use a heuristic that catches very bad gradients, but is not perfectly
# accurate.
type_eps
=
{
'float64'
:
1e-7
,
type_eps
=
{
'float64'
:
1e-7
,
'float32'
:
3e-3
}
'float32'
:
3e-3
,
numpy
.
dtype
(
'float64'
):
1e-7
,
numpy
.
dtype
(
'float32'
):
3e-3
}
type_tol
=
{
'float64'
:
1e-4
,
numpy
.
dtype
(
'float64'
):
1e-4
,
'float32'
:
1e-1
,
numpy
.
dtype
(
'float32'
):
1e-1
}
def
__init__
(
self
,
f
,
pt
,
eps
=
None
):
def
__init__
(
self
,
f
,
pt
,
eps
=
None
):
"""Return the gradient of f at pt.
"""Return the gradient of f at pt.
...
@@ -3888,12 +3914,29 @@ class numeric_grad:
...
@@ -3888,12 +3914,29 @@ class numeric_grad:
self
.
gf
=
self
.
gf
[
0
]
self
.
gf
=
self
.
gf
[
0
]
@staticmethod
@staticmethod
def
abs_rel_err
(
a
,
b
,
eps
=
1.0e-10
):
def
abs_rel_err
(
a
,
b
,
tol
=
None
):
"""Return a small number when a and b are close, relative to how big they are"""
"""Return a small number when a and b are close, relative to how big they are.
return
abs
(
a
-
b
)
/
(
abs
(
a
)
+
abs
(
b
)
+
eps
)
def
max_err
(
self
,
g_pt
):
Formula used: a - b / (abs(a) + abs(b) + tol)
"""Return the biggest relative error between g_pt and self.gf"""
`tol` defaults to relatively generous value that is suitable for catching completely
wrong gradients. (`self.type_tol`
"""
if
tol
is
None
:
tol
=
__builtin__
.
max
(
numeric_grad
.
type_tol
[
a
.
dtype
],
numeric_grad
.
type_tol
[
b
.
dtype
])
return
abs
(
a
-
b
)
/
(
abs
(
a
)
+
abs
(
b
)
+
tol
)
def
abs_rel_errors
(
self
,
g_pt
):
"""Return the abs rel error of gradient estimate `g_pt`
`g_pt` must be a list of ndarrays of the same length as self.gf, otherwise a ValueError
is raised.
Corresponding ndarrays in `g_pt` and `self.gf` must have the same shape or ValueError
is raised.
"""
if
len
(
g_pt
)
!=
len
(
self
.
gf
):
if
len
(
g_pt
)
!=
len
(
self
.
gf
):
raise
ValueError
(
'argument has wrong number of elements'
,
len
(
g_pt
))
raise
ValueError
(
'argument has wrong number of elements'
,
len
(
g_pt
))
errs
=
[]
errs
=
[]
...
@@ -3901,7 +3944,12 @@ class numeric_grad:
...
@@ -3901,7 +3944,12 @@ class numeric_grad:
if
a
.
shape
!=
b
.
shape
:
if
a
.
shape
!=
b
.
shape
:
raise
ValueError
(
'argument element
%
i has wrong shape
%
s'
%
(
i
,
str
((
a
.
shape
,
raise
ValueError
(
'argument element
%
i has wrong shape
%
s'
%
(
i
,
str
((
a
.
shape
,
b
.
shape
))))
b
.
shape
))))
errs
.
append
(
numpy
.
max
(
numeric_grad
.
abs_rel_err
(
a
,
b
)))
errs
.
append
(
numeric_grad
.
abs_rel_err
(
a
,
b
))
return
errs
def
max_err
(
self
,
g_pt
):
"""Return the biggest relative error between g_pt and self.gf"""
errs
=
[
numpy
.
max
(
e
)
for
e
in
self
.
abs_rel_errors
(
g_pt
)]
if
numpy
.
all
(
numpy
.
isfinite
(
errs
)):
if
numpy
.
all
(
numpy
.
isfinite
(
errs
)):
return
numpy
.
max
(
errs
),
numpy
.
argmax
(
errs
)
return
numpy
.
max
(
errs
),
numpy
.
argmax
(
errs
)
else
:
else
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论