Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7681f4c7
提交
7681f4c7
authored
11月 13, 2012
作者:
Ian Goodfellow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added known_grads parameter to grad function
上级
26520161
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
77 行增加
和
33 行删除
+77
-33
gradient.py
theano/gradient.py
+77
-33
没有找到文件。
theano/gradient.py
浏览文件 @
7681f4c7
...
...
@@ -384,9 +384,11 @@ def Lop(f, wrt, eval_points, consider_constant=None,
#########################
def
grad
(
cost
,
wrt
,
g_cost
=
None
,
consider_constant
=
None
,
disconnected_inputs
=
'raise'
,
add_names
=
True
):
disconnected_inputs
=
'raise'
,
add_names
=
True
,
known_grads
=
None
):
"""
:type cost: Scalar (0-dimensional) Variable.
May optionally be None if known_grads is provided.
:type wrt: Variable or list of Variables.
:type g_cost: Scalar Variable, or None.
:param g_cost: an expression for the gradient through cost. The default is
...
...
@@ -407,6 +409,12 @@ def grad(cost, wrt, g_cost=None, consider_constant=None,
(d<cost.name>/d<wrt.name>) provided that both cost and wrt have
names
:type known_grads: dict
:param known_grads: If not None, a dictionary mapping variables to their
gradients. This is useful in the case where you know the
gradient on some variables but do not know the original
cost.
:rtype: Variable or list/tuple of Variables (depending upon `wrt`)
:return: symbolic expression of gradient of `cost` with respect to `wrt`.
...
...
@@ -420,12 +428,15 @@ def grad(cost, wrt, g_cost=None, consider_constant=None,
if
tensor
is
None
:
from
theano
import
tensor
if
isinstance
(
cost
.
type
,
NullType
):
if
cost
is
None
:
assert
known_grads
is
not
None
if
cost
is
not
None
and
isinstance
(
cost
.
type
,
NullType
):
raise
ValueError
(
"Can't differentiate a NaN cost."
"cost is NaN because "
+
\
cost
.
type
.
why_null
)
if
cost
.
ndim
!=
0
:
if
cost
is
not
None
and
cost
.
ndim
!=
0
:
raise
TypeError
(
"cost must be a scalar."
)
...
...
@@ -444,7 +455,14 @@ def grad(cost, wrt, g_cost=None, consider_constant=None,
raise
TypeError
(
"Expected Variable, got "
+
str
(
elem
)
+
" of type "
+
str
(
type
(
elem
)))
var_to_node_to_idx
=
_populate_var_to_node_to_idx
([
cost
],
wrt
,
consider_constant
)
outputs
=
[]
if
cost
is
not
None
:
outputs
.
append
(
cost
)
if
known_grads
is
not
None
:
outputs
.
extend
(
known_grads
.
keys
())
var_to_node_to_idx
=
_populate_var_to_node_to_idx
(
outputs
,
wrt
,
consider_constant
)
# build a dict mapping var to the gradient of cost with respect to var
grad_dict
=
{}
...
...
@@ -452,38 +470,63 @@ def grad(cost, wrt, g_cost=None, consider_constant=None,
# The gradient of the cost should default to 1 if the cost is of a
# continuous dtype (float, for the moment, as complex are unsupported),
# and should always be 0 if the cost is of discrete (integer) dtype.
if
getattr
(
cost
.
type
,
'dtype'
,
None
)
not
in
tensor
.
float_dtypes
:
if
g_cost
is
not
None
:
try
:
cval
=
theano
.
get_constant_value
(
g_cost
)
if
cval
==
0
:
g_cost_is_zero
=
True
else
:
if
cost
is
not
None
:
if
getattr
(
cost
.
type
,
'dtype'
,
None
)
not
in
tensor
.
float_dtypes
:
if
g_cost
is
not
None
:
try
:
cval
=
theano
.
get_constant_value
(
g_cost
)
if
cval
==
0
:
g_cost_is_zero
=
True
else
:
g_cost_is_zero
=
False
except
TypeError
:
g_cost_is_zero
=
False
except
TypeError
:
g_cost_is_zero
=
False
if
not
g_cost_is_zero
:
raise
ValueError
(
"The gradient of a cost of non-continuous
"
"
dtype (here,
%
s), if it is defined, should be 0.
"
"
However, a value of
%
s was provided in the
'g_cost' "
"argument of theano.grad(). To remove this error,
"
"you can simply omit the 'g_cost' argument, or "
if
not
g_cost_is_zero
:
raise
ValueError
(
"The gradient of a cost of non-continuous dtype "
"(here,
%
s), if it is defined, should be 0.
"
"
However, a value of
%
s was provided in the
"
"'g_cost' "
"argument of theano.grad(). To remove this error,"
"
you can simply omit the 'g_cost' argument, or "
"give it the default value of None."
%
(
getattr
(
g_cost
.
type
,
'dtype'
,
'no dtype defined'
),
g_cost
))
g_cost
=
tensor
.
zeros_like
(
cost
)
getattr
(
g_cost
.
type
,
'dtype'
,
'no dtype defined'
),
g_cost
))
g_cost
=
tensor
.
zeros_like
(
cost
)
elif
g_cost
is
None
:
# cost.type.dtype is in tensor.float_dtypes at that point
g_cost
=
tensor
.
ones_like
(
cost
)
elif
g_cost
is
None
:
# cost.type.dtype is in tensor.float_dtypes at that point
g_cost
=
tensor
.
ones_like
(
cost
)
else
:
# Cast the provided gradient so that it has the same dtype
# as the cost.
g_cost
=
g_cost
.
astype
(
cost
.
type
.
dtype
)
grad_dict
[
cost
]
=
g_cost
else
:
# Cast the provided gradient so that it has the same dtype
# as the cost.
g_cost
=
g_cost
.
astype
(
cost
.
type
.
dtype
)
if
g_cost
is
not
None
:
raise
ValueError
(
"No cost node was specified, but a gradient"
" on it was."
)
if
known_grads
is
not
None
:
for
var
in
known_grads
:
g_var
=
known_grads
[
var
]
if
not
hasattr
(
g_var
,
'type'
):
raise
TypeError
(
'output grads must be theano variables.'
'Ambiguous whether
%
s should be made into tensor'
' or sparse theano variable'
%
str
(
type
(
g_var
)))
if
g_var
.
type
not
in
[
NullType
,
DisconnectedType
]
and
'float'
\
not
in
str
(
g_var
.
type
.
dtype
):
raise
TypeError
(
"Gradients must always be NullType, "
"DisconnectedType, or continuous."
)
grad_dict
[
var
]
=
g_var
grad_dict
[
cost
]
=
g_cost
# variables that do not influence the cost have zero gradient.
# if wrt is such a variable, populate the grad_dict with this info
...
...
@@ -573,7 +616,7 @@ def _node_to_pattern(node):
def
_populate_var_to_node_to_idx
(
outputs
,
wrt
,
consider_constant
):
"""
Common code shared between grad and grad_sources_inputs
Helper function for grad function.
outputs: a list of variables we want to take gradients of
...
...
@@ -713,7 +756,7 @@ def _populate_var_to_node_to_idx(outputs, wrt, consider_constant):
def
_populate_grad_dict
(
var_to_node_to_idx
,
grad_dict
,
wrt
,
cost_name
=
None
):
"""
Common code shared between grad_sources_inputs and grad
Helper function for grad function.
var_to_node_to_idx: a dictionary mapping a variable to
a second dictionary.
...
...
@@ -722,7 +765,7 @@ def _populate_grad_dict(var_to_node_to_idx,
node's input list
grad_dict: a dictionary mapping variables to their gradients
should be populated by grad
or grad_sources_inputs
should be populated by grad
function.
grad should set gradients to DisconnectedType()() for
variables to be considered constant, set the
...
...
@@ -873,6 +916,7 @@ def _populate_grad_dict(var_to_node_to_idx,
'the grad_undefined or grad_unimplemented helper '
'functions.'
)
%
node
.
op
)
if
not
isinstance
(
term
.
type
,
(
NullType
,
DisconnectedType
)):
if
term
.
type
.
dtype
not
in
theano
.
tensor
.
float_dtypes
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论