Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b9854ee7
提交
b9854ee7
authored
9月 25, 2017
作者:
abergeron
提交者:
GitHub
9月 25, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5641 from khaotik/ofg_lop
L_op for OpFromGraph
上级
2ecf9f1d
62d96903
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
183 行增加
和
72 行删除
+183
-72
builders.py
theano/compile/builders.py
+154
-71
test_builders.py
theano/compile/tests/test_builders.py
+29
-1
没有找到文件。
theano/compile/builders.py
浏览文件 @
b9854ee7
...
@@ -42,15 +42,39 @@ class OpFromGraph(gof.Op):
...
@@ -42,15 +42,39 @@ class OpFromGraph(gof.Op):
grad_overrides : single or list of {'default', OpFromGraph, callable, Variable with special type}, optional
grad_overrides : single or list of {'default', OpFromGraph, callable, Variable with special type}, optional
Defaults to ``'default'``.
Defaults to ``'default'``.
This argument is mutually exclusive with lop_overrides.
``'default'`` : Do not override, use default grad() result
``'default'`` : Do not override, use default grad() result
OpFromGraph instance : Override with another OpFromGraph, should
OpFromGraph instance : Override with another OpFromGraph, should
accept inputs as the same order and types of
"inputs" and "output_grads"
accept inputs as the same order and types of
``inputs`` and ``output_grads``
arguments as one would specify in grad() method.
arguments as one would specify in grad() method.
callable : similar to OpFromGraph instance, must return list of
callable : Should take two args: ``inputs`` and ``output_grads``.
:class:`Variable <theano.gof.Variable>`.
Each argument is expected to be a list of :class:`Variable <theano.gof.Variable>`.
Must return list of :class:`Variable <theano.gof.Variable>`.
Variable :
``NullType() instance`` : Treat as non-differentiable
``DisconnectedType() instance`` : Treat as disconnected gradient, numerically gives zero
list: Each OpFromGraph/callable must return a single
:class:`Variable <theano.gof.Variable>`. Each list element corresponds to gradient of
a specific input, length of list must be equal to number of inputs.
lop_overrides : single or list of {'default', OpFromGraph, callable, Variable with special type}, optional
Defaults to ``'default'``.
This argument is mutually exclusive with ``grad_overrides``.
``'default'`` : Do not override, use default L_op() result
OpFromGraph instance : Override with another OpFromGraph, should
accept inputs as the same order and types of ``inputs``, ``outputs`` and ``output_grads``
arguments as one would specify in grad() method.
callable : Should take three args: ``inputs``, ``outputs`` and ``output_grads``.
Each argument is expected to be a list of :class:`Variable <theano.gof.Variable>`.
Must return list of :class:`Variable <theano.gof.Variable>`.
Variable :
Variable :
``NullType() instance`` : Treat as non-differentiable
``NullType() instance`` : Treat as non-differentiable
...
@@ -66,11 +90,12 @@ class OpFromGraph(gof.Op):
...
@@ -66,11 +90,12 @@ class OpFromGraph(gof.Op):
``'default'`` : Do not override, use default R_op() result
``'default'`` : Do not override, use default R_op() result
OpFromGraph instance : Override with another OpFromGraph, should
OpFromGraph instance : Override with another OpFromGraph, should
accept inputs as the same order and types of
"inputs" and "output_grads"
accept inputs as the same order and types of
``inputs`` and ``eval_points``
arguments as one would specify in
grad
() method.
arguments as one would specify in
R_op
() method.
callable : similar to OpFromGraph instance, must return list of
callable : Should take two args: ``inputs`` and ``eval_points``.
:class:`Variable <theano.gof.Variable>`.
Each argument is expected to be a list of :class:`Variable <theano.gof.Variable>`.
Must return list of :class:`Variable <theano.gof.Variable>`.
Variable :
Variable :
``NullType() instance`` : Treat as non-differentiable
``NullType() instance`` : Treat as non-differentiable
...
@@ -96,7 +121,6 @@ class OpFromGraph(gof.Op):
...
@@ -96,7 +121,6 @@ class OpFromGraph(gof.Op):
local_outputs)
local_outputs)
- c_code() to remove the double overhead?
- c_code() to remove the double overhead?
- grad() make it support DisconnectedType and the new interface
- grad() make it support DisconnectedType and the new interface
- extend grad() to L_op
- add support for NullType and DisconnectedType when R_op supports them
- add support for NullType and DisconnectedType when R_op supports them
- check how it works with updates.
- check how it works with updates.
- add test with constant as input or inside the inner graph.
- add test with constant as input or inside the inner graph.
...
@@ -116,10 +140,10 @@ class OpFromGraph(gof.Op):
...
@@ -116,10 +140,10 @@ class OpFromGraph(gof.Op):
- ``inline=True`` will cause better runtime optimization at the cost
- ``inline=True`` will cause better runtime optimization at the cost
of compilation time. Currently only works with ``fast_compile`` or
of compilation time. Currently only works with ``fast_compile`` or
``fast_run`` mode.
``fast_run`` mode.
-
It's recommanded to provide pure functions (no side effects lik
e
-
For overriding, it's recommended to provide pure functions (no sid
e
setting global variable) as callable(s). The callable(s) supplied
effects like setting global variable) as callable(s). The callable(s)
for overriding gradient/rop will be called only once at the first
supplied for overriding gradient/rop will be called only once at the
call to grad/R_op, and will be converted to OpFromGraph instances.
first
call to grad/R_op, and will be converted to OpFromGraph instances.
Examples
Examples
--------
--------
...
@@ -171,6 +195,13 @@ class OpFromGraph(gof.Op):
...
@@ -171,6 +195,13 @@ class OpFromGraph(gof.Op):
fn(2., 3., 4.) # [1., 8., 3.]
fn(2., 3., 4.) # [1., 8., 3.]
"""
"""
TYPE_ERR_MSG
=
(
"L_op/gradient override should be (single or list of)"
"'default' | OpFromGraph | callable | Variable "
"with NullType or DisconnectedType, got
%
s"
)
STYPE_ERR_MSG
=
(
'Overriding Variable instance can only have type'
' of DisconnectedType or NullType, got
%
s'
)
LOP_TYPE_ERR_MSG
=
'L_op type can only be "grad" or "lop", got
%
s.'
OV_INP_LEN_ERR_MSG
=
'expect overrider with
%
d inputs, got
%
d'
@staticmethod
@staticmethod
def
_filter_grad_var
(
grad
,
inp
):
def
_filter_grad_var
(
grad
,
inp
):
...
@@ -182,9 +213,9 @@ class OpFromGraph(gof.Op):
...
@@ -182,9 +213,9 @@ class OpFromGraph(gof.Op):
# a grad() call could return instance of NullType() or DisconnectedType()
# a grad() call could return instance of NullType() or DisconnectedType()
# which cannot be directly used in OfG
# which cannot be directly used in OfG
#
#
# Since we always use an OfG instance as self._
grad
_op, the current
# Since we always use an OfG instance as self._
lop
_op, the current
# workaround is to "remember" the special cases of the gradient and
# workaround is to "remember" the special cases of the gradient and
# replace them after self._
grad
_op is called.
# replace them after self._
lop
_op is called.
#
#
# This helper function changes invalid types into a filtered_var,
# This helper function changes invalid types into a filtered_var,
# and provides a overrider_var to be replaced at grad() call
# and provides a overrider_var to be replaced at grad() call
...
@@ -214,7 +245,9 @@ class OpFromGraph(gof.Op):
...
@@ -214,7 +245,9 @@ class OpFromGraph(gof.Op):
def
__init__
(
def
__init__
(
self
,
inputs
,
outputs
,
self
,
inputs
,
outputs
,
inline
=
False
,
inline
=
False
,
grad_overrides
=
'default'
,
rop_overrides
=
'default'
,
lop_overrides
=
'default'
,
grad_overrides
=
'default'
,
rop_overrides
=
'default'
,
name
=
None
,
**
kwargs
name
=
None
,
**
kwargs
):
):
if
not
isinstance
(
outputs
,
list
):
if
not
isinstance
(
outputs
,
list
):
...
@@ -251,7 +284,18 @@ class OpFromGraph(gof.Op):
...
@@ -251,7 +284,18 @@ class OpFromGraph(gof.Op):
self
.
kwargs
=
kwargs
self
.
kwargs
=
kwargs
self
.
input_types
=
[
inp
.
type
for
inp
in
inputs
]
self
.
input_types
=
[
inp
.
type
for
inp
in
inputs
]
self
.
output_types
=
[
out
.
type
for
out
in
outputs
]
self
.
output_types
=
[
out
.
type
for
out
in
outputs
]
self
.
set_grad_overrides
(
grad_overrides
)
if
lop_overrides
!=
'default'
:
if
grad_overrides
!=
'default'
:
raise
ValueError
(
'lop_overrides and grad_overrides are mutually exclusive'
)
else
:
self
.
set_lop_overrides
(
lop_overrides
)
self
.
_lop_type
=
'lop'
elif
grad_overrides
!=
'default'
:
self
.
set_lop_overrides
(
grad_overrides
)
self
.
_lop_type
=
'grad'
else
:
self
.
set_lop_overrides
(
'default'
)
self
.
_lop_type
=
'lop'
self
.
set_rop_overrides
(
rop_overrides
)
self
.
set_rop_overrides
(
rop_overrides
)
if
name
is
not
None
:
if
name
is
not
None
:
...
@@ -272,20 +316,41 @@ class OpFromGraph(gof.Op):
...
@@ -272,20 +316,41 @@ class OpFromGraph(gof.Op):
return
'
%(name)
s{inline=
%(is_inline)
s}'
%
locals
()
return
'
%(name)
s{inline=
%(is_inline)
s}'
%
locals
()
@theano.change_flags
(
compute_test_value
=
'off'
)
@theano.change_flags
(
compute_test_value
=
'off'
)
def
_recompute_
grad
_op
(
self
):
def
_recompute_
lop
_op
(
self
):
'''
'''
converts self._
grad
_op from user supplied form to type(self) instance
converts self._
lop
_op from user supplied form to type(self) instance
'''
'''
local_inputs
=
self
.
local_inputs
local_inputs
=
self
.
local_inputs
local_outputs
=
self
.
local_outputs
local_outputs
=
self
.
local_outputs
inp_len
=
len
(
local_inputs
)
inp_len
=
len
(
local_inputs
)
grad_op
=
self
.
_grad
_op
lop_op
=
self
.
_lop
_op
if
isinstance
(
grad_op
,
OpFromGraph
):
if
isinstance
(
lop_op
,
OpFromGraph
):
if
not
self
.
_grad_op_is_cached
:
if
self
.
_lop_op_is_cached
:
self
.
_grad_op_is_cached
=
True
return
self
.
_grad_op_stypes_l
=
[
None
]
*
inp_len
assert
self
.
_lop_type
in
[
'lop'
,
'grad'
],
\
self
.
LOP_TYPE_ERR_MSG
%
self
.
_lop_type
if
self
.
_lop_type
==
'grad'
:
needed_ninps
=
inp_len
+
len
(
local_outputs
)
ninps
=
len
(
lop_op
.
local_inputs
)
if
needed_ninps
!=
ninps
:
raise
ValueError
(
self
.
OV_INP_LEN_ERR_MSG
%
(
needed_ninps
,
ninps
))
# make a wrapper callable
def
lop_op
(
inps
,
grads
):
return
self
.
_lop_op
(
*
(
inps
+
grads
))
elif
self
.
_lop_type
==
'lop'
:
# OfG can be directly used in L_op format
needed_ninps
=
inp_len
+
2
*
len
(
local_outputs
)
ninps
=
len
(
lop_op
.
local_inputs
)
if
needed_ninps
!=
ninps
:
raise
ValueError
(
self
.
OV_INP_LEN_ERR_MSG
%
(
needed_ninps
,
ninps
))
self
.
_lop_op_is_cached
=
True
self
.
_lop_op_stypes_l
=
[
None
]
*
inp_len
self
.
_lop_op
.
kwargs
[
'on_unused_input'
]
=
'ignore'
return
return
output_grads
=
[
out_t
()
for
out_t
in
self
.
output_types
]
output_grads
=
[
out_t
()
for
out_t
in
self
.
output_types
]
...
@@ -297,26 +362,28 @@ class OpFromGraph(gof.Op):
...
@@ -297,26 +362,28 @@ class OpFromGraph(gof.Op):
null_gradients
=
'return'
,
null_gradients
=
'return'
,
known_grads
=
OrderedDict
(
izip
(
local_outputs
,
output_grads
)))
known_grads
=
OrderedDict
(
izip
(
local_outputs
,
output_grads
)))
TYPE_ERR_MSG
=
(
"Gradient override should be (single or list of)"
assert
self
.
_lop_type
in
[
'lop'
,
'grad'
],
\
"'default' | OpFromGraph | callable | Variable "
self
.
LOP_TYPE_ERR_MSG
%
self
.
_lop_type
"with NullType or DisconnectedType, got
%
s"
)
if
self
.
_lop_type
==
'lop'
:
STYPE_ERR_MSG
=
(
'Overriding Variable instance can only have type'
callable_args
=
(
local_inputs
,
local_outputs
,
output_grads
)
' of DisconnectedType or NullType, got
%
s'
)
elif
self
.
_lop_type
==
'grad'
:
# we need to convert _grad_op into an OfG instance
callable_args
=
(
local_inputs
,
output_grads
)
if
grad_op
==
'default'
:
# we need to convert _lop_op into an OfG instance
if
lop_op
==
'default'
:
gdefaults_l
=
fn_grad
(
wrt
=
local_inputs
)
gdefaults_l
=
fn_grad
(
wrt
=
local_inputs
)
all_grads_l
,
all_grads_ov_l
=
izip
(
all_grads_l
,
all_grads_ov_l
=
izip
(
*
[
OpFromGraph
.
_filter_grad_var
(
grad
,
inp
)
for
grad
,
inp
in
izip
(
gdefaults_l
,
local_inputs
)])
*
[
OpFromGraph
.
_filter_grad_var
(
grad
,
inp
)
for
grad
,
inp
in
izip
(
gdefaults_l
,
local_inputs
)])
all_grads_l
=
list
(
all_grads_l
)
all_grads_l
=
list
(
all_grads_l
)
all_grads_ov_l
=
list
(
all_grads_ov_l
)
all_grads_ov_l
=
list
(
all_grads_ov_l
)
elif
isinstance
(
grad
_op
,
Variable
):
elif
isinstance
(
lop
_op
,
Variable
):
if
isinstance
(
grad
_op
.
type
,
(
DisconnectedType
,
NullType
)):
if
isinstance
(
lop
_op
.
type
,
(
DisconnectedType
,
NullType
)):
all_grads_l
=
[
inp
.
zeros_like
()
for
inp
in
local_inputs
]
all_grads_l
=
[
inp
.
zeros_like
()
for
inp
in
local_inputs
]
all_grads_ov_l
=
[
grad
_op
.
type
()
for
_
in
range
(
inp_len
)]
all_grads_ov_l
=
[
lop
_op
.
type
()
for
_
in
range
(
inp_len
)]
else
:
else
:
raise
ValueError
(
STYPE_ERR_MSG
%
grad
_op
.
type
)
raise
ValueError
(
self
.
STYPE_ERR_MSG
%
lop
_op
.
type
)
elif
isinstance
(
grad
_op
,
list
):
elif
isinstance
(
lop
_op
,
list
):
goverrides_l
=
grad
_op
goverrides_l
=
lop
_op
if
len
(
goverrides_l
)
!=
inp_len
:
if
len
(
goverrides_l
)
!=
inp_len
:
raise
ValueError
(
raise
ValueError
(
'Need to override
%
d gradients, got
%
d'
%
(
'Need to override
%
d gradients, got
%
d'
%
(
...
@@ -340,40 +407,41 @@ class OpFromGraph(gof.Op):
...
@@ -340,40 +407,41 @@ class OpFromGraph(gof.Op):
all_grads_l
.
append
(
inp
.
zeros_like
())
all_grads_l
.
append
(
inp
.
zeros_like
())
all_grads_ov_l
.
append
(
fn_gov
.
type
())
all_grads_ov_l
.
append
(
fn_gov
.
type
())
else
:
else
:
raise
ValueError
(
STYPE_ERR_MSG
%
fn_gov
.
type
)
raise
ValueError
(
self
.
STYPE_ERR_MSG
%
fn_gov
.
type
)
else
:
else
:
if
not
hasattr
(
fn_gov
,
'__call__'
):
if
not
callable
(
fn_gov
):
raise
TypeError
(
TYPE_ERR_MSG
%
fn_gov
)
raise
TypeError
(
self
.
TYPE_ERR_MSG
%
fn_gov
)
gov
,
gov_ov
=
OpFromGraph
.
_filter_grad_var
(
gov
,
gov_ov
=
OpFromGraph
.
_filter_grad_var
(
fn_gov
(
local_inputs
,
output_grad
s
),
inp
)
fn_gov
(
*
callable_arg
s
),
inp
)
all_grads_l
.
append
(
gov
)
all_grads_l
.
append
(
gov
)
all_grads_ov_l
.
append
(
gov_ov
)
all_grads_ov_l
.
append
(
gov_ov
)
else
:
else
:
# callable case
# callable case
if
not
hasattr
(
grad_op
,
'__call__'
):
if
not
callable
(
lop_op
):
raise
TypeError
(
TYPE_ERR_MSG
%
grad
_op
)
raise
TypeError
(
self
.
TYPE_ERR_MSG
%
lop
_op
)
goverrides_l
=
grad_op
(
local_inputs
,
output_grad
s
)
goverrides_l
=
lop_op
(
*
callable_arg
s
)
if
not
isinstance
(
goverrides_l
,
list
):
if
not
isinstance
(
goverrides_l
,
list
):
raise
TypeError
(
raise
TypeError
(
'Gradient overriding function should return a list, '
'Gradient
/L_op
overriding function should return a list, '
'got "
%
s"'
%
type
(
goverrides_l
))
'got "
%
s"'
%
type
(
goverrides_l
))
all_grads_l
,
all_grads_ov_l
=
izip
(
all_grads_l
,
all_grads_ov_l
=
izip
(
*
[
OpFromGraph
.
_filter_grad_var
(
grad
,
inp
)
*
[
OpFromGraph
.
_filter_grad_var
(
grad
,
inp
)
for
grad
,
inp
in
izip
(
goverrides_l
,
local_inputs
)])
for
grad
,
inp
in
izip
(
goverrides_l
,
local_inputs
)])
if
len
(
all_grads_l
)
!=
len
(
local_inputs
):
if
len
(
all_grads_l
)
!=
len
(
local_inputs
):
raise
ValueError
(
raise
ValueError
(
'Gradient overriding function should return list of '
'Gradient
/L_op
overriding function should return list of '
'
%
d outputs, got
%
d'
%
(
inp_len
,
len
(
all_grads_l
)))
'
%
d outputs, got
%
d'
%
(
inp_len
,
len
(
all_grads_l
)))
all_grads_l
=
list
(
all_grads_l
)
all_grads_l
=
list
(
all_grads_l
)
all_grads_ov_l
=
list
(
all_grads_ov_l
)
all_grads_ov_l
=
list
(
all_grads_ov_l
)
self
.
_
grad
_op
=
type
(
self
)(
self
.
_
lop
_op
=
type
(
self
)(
inputs
=
local_inputs
+
output_grads
,
inputs
=
local_inputs
+
local_outputs
+
output_grads
,
outputs
=
all_grads_l
,
outputs
=
all_grads_l
,
inline
=
self
.
is_inline
,
inline
=
self
.
is_inline
,
name
=
(
None
if
self
.
name
is
None
else
self
.
name
+
'_
grad'
),
name
=
(
None
if
self
.
name
is
None
else
self
.
name
+
'_
'
+
self
.
_lop_type
),
on_unused_input
=
'ignore'
)
on_unused_input
=
'ignore'
)
self
.
_grad_op_stypes_l
=
all_grads_ov_l
self
.
_lop_op_stypes_l
=
all_grads_ov_l
self
.
_grad_op_is_cached
=
True
self
.
_lop_op_is_cached
=
True
self
.
_lop_type
=
'lop'
@theano.change_flags
(
compute_test_value
=
'off'
)
@theano.change_flags
(
compute_test_value
=
'off'
)
def
_recompute_rop_op
(
self
):
def
_recompute_rop_op
(
self
):
...
@@ -448,14 +516,14 @@ class OpFromGraph(gof.Op):
...
@@ -448,14 +516,14 @@ class OpFromGraph(gof.Op):
else
:
else
:
raise
ValueError
(
STYPE_ERR_MSG
%
fn_rov
.
type
)
raise
ValueError
(
STYPE_ERR_MSG
%
fn_rov
.
type
)
else
:
else
:
if
not
hasattr
(
fn_rov
,
'__call__'
):
if
not
callable
(
fn_rov
):
raise
TypeError
(
TYPE_ERR_MSG
%
fn_rov
)
raise
TypeError
(
TYPE_ERR_MSG
%
fn_rov
)
rov
,
rov_ov
=
OpFromGraph
.
_filter_rop_var
(
rov
,
rov_ov
=
OpFromGraph
.
_filter_rop_var
(
fn_rov
(
local_inputs
,
eval_points
),
out
)
fn_rov
(
local_inputs
,
eval_points
),
out
)
all_rops_l
.
append
(
rov
)
all_rops_l
.
append
(
rov
)
all_rops_ov_l
.
append
(
rov_ov
)
all_rops_ov_l
.
append
(
rov_ov
)
else
:
else
:
if
not
hasattr
(
rop_op
,
'__call__'
):
if
not
callable
(
rop_op
):
raise
TypeError
(
TYPE_ERR_MSG
%
rop_op
)
raise
TypeError
(
TYPE_ERR_MSG
%
rop_op
)
roverrides_l
=
rop_op
(
local_inputs
,
eval_points
)
roverrides_l
=
rop_op
(
local_inputs
,
eval_points
)
if
not
isinstance
(
roverrides_l
,
list
):
if
not
isinstance
(
roverrides_l
,
list
):
...
@@ -482,13 +550,13 @@ class OpFromGraph(gof.Op):
...
@@ -482,13 +550,13 @@ class OpFromGraph(gof.Op):
self
.
_rop_op_stypes_l
=
all_rops_ov_l
self
.
_rop_op_stypes_l
=
all_rops_ov_l
self
.
_rop_op_is_cached
=
True
self
.
_rop_op_is_cached
=
True
def
get_
grad
_op
(
self
):
def
get_
lop
_op
(
self
):
"""
"""
getter method for self._
grad
_op
getter method for self._
lop
_op
"""
"""
if
not
self
.
_
grad
_op_is_cached
:
if
not
self
.
_
lop
_op_is_cached
:
self
.
_recompute_
grad
_op
()
self
.
_recompute_
lop
_op
()
return
self
.
_
grad
_op
return
self
.
_
lop
_op
def
get_rop_op
(
self
):
def
get_rop_op
(
self
):
"""
"""
...
@@ -501,11 +569,24 @@ class OpFromGraph(gof.Op):
...
@@ -501,11 +569,24 @@ class OpFromGraph(gof.Op):
def
set_grad_overrides
(
self
,
grad_overrides
):
def
set_grad_overrides
(
self
,
grad_overrides
):
"""
"""
Set gradient overrides, see help(theano.OpFromGraph) for syntax
Set gradient overrides, see help(theano.OpFromGraph) for syntax
This will completely remove any previously set gradient overrides
This will completely remove any previously set L_op/gradient overrides
"""
self
.
_lop_op
=
grad_overrides
self
.
_lop_op_is_cached
=
False
self
.
_lop_type
=
'grad'
self
.
_lop_is_default
=
(
grad_overrides
==
'default'
)
def
set_lop_overrides
(
self
,
lop_overrides
):
"""
Set L_op overrides, see help(theano.OpFromGraph) for syntax
This will completely remove any previously set L_op/gradient overrides
"""
"""
self
.
_grad_op
=
grad_overrides
self
.
_lop_op
=
lop_overrides
self
.
_grad_op_is_cached
=
False
self
.
_lop_op_is_cached
=
False
self
.
_lop_type
=
'lop'
self
.
_lop_is_default
=
(
lop_overrides
==
'default'
)
def
set_rop_overrides
(
self
,
rop_overrides
):
def
set_rop_overrides
(
self
,
rop_overrides
):
"""
"""
...
@@ -515,15 +596,17 @@ class OpFromGraph(gof.Op):
...
@@ -515,15 +596,17 @@ class OpFromGraph(gof.Op):
"""
"""
self
.
_rop_op
=
rop_overrides
self
.
_rop_op
=
rop_overrides
self
.
_rop_op_is_cached
=
False
self
.
_rop_op_is_cached
=
False
self
.
_rop_is_default
=
(
rop_overrides
==
'default'
)
def
grad
(
self
,
inputs
,
output_grads
):
if
not
self
.
_grad_op_is_cached
:
def
L_op
(
self
,
inputs
,
outputs
,
output_grads
):
self
.
_recompute_grad_op
()
if
not
self
.
_lop_op_is_cached
:
ret_ofg_l
=
self
.
_grad_op
(
self
.
_recompute_lop_op
()
*
(
list
(
inputs
)
+
list
(
output_grads
)),
return_list
=
True
)
inps
=
list
(
inputs
)
+
list
(
outputs
)
+
list
(
output_grads
)
ret_ofg_l
=
self
.
_lop_op
(
*
inps
,
return_list
=
True
)
ret_l
=
[
ret_l
=
[
ret_ofg
if
ov
is
None
else
ov
for
ret_ofg
,
ov
in
izip
(
ret_ofg
if
ov
is
None
else
ov
for
ret_ofg
,
ov
in
izip
(
ret_ofg_l
,
self
.
_
grad
_op_stypes_l
)]
ret_ofg_l
,
self
.
_
lop
_op_stypes_l
)]
return
ret_l
return
ret_l
def
R_op
(
self
,
inputs
,
eval_points
):
def
R_op
(
self
,
inputs
,
eval_points
):
...
@@ -559,14 +642,14 @@ class OpFromGraph(gof.Op):
...
@@ -559,14 +642,14 @@ class OpFromGraph(gof.Op):
cpmat_self
=
io_connection_pattern
(
cpmat_self
=
io_connection_pattern
(
self
.
local_inputs
,
self
.
local_outputs
)
self
.
local_inputs
,
self
.
local_outputs
)
grad_op
=
self
.
get_grad
_op
()
lop_op
=
self
.
get_lop
_op
()
cpmat_grad
=
io_connection_pattern
(
cpmat_grad
=
io_connection_pattern
(
grad
_op
.
local_inputs
[
inp_len
:],
lop
_op
.
local_inputs
[
inp_len
:],
grad
_op
.
local_outputs
)
lop
_op
.
local_outputs
)
# cpmat_self |= cpmat_grad.T
# cpmat_self |= cpmat_grad.T
# cpmat_self &= out_is_disconnected
# cpmat_self &= out_is_disconnected
for
i
,
t
in
enumerate
(
self
.
_
grad
_op_stypes_l
):
for
i
,
t
in
enumerate
(
self
.
_
lop
_op_stypes_l
):
if
t
is
not
None
:
if
t
is
not
None
:
if
isinstance
(
t
.
type
,
DisconnectedType
):
if
isinstance
(
t
.
type
,
DisconnectedType
):
for
o
in
range
(
out_len
):
for
o
in
range
(
out_len
):
...
...
theano/compile/tests/test_builders.py
浏览文件 @
b9854ee7
...
@@ -163,7 +163,8 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
...
@@ -163,7 +163,8 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
w
,
b
=
T
.
vectors
(
'wb'
)
w
,
b
=
T
.
vectors
(
'wb'
)
# we make the 3rd gradient default (no override)
# we make the 3rd gradient default (no override)
op_linear
=
cls_ofg
([
x
,
w
,
b
],
[
x
*
w
+
b
],
grad_overrides
=
[
go1
,
go2
,
'default'
])
op_linear
=
cls_ofg
(
[
x
,
w
,
b
],
[
x
*
w
+
b
],
grad_overrides
=
[
go1
,
go2
,
'default'
])
xx
,
ww
,
bb
=
T
.
vector
(
'xx'
),
T
.
vector
(
'yy'
),
T
.
vector
(
'bb'
)
xx
,
ww
,
bb
=
T
.
vector
(
'xx'
),
T
.
vector
(
'yy'
),
T
.
vector
(
'bb'
)
zz
=
T
.
sum
(
op_linear
(
xx
,
ww
,
bb
))
zz
=
T
.
sum
(
op_linear
(
xx
,
ww
,
bb
))
dx
,
dw
,
db
=
T
.
grad
(
zz
,
[
xx
,
ww
,
bb
])
dx
,
dw
,
db
=
T
.
grad
(
zz
,
[
xx
,
ww
,
bb
])
...
@@ -191,6 +192,33 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
...
@@ -191,6 +192,33 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
assert
isinstance
(
dw2
.
type
,
NullType
)
assert
isinstance
(
dw2
.
type
,
NullType
)
assert
isinstance
(
db2
.
type
,
DisconnectedType
)
assert
isinstance
(
db2
.
type
,
DisconnectedType
)
@test_params
def
test_lop_override
(
self
,
cls_ofg
):
x
=
T
.
vector
()
y
=
1.
/
(
1.
+
T
.
exp
(
-
x
))
def
lop_ov
(
inps
,
outs
,
grads
):
y_
,
=
outs
dedy_
,
=
grads
return
[
2.
*
y_
*
(
1.
-
y_
)
*
dedy_
]
y_
,
dedy
=
T
.
vector
(),
T
.
vector
()
op_lop_ov
=
cls_ofg
([
x
,
y_
,
dedy
],
[
2.
*
y_
*
(
1.
-
y_
)
*
dedy
])
xx
=
T
.
vector
()
yy1
=
T
.
sum
(
T
.
nnet
.
sigmoid
(
xx
))
gyy1
=
2.
*
T
.
grad
(
yy1
,
xx
)
for
ov
in
[
lop_ov
,
op_lop_ov
]:
op
=
cls_ofg
([
x
],
[
y
],
lop_overrides
=
ov
)
yy2
=
T
.
sum
(
op
(
xx
))
gyy2
=
T
.
grad
(
yy2
,
xx
)
fn
=
function
([
xx
],
[
gyy1
,
gyy2
])
xval
=
np
.
random
.
rand
(
32
)
.
astype
(
config
.
floatX
)
y1val
,
y2val
=
fn
(
xval
)
assert
np
.
allclose
(
y1val
,
y2val
)
@test_params
@test_params
def
test_rop
(
self
,
cls_ofg
):
def
test_rop
(
self
,
cls_ofg
):
a
=
T
.
vector
()
a
=
T
.
vector
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论