Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b8bba33c
提交
b8bba33c
authored
7月 10, 2015
作者:
ChienliMa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move scanOP.inner_connection_pattern to gof.graph and reuse it in OpFromGraph
上级
97ab0274
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
73 行增加
和
118 行删除
+73
-118
builders.py
theano/compile/builders.py
+3
-51
graph.py
theano/gof/graph.py
+68
-0
scan_op.py
theano/scan_module/scan_op.py
+2
-67
没有找到文件。
theano/compile/builders.py
浏览文件 @
b8bba33c
...
@@ -6,6 +6,7 @@ from theano.compat import izip
...
@@ -6,6 +6,7 @@ from theano.compat import izip
from
theano.compile.function_module
import
orig_function
from
theano.compile.function_module
import
orig_function
from
theano.compile
import
SharedVariable
,
rebuild_collect_shared
from
theano.compile
import
SharedVariable
,
rebuild_collect_shared
from
theano.gof
import
ops_with_inner_function
,
FunctionGraph
from
theano.gof
import
ops_with_inner_function
,
FunctionGraph
from
theano.gof.graph
import
io_connection_pattern
class
OpFromGraph
(
gof
.
Op
):
class
OpFromGraph
(
gof
.
Op
):
...
@@ -138,58 +139,9 @@ class OpFromGraph(gof.Op):
...
@@ -138,58 +139,9 @@ class OpFromGraph(gof.Op):
def
connection_pattern
(
self
,
node
):
def
connection_pattern
(
self
,
node
):
"""
"""
Connection_pattern is hard to calculate. In the function, we calculate
Return connection pattern of subfgraph defined by inputs and outputs
the transpose of connection_pattern, where M[output_index,input_index]
indicates whether input with index i affects output with index i.
At last we return the transpose of final result
"""
"""
# or ori_inputs because user do not customize sharejvariable
return
io_connection_pattern
(
self
.
new_inputs
,
self
.
new_outputs
)
fgraph
=
FunctionGraph
(
self
.
new_inputs
,
self
.
new_outputs
)
# c for connection, stores the connection pattern of each variable
c_map
=
{}
num_of_input
=
len
(
fgraph
.
inputs
)
# Initialize input connection pattern, each input affects itself
for
index
in
xrange
(
num_of_input
):
vec
=
[
False
]
*
num_of_input
vec
[
index
]
=
True
# Make use of numpy.array to simplify codes
c_map
.
setdefault
(
fgraph
.
inputs
[
index
],
numpy
.
array
(
vec
))
# Toposort the fgraph and get connection pattern for each variable
for
node
in
fgraph
.
toposort
():
# connection pattern of node's inputs.
in_vecs
=
[]
for
var
in
node
.
inputs
:
if
not
isinstance
(
var
,
theano
.
Constant
):
in_vecs
.
append
(
c_map
[
var
])
else
:
in_vecs
.
append
(
numpy
.
array
([
False
]
*
num_of_input
))
if
not
hasattr
(
node
.
op
,
'connection_pattern'
):
# By default, nodes inputs affect all outputs
result
=
in_vecs
[
0
]
for
vec
in
in_vecs
[
1
:]:
result
|=
vec
results
=
result
*
len
(
node
.
outputs
)
else
:
# If node's output connect to node's input, and that input
# connect to fgraph.input, that output connect to fgraph.input
# Therefore we use OR operation here.
results
=
[]
out_vecs
=
numpy
.
array
(
node
.
op
.
connection_pattern
(
node
))
for
out_vec
in
out_vecs
.
T
:
result
=
[
False
]
*
num_of_input
for
in_vec
,
val
in
zip
(
in_vecs
,
out_vec
):
result
|=
(
in_vec
&
val
)
results
.
append
(
result
)
for
var
,
result
in
zip
(
node
.
outputs
,
results
):
c_map
.
setdefault
(
var
,
result
)
# Transpose final result and convert pattern into python list
pattern
=
numpy
.
array
([
c_map
[
var
]
for
var
in
fgraph
.
outputs
])
.
T
return
[
list
(
vec
)
for
vec
in
pattern
]
def
grad
(
self
,
inputs
,
output_grads
):
def
grad
(
self
,
inputs
,
output_grads
):
# OpFromGraph doesn't implement a connection_pattern, so for
# OpFromGraph doesn't implement a connection_pattern, so for
...
...
theano/gof/graph.py
浏览文件 @
b8bba33c
...
@@ -862,6 +862,74 @@ default_node_formatter = lambda op, argstrings: "%s(%s)" % (op.op,
...
@@ -862,6 +862,74 @@ default_node_formatter = lambda op, argstrings: "%s(%s)" % (op.op,
", "
.
join
(
argstrings
))
", "
.
join
(
argstrings
))
def
io_connection_pattern
(
inputs
,
outputs
):
"""
Returns the connection pattern of a subgraph defined by given
inputs and outputs
"""
inner_nodes
=
io_toposort
(
inputs
,
outputs
)
# Initialize 'connect_pattern_by_var' by establishing each input as
# connected only to itself
connect_pattern_by_var
=
{}
nb_inputs
=
len
(
inputs
)
nb_outputs
=
len
(
outputs
)
for
i
in
xrange
(
nb_inputs
):
input
=
inputs
[
i
]
inp_connection_pattern
=
[
i
==
j
for
j
in
xrange
(
nb_inputs
)]
connect_pattern_by_var
[
input
]
=
inp_connection_pattern
# Iterate through the nodes used to produce the outputs from the
# inputs and, for every node, infer their connection pattern to
# every input from the connection patterns of their parents.
for
n
in
inner_nodes
:
# Get the connection pattern of the inner node's op. If the op
# does not define a connection_pattern method, assume that
# every node output is connected to every node input
try
:
op_connection_pattern
=
n
.
op
.
connection_pattern
(
n
)
except
AttributeError
:
op_connection_pattern
=
([[
True
]
*
len
(
n
.
outputs
)]
*
len
(
n
.
inputs
))
# For every output of the inner node, figure out which inputs it
# is connected to by combining the connection pattern of the inner
# node and the connection patterns of the inner node's inputs.
for
out_idx
in
xrange
(
len
(
n
.
outputs
)):
out
=
n
.
outputs
[
out_idx
]
out_connection_pattern
=
[
False
]
*
nb_inputs
for
inp_idx
in
xrange
(
len
(
n
.
inputs
)):
inp
=
n
.
inputs
[
inp_idx
]
if
inp
in
connect_pattern_by_var
:
inp_connection_pattern
=
connect_pattern_by_var
[
inp
]
# If the node output is connected to the node input, it
# means it is connected to every inner input that the
# node inputs is connected to
if
op_connection_pattern
[
inp_idx
][
out_idx
]:
out_connection_pattern
=
[
out_connection_pattern
[
i
]
or
inp_connection_pattern
[
i
]
for
i
in
xrange
(
nb_inputs
)]
# Store the connection pattern of the node output
connect_pattern_by_var
[
out
]
=
out_connection_pattern
# Obtain the global connection pattern by combining the
# connnection patterns of the individual outputs
global_connection_pattern
=
[[]
for
o
in
xrange
(
len
(
inputs
))]
for
out
in
outputs
:
out_connection_pattern
=
connect_pattern_by_var
[
out
]
for
i
in
xrange
(
len
(
inputs
)):
global_connection_pattern
[
i
]
.
append
(
out_connection_pattern
[
i
])
return
global_connection_pattern
def
is_same_graph
(
var1
,
var2
,
givens
=
None
,
debug
=
False
):
def
is_same_graph
(
var1
,
var2
,
givens
=
None
,
debug
=
False
):
"""
"""
Return True iff Variables `var1` and `var2` perform the same computation.
Return True iff Variables `var1` and `var2` perform the same computation.
...
...
theano/scan_module/scan_op.py
浏览文件 @
b8bba33c
...
@@ -68,7 +68,7 @@ from theano.compat import exc_message
...
@@ -68,7 +68,7 @@ from theano.compat import exc_message
from
theano.compile
import
function
,
Param
,
Out
from
theano.compile
import
function
,
Param
,
Out
from
theano
import
compile
,
config
,
gradient
,
gof
,
tensor
from
theano
import
compile
,
config
,
gradient
,
gof
,
tensor
from
theano.gof
import
PureOp
,
Apply
from
theano.gof
import
PureOp
,
Apply
from
theano.gof.graph
import
io_
toposort
from
theano.gof.graph
import
io_
connection_pattern
from
theano.compat
import
OrderedDict
,
izip
from
theano.compat
import
OrderedDict
,
izip
from
theano.tensor
import
TensorType
from
theano.tensor
import
TensorType
from
theano.tensor.opt
import
Shape_i
from
theano.tensor.opt
import
Shape_i
...
@@ -1471,71 +1471,6 @@ class Scan(PureOp):
...
@@ -1471,71 +1471,6 @@ class Scan(PureOp):
scan_outs
.
append
((
Shape_i
(
0
)(
o
),)
+
x
[
1
:])
scan_outs
.
append
((
Shape_i
(
0
)(
o
),)
+
x
[
1
:])
return
scan_outs
return
scan_outs
def
inner_connection_pattern
(
self
):
""" Returns the connection pattern of scan's inner function
"""
inner_nodes
=
io_toposort
(
self
.
inputs
,
self
.
outputs
)
# Initialize 'connect_pattern_by_var' by establishing each input as
# connected only to itself
connect_pattern_by_var
=
{}
nb_inputs
=
len
(
self
.
inputs
)
nb_outputs
=
len
(
self
.
outputs
)
for
i
in
xrange
(
nb_inputs
):
input
=
self
.
inputs
[
i
]
inp_connection_pattern
=
[
i
==
j
for
j
in
xrange
(
nb_inputs
)]
connect_pattern_by_var
[
input
]
=
inp_connection_pattern
# Iterate through the nodes used to produce the outputs from the
# inputs and, for every node, infer their connection pattern to
# every input from the connection patterns of their parents.
for
n
in
inner_nodes
:
# Get the connection pattern of the inner node's op. If the op
# does not define a connection_pattern method, assume that
# every node output is connected to every node input
try
:
op_connection_pattern
=
n
.
op
.
connection_pattern
(
n
)
except
AttributeError
:
op_connection_pattern
=
([[
True
]
*
len
(
n
.
outputs
)]
*
len
(
n
.
inputs
))
# For every output of the inner node, figure out which inputs it
# is connected to by combining the connection pattern of the inner
# node and the connection patterns of the inner node's inputs.
for
out_idx
in
xrange
(
len
(
n
.
outputs
)):
out
=
n
.
outputs
[
out_idx
]
out_connection_pattern
=
[
False
]
*
nb_inputs
for
inp_idx
in
xrange
(
len
(
n
.
inputs
)):
inp
=
n
.
inputs
[
inp_idx
]
if
inp
in
connect_pattern_by_var
:
inp_connection_pattern
=
connect_pattern_by_var
[
inp
]
# If the node output is connected to the node input, it
# means it is connected to every inner input that the
# node inputs is connected to
if
op_connection_pattern
[
inp_idx
][
out_idx
]:
out_connection_pattern
=
[
out_connection_pattern
[
i
]
or
inp_connection_pattern
[
i
]
for
i
in
xrange
(
nb_inputs
)]
# Store the connection pattern of the node output
connect_pattern_by_var
[
out
]
=
out_connection_pattern
# Obtain the global connection pattern by combining the
# connnection patterns of the individual outputs
global_connection_pattern
=
[[]
for
o
in
xrange
(
len
(
self
.
inputs
))]
for
out
in
self
.
outputs
:
out_connection_pattern
=
connect_pattern_by_var
[
out
]
for
i
in
xrange
(
len
(
self
.
inputs
)):
global_connection_pattern
[
i
]
.
append
(
out_connection_pattern
[
i
])
return
global_connection_pattern
def
connection_pattern
(
self
,
node
):
def
connection_pattern
(
self
,
node
):
# We cache the result of this function because, with a previous
# We cache the result of this function because, with a previous
...
@@ -1546,7 +1481,7 @@ class Scan(PureOp):
...
@@ -1546,7 +1481,7 @@ class Scan(PureOp):
return
node
.
tag
.
connection_pattern
return
node
.
tag
.
connection_pattern
# Obtain the connection pattern of the inner function.
# Obtain the connection pattern of the inner function.
inner_connect_pattern
=
self
.
inner_connection_pattern
(
)
inner_connect_pattern
=
io_connection_pattern
(
self
.
inputs
,
self
.
outputs
)
# Initially assume no outer input is connected to any outer output
# Initially assume no outer input is connected to any outer output
connection_pattern
=
[[
False
for
output
in
node
.
outputs
]
connection_pattern
=
[[
False
for
output
in
node
.
outputs
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论