Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c048e255
提交
c048e255
authored
4月 08, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
d49b5368
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
17 行增加
和
17 行删除
+17
-17
gradient.py
theano/gradient.py
+17
-17
没有找到文件。
theano/gradient.py
浏览文件 @
c048e255
...
@@ -6,7 +6,7 @@ import logging
...
@@ -6,7 +6,7 @@ import logging
import
time
import
time
import
warnings
import
warnings
import
numpy
# for numeric_grad
import
numpy
as
np
# for numeric_grad
from
six
import
itervalues
from
six
import
itervalues
import
theano
import
theano
...
@@ -1374,9 +1374,9 @@ class numeric_grad(object):
...
@@ -1374,9 +1374,9 @@ class numeric_grad(object):
type_eps
=
{
'float64'
:
1e-7
,
type_eps
=
{
'float64'
:
1e-7
,
'float32'
:
3e-4
,
'float32'
:
3e-4
,
'float16'
:
1e-1
,
'float16'
:
1e-1
,
n
umpy
.
dtype
(
'float64'
):
1e-7
,
n
p
.
dtype
(
'float64'
):
1e-7
,
n
umpy
.
dtype
(
'float32'
):
3e-4
,
n
p
.
dtype
(
'float32'
):
3e-4
,
n
umpy
.
dtype
(
'float16'
):
1e-1
}
n
p
.
dtype
(
'float16'
):
1e-1
}
def
__init__
(
self
,
f
,
pt
,
eps
=
None
,
out_type
=
None
):
def
__init__
(
self
,
f
,
pt
,
eps
=
None
,
out_type
=
None
):
"""Return the gradient of f at pt.
"""Return the gradient of f at pt.
...
@@ -1406,7 +1406,7 @@ class numeric_grad(object):
...
@@ -1406,7 +1406,7 @@ class numeric_grad(object):
pt
=
[
pt
]
pt
=
[
pt
]
packed_pt
=
True
packed_pt
=
True
apt
=
[
n
umpy
.
array
(
p
)
for
p
in
pt
]
apt
=
[
n
p
.
array
(
p
)
for
p
in
pt
]
shapes
=
[
p
.
shape
for
p
in
apt
]
shapes
=
[
p
.
shape
for
p
in
apt
]
dtypes
=
[
str
(
p
.
dtype
)
for
p
in
apt
]
dtypes
=
[
str
(
p
.
dtype
)
for
p
in
apt
]
...
@@ -1423,12 +1423,12 @@ class numeric_grad(object):
...
@@ -1423,12 +1423,12 @@ class numeric_grad(object):
(
self
.
type_eps
[
dt
],
dt
)
for
dt
in
dtypes
)[
1
]
(
self
.
type_eps
[
dt
],
dt
)
for
dt
in
dtypes
)[
1
]
# create un-initialized memory
# create un-initialized memory
x
=
n
umpy
.
ndarray
((
total_size
,),
dtype
=
working_dtype
)
x
=
n
p
.
ndarray
((
total_size
,),
dtype
=
working_dtype
)
# (not out_type is None) --> (out_type is not None) ???
# (not out_type is None) --> (out_type is not None) ???
if
(
out_type
is
not
None
)
and
(
out_type
.
startswith
(
'complex'
)):
if
(
out_type
is
not
None
)
and
(
out_type
.
startswith
(
'complex'
)):
gx
=
n
umpy
.
ndarray
((
total_size
,),
dtype
=
out_type
)
gx
=
n
p
.
ndarray
((
total_size
,),
dtype
=
out_type
)
else
:
else
:
gx
=
n
umpy
.
ndarray
((
total_size
,),
dtype
=
working_dtype
)
gx
=
n
p
.
ndarray
((
total_size
,),
dtype
=
working_dtype
)
if
eps
is
None
:
if
eps
is
None
:
eps
=
builtins
.
max
(
self
.
type_eps
[
dt
]
for
dt
in
dtypes
)
eps
=
builtins
.
max
(
self
.
type_eps
[
dt
]
for
dt
in
dtypes
)
...
@@ -1483,13 +1483,13 @@ class numeric_grad(object):
...
@@ -1483,13 +1483,13 @@ class numeric_grad(object):
The tuple (abs_err, rel_err) is returned
The tuple (abs_err, rel_err) is returned
"""
"""
abs_err
=
abs
(
a
-
b
)
abs_err
=
abs
(
a
-
b
)
rel_err
=
abs_err
/
n
umpy
.
maximum
(
abs
(
a
)
+
abs
(
b
),
1e-8
)
rel_err
=
abs_err
/
n
p
.
maximum
(
abs
(
a
)
+
abs
(
b
),
1e-8
)
# The numpy.asarray are needed as if a or b is a sparse matrix
# The numpy.asarray are needed as if a or b is a sparse matrix
# this would result in a numpy.matrix and not a numpy.ndarray
# this would result in a numpy.matrix and not a numpy.ndarray
# and the behave differently causing problem later.
# and the behave differently causing problem later.
# In particular a_npy_matrix.flatten().shape == (1, n_element)
# In particular a_npy_matrix.flatten().shape == (1, n_element)
abs_err
=
n
umpy
.
asarray
(
abs_err
)
abs_err
=
n
p
.
asarray
(
abs_err
)
rel_err
=
n
umpy
.
asarray
(
rel_err
)
rel_err
=
n
p
.
asarray
(
rel_err
)
return
(
abs_err
,
rel_err
)
return
(
abs_err
,
rel_err
)
def
abs_rel_errors
(
self
,
g_pt
):
def
abs_rel_errors
(
self
,
g_pt
):
...
@@ -1530,11 +1530,11 @@ class numeric_grad(object):
...
@@ -1530,11 +1530,11 @@ class numeric_grad(object):
abs_rel_errs
=
self
.
abs_rel_errors
(
g_pt
)
abs_rel_errs
=
self
.
abs_rel_errors
(
g_pt
)
for
abs_err
,
rel_err
in
abs_rel_errs
:
for
abs_err
,
rel_err
in
abs_rel_errs
:
if
not
n
umpy
.
all
(
numpy
.
isfinite
(
abs_err
)):
if
not
n
p
.
all
(
np
.
isfinite
(
abs_err
)):
raise
ValueError
(
'abs_err not finite'
,
repr
(
abs_err
))
raise
ValueError
(
'abs_err not finite'
,
repr
(
abs_err
))
if
not
n
umpy
.
all
(
numpy
.
isfinite
(
rel_err
)):
if
not
n
p
.
all
(
np
.
isfinite
(
rel_err
)):
raise
ValueError
(
'rel_err not finite'
,
repr
(
rel_err
))
raise
ValueError
(
'rel_err not finite'
,
repr
(
rel_err
))
scaled_err
=
n
umpy
.
minimum
(
abs_err
/
abs_tol
,
rel_err
/
rel_tol
)
scaled_err
=
n
p
.
minimum
(
abs_err
/
abs_tol
,
rel_err
/
rel_tol
)
max_i
=
scaled_err
.
argmax
()
max_i
=
scaled_err
.
argmax
()
pos
.
append
(
max_i
)
pos
.
append
(
max_i
)
...
@@ -1543,7 +1543,7 @@ class numeric_grad(object):
...
@@ -1543,7 +1543,7 @@ class numeric_grad(object):
rel_errs
.
append
(
rel_err
.
flatten
()[
max_i
])
rel_errs
.
append
(
rel_err
.
flatten
()[
max_i
])
# max over the arrays in g_pt
# max over the arrays in g_pt
max_arg
=
n
umpy
.
argmax
(
errs
)
max_arg
=
n
p
.
argmax
(
errs
)
max_pos
=
pos
[
max_arg
]
max_pos
=
pos
[
max_arg
]
return
(
max_arg
,
max_pos
,
abs_errs
[
max_arg
],
rel_errs
[
max_arg
])
return
(
max_arg
,
max_pos
,
abs_errs
[
max_arg
],
rel_errs
[
max_arg
])
...
@@ -1609,7 +1609,7 @@ def verify_grad(fun, pt, n_tests=2, rng=None, eps=None,
...
@@ -1609,7 +1609,7 @@ def verify_grad(fun, pt, n_tests=2, rng=None, eps=None,
import
theano.tensor
import
theano.tensor
from
theano.tensor
import
as_tensor_variable
,
TensorType
from
theano.tensor
import
as_tensor_variable
,
TensorType
assert
isinstance
(
pt
,
(
list
,
tuple
))
assert
isinstance
(
pt
,
(
list
,
tuple
))
pt
=
[
n
umpy
.
array
(
p
)
for
p
in
pt
]
pt
=
[
n
p
.
array
(
p
)
for
p
in
pt
]
for
i
,
p
in
enumerate
(
pt
):
for
i
,
p
in
enumerate
(
pt
):
if
p
.
dtype
not
in
(
'float16'
,
'float32'
,
'float64'
):
if
p
.
dtype
not
in
(
'float16'
,
'float32'
,
'float64'
):
...
@@ -1672,7 +1672,7 @@ def verify_grad(fun, pt, n_tests=2, rng=None, eps=None,
...
@@ -1672,7 +1672,7 @@ def verify_grad(fun, pt, n_tests=2, rng=None, eps=None,
def
random_projection
():
def
random_projection
():
plain
=
rng
.
rand
(
*
o_fn_out
.
shape
)
+
0.5
plain
=
rng
.
rand
(
*
o_fn_out
.
shape
)
+
0.5
if
cast_to_output_type
and
o_output
.
dtype
==
"float32"
:
if
cast_to_output_type
and
o_output
.
dtype
==
"float32"
:
return
n
umpy
.
array
(
plain
,
o_output
.
dtype
)
return
n
p
.
array
(
plain
,
o_output
.
dtype
)
return
plain
return
plain
t_r
=
shared
(
random_projection
())
t_r
=
shared
(
random_projection
())
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论