Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
23721b71
提交
23721b71
authored
2月 15, 2008
作者:
bergstrj@iro.umontreal.ca
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init sparse
上级
3bafec1a
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
252 行增加
和
23 行删除
+252
-23
compile.py
gof/compile.py
+3
-1
env.py
gof/env.py
+14
-1
err.py
gof/err.py
+3
-1
op.py
gof/op.py
+12
-16
result.py
gof/result.py
+4
-4
utils.py
gof/utils.py
+2
-0
sparse.py
sparse.py
+214
-0
没有找到文件。
gof/compile.py
浏览文件 @
23721b71
import
env
import
tools
import
utils
class
Compiler
:
""" What is this? Please document.
"""
def
__init__
(
self
,
optimizer
,
features
):
self
.
features
=
set
(
features
)
...
...
gof/env.py
浏览文件 @
23721b71
...
...
@@ -2,7 +2,6 @@
from
copy
import
copy
import
graph
## from value import Value, AsValue
from
utils
import
ClsInit
from
err
import
GofError
,
GofTypeError
,
PropagationError
from
op
import
Op
...
...
@@ -46,6 +45,7 @@ __all__ = ['InconsistencyError',
#TODO: why is this not in err.py? -James
class
InconsistencyError
(
GofError
):
"""
This exception is raised by Env whenever one of the listeners marks
...
...
@@ -71,6 +71,14 @@ class Env(graph.Graph):
time and whenever there is a replacement). In addition to that, each listener can
implement the 'consistent' and 'ordering' methods (see EnvListener) in order to
restrict how ops in the subgraph can be related.
Regarding inputs and orphans:
In the context of a computation graph, the inputs and orphans are both
results that are the source nodes of computation. Those results that are
named as inputs will be assumed to contain fresh. In other words, the
backward search from outputs will stop at any node that has been explicitly
named as an input.
"""
### Special ###
...
...
@@ -79,6 +87,11 @@ class Env(graph.Graph):
"""
Create an Env which operates on the subgraph bound by the inputs and outputs
sets. If consistency_check is False, an illegal graph will be tolerated.
Features are class types derived from things in the tools file. These
can be listeners, constraints, orderings, etc. Features add much
(most?) functionality to an Env.
"""
self
.
_features
=
{}
...
...
gof/err.py
浏览文件 @
23721b71
"""
This file defines the Exceptions that may be raised by graph manipulations.
"""
class
GofError
(
Exception
):
pass
...
...
gof/op.py
浏览文件 @
23721b71
...
...
@@ -30,17 +30,21 @@ class Op(object):
__slots__
=
[
'_inputs'
,
'_outputs'
]
__require__
=
[]
#create inputs and outputs as read-only attributes
inputs
=
property
(
lambda
self
:
self
.
_inputs
,
doc
=
"The list of this Op's input Results."
)
outputs
=
property
(
lambda
self
:
self
.
_outputs
,
doc
=
"The list of this Op's output Results."
)
"""
If true, self.default_output() or self.out can be used to access
self.outputs[0]
"""
has_default_output
=
True
out
=
property
(
lambda
self
:
self
.
default_output
(),
doc
=
"Same as self.outputs[0] if this Op's has_default_output field is True."
)
_default_output_idx
=
0
def
default_output
(
self
):
"""Returns the default output of this Op instance, typically self.outputs[0]."""
try
:
return
self
.
outputs
[
self
.
_default_output_idx
]
except
(
IndexError
,
TypeError
):
raise
AttributeError
(
"Op does not have a default output."
)
out
=
property
(
default_output
,
doc
=
"Same as self.outputs[0] if this Op's has_default_output field is True."
)
def
__init__
(
self
,
inputs
,
outputs
,
use_self_setters
=
False
):
"""
...
...
@@ -72,14 +76,6 @@ class Op(object):
self
.
validate
()
def
default_output
(
self
):
"""
Returns the default output of this Op instance, typically self.outputs[0].
"""
if
self
.
has_default_output
:
return
self
.
outputs
[
0
]
else
:
raise
AttributeError
(
"Op does not have a default output."
)
def
set_input
(
self
,
i
,
input
,
allow_changes
=
False
,
validate
=
True
):
...
...
@@ -153,7 +149,7 @@ class Op(object):
self
.
set_output
(
i
,
previous
,
False
)
def
repair
(
self
,
allow_changes
=
False
):
def
_dontuse_
repair
(
self
,
allow_changes
=
False
):
"""
This function attempts to repair all inputs that are broken
links by calling set_input on the new Result that replaced
...
...
gof/result.py
浏览文件 @
23721b71
...
...
@@ -2,6 +2,7 @@
"""
Contains the Result class, which is the base interface for a
value that is the input or the output of an Op.
"""
...
...
@@ -43,10 +44,9 @@ class Result(object):
The Result class represents a datum for use in a graph of Ops. It
has two slots:
- owner: represents the Op which computes this Result. It is
assumed to be an instance of Op. If owner raises an
AttributeError, the Result is assumed to be an input.
- index: the index this Result holds in its owner's outputs.
- owner: represents the Op which computes this Result. Contains either None
or an instance of Op.
- index: the index of this Result in owner.outputs.
Result has no __init__ or __new__ routine. It is the Op's
responsibility to set the owner field of its results.
...
...
gof/utils.py
浏览文件 @
23721b71
...
...
@@ -30,6 +30,8 @@ def all_bases_collect(cls, raw_name):
def
uniq_features
(
_features
,
*
_rest
):
"""Return a list such that no element is a subclass of another"""
# used in Env.__init__ to
features
=
[
x
for
x
in
_features
]
for
other
in
_rest
:
features
+=
[
x
for
x
in
other
]
...
...
sparse.py
0 → 100644
浏览文件 @
23721b71
import
unittest
import
numpy
from
scipy
import
sparse
import
gof.lib
import
core
import
grad
# Wrapper type
class
SparseR
(
gof
.
PythonR
):
"""
Attribute:
format - a subclass of sparse.spmatrix indicating self.data.__class__
"""
def
__init__
(
self
,
x
=
core
.
UNCOMPUTED
,
constant
=
False
,
format
=
sparse
.
csr_matrix
):
gof
.
PythonR
.
__init__
(
self
,
x
,
constant
)
self
.
format
=
isinstance
(
x
,
sparse
.
spmatrix
)
and
x
.
__class__
or
format
def
set_value
(
self
,
value
):
"""Extend base impl, assert value is sparse matrix"""
gof
.
PythonR
.
set_value
(
self
,
value
)
if
self
.
data
is
not
core
.
UNCOMPUTED
:
if
not
isinstance
(
self
.
data
,
sparse
.
spmatrix
):
print
self
.
data
.
__class__
print
self
.
owner
.
__class__
raise
TypeError
((
'hrm'
,
value
))
def
__add__
(
left
,
right
):
return
add
(
left
,
right
)
def
__radd__
(
right
,
left
):
return
add
(
left
,
right
)
T
=
property
(
lambda
self
:
transpose
(
self
),
doc
=
"Return aliased transpose"
)
# convenience base class
class
op
(
gof
.
PythonOp
,
grad
.
update_gradient_via_grad
):
pass
#
# Conversion
#
# convert a sparse matrix to an ndarray
class
sparse2dense
(
op
):
def
gen_outputs
(
self
):
return
[
core
.
NumpyR
()]
def
impl
(
x
):
return
numpy
.
asarray
(
x
.
todense
())
def
grad
(
self
,
x
,
gz
):
if
x
.
format
is
sparse
.
coo_matrix
:
return
dense2coo
(
gz
)
if
x
.
format
is
sparse
.
csc_matrix
:
return
dense2csc
(
gz
)
if
x
.
format
is
sparse
.
csr_matrix
:
return
dense2csr
(
gz
)
if
x
.
format
is
sparse
.
dok_matrix
:
return
dense2dok
(
gz
)
if
x
.
format
is
sparse
.
lil_matrix
:
return
dense2lil
(
gz
)
# convert an ndarray to various sorts of sparse matrices.
class
_dense2sparse
(
op
):
def
gen_outputs
(
self
):
return
[
SparseR
()]
def
grad
(
self
,
x
,
gz
):
return
sparse2dense
(
gz
)
class
dense2coo
(
_dense2sparse
):
def
impl
(
x
):
return
sparse
.
coo_matrix
(
x
)
class
dense2csc
(
_dense2sparse
):
def
impl
(
x
):
return
sparse
.
csc_matrix
(
x
)
class
dense2csr
(
_dense2sparse
):
def
impl
(
x
):
return
sparse
.
csr_matrix
(
x
)
class
dense2dok
(
_dense2sparse
):
def
impl
(
x
):
return
sparse
.
dok_matrix
(
x
)
class
dense2lil
(
_dense2sparse
):
def
impl
(
x
):
return
sparse
.
lil_matrix
(
x
)
# Linear Algebra
class
add
(
op
):
def
gen_outputs
(
self
):
return
[
SparseR
()]
def
impl
(
csr
,
y
):
return
csr
+
y
class
transpose
(
op
):
def
gen_outputs
(
self
):
return
[
SparseR
()]
def
impl
(
x
):
return
x
.
transpose
()
def
grad
(
self
,
x
,
gz
):
return
transpose
(
gz
)
class
_testCase_transpose
(
unittest
.
TestCase
):
def
setUp
(
self
):
core
.
build_eval_mode
()
numpy
.
random
.
seed
(
44
)
def
tearDown
(
self
):
core
.
pop_mode
()
def
test_transpose
(
self
):
a
=
SparseR
(
sparse
.
csr_matrix
(
sparse
.
speye
(
5
,
3
)))
self
.
failUnless
(
a
.
data
.
shape
==
(
5
,
3
))
ta
=
transpose
(
a
)
self
.
failUnless
(
ta
.
data
.
shape
==
(
3
,
5
))
class
dot
(
op
):
"""
Attributes:
grad_preserves_dense - an array of boolean flags (described below)
grad_preserves_dense controls whether gradients with respect to inputs are
converted to dense matrices when the corresponding inputs are not in a
SparseR wrapper. This can be a good idea when dot is in the middle of a
larger graph, because the types of gx and gy will match those of x and y.
This conversion might be annoying if the gradients are graph outputs though,
hence this mask.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
op
.
__init__
(
self
,
*
args
,
**
kwargs
)
self
.
grad_preserves_dense
=
[
True
,
True
]
def
gen_outputs
(
self
):
return
[
SparseR
()]
def
impl
(
x
,
y
):
if
hasattr
(
x
,
'getnnz'
):
return
x
.
dot
(
y
)
else
:
return
y
.
transpose
()
.
dot
(
x
.
transpose
())
.
transpose
()
def
grad
(
self
,
x
,
y
,
gz
):
rval
=
[
dot
(
gz
,
y
.
T
),
dot
(
x
.
T
,
gz
)]
for
i
in
0
,
1
:
if
not
isinstance
(
self
.
inputs
[
i
],
SparseR
):
#assume it is a dense matrix
if
self
.
grad_preserves_dense
[
i
]:
rval
[
i
]
=
sparse2dense
(
rval
[
i
])
return
rval
class
_testCase_dot
(
unittest
.
TestCase
):
def
setUp
(
self
):
core
.
build_eval_mode
()
numpy
.
random
.
seed
(
44
)
def
tearDown
(
self
):
core
.
pop_mode
()
def
test_basic0
(
self
):
for
mtype
in
[
sparse
.
csc_matrix
,
sparse
.
csr_matrix
]:
x
=
SparseR
(
mtype
(
sparse
.
speye
(
5
,
3
)))
y
=
core
.
NumpyR
(
numpy
.
random
.
rand
(
3
,
2
))
z
=
dot
(
x
,
y
)
self
.
failUnless
(
z
.
data
.
shape
==
(
5
,
2
))
self
.
failUnless
(
type
(
z
.
data
)
is
mtype
)
def
test_basic1
(
self
):
"""dot: sparse left"""
a
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)
b
=
numpy
.
random
.
rand
(
5
,
3
)
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
#print type(a), mtype
m
=
mtype
(
a
)
ab
=
m
.
dot
(
b
)
try
:
z
=
dot
(
SparseR
(
m
),
gof
.
lib
.
PythonR
(
b
))
self
.
failUnless
(
z
.
data
.
shape
==
ab
.
shape
)
self
.
failUnless
(
type
(
z
.
data
)
==
type
(
ab
))
except
Exception
,
e
:
print
mtype
,
e
,
str
(
e
)
raise
def
test_basic2
(
self
):
"""dot: sparse right"""
a
=
numpy
.
random
.
rand
(
2
,
5
)
b
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)
.
transpose
()
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
m
=
mtype
(
b
)
ab
=
m
.
transpose
()
.
dot
(
a
.
transpose
())
.
transpose
()
z
=
dot
(
gof
.
lib
.
PythonR
(
a
),
SparseR
(
mtype
(
b
)))
self
.
failUnless
(
z
.
data
.
shape
==
ab
.
shape
)
self
.
failUnless
(
type
(
z
.
data
)
==
type
(
ab
))
def
test_graph_bprop0
(
self
):
x
=
core
.
NumpyR
(
numpy
.
random
.
rand
(
10
,
2
))
w
=
SparseR
(
sparse
.
csr_matrix
(
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)))
for
epoch
in
xrange
(
50
):
xw
=
sparse2dense
(
dot
(
x
,
w
))
y
=
sparse2dense
(
dot
(
xw
,
transpose
(
w
)))
loss
=
core
.
sum
(
core
.
sqr
(
x
-
y
))
gy
=
y
-
x
g
=
grad
.
Grad
({
y
:
gy
})
g
.
bprop
()
lr
=
0.002
g
(
w
)
.
data
[
1
,
0
]
=
0
g
(
w
)
.
data
[
1
,
4
]
=
0
w
.
data
=
-
lr
*
g
(
w
)
.
data
+
w
.
data
self
.
failUnless
(
'3.08560636025'
==
str
(
loss
))
def
test_graph_bprop1
(
self
):
x
=
core
.
NumpyR
(
numpy
.
random
.
rand
(
10
,
2
))
w
=
SparseR
(
sparse
.
csr_matrix
(
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)))
for
epoch
in
xrange
(
50
):
xw
=
sparse2dense
(
dot
(
x
,
w
))
y
=
sparse2dense
(
dot
(
xw
,
transpose
(
w
)))
loss
=
core
.
sum
(
core
.
sqr
(
x
-
y
))
g
=
grad
.
grad
(
loss
)
lr
=
0.001
g
(
w
)
.
data
[
1
,
0
]
=
0
g
(
w
)
.
data
[
1
,
4
]
=
0
w
.
data
=
-
lr
*
g
(
w
)
.
data
+
w
.
data
self
.
failUnless
(
'3.08560636025'
==
str
(
loss
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论