Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
04c38218
提交
04c38218
authored
1月 23, 2013
作者:
Vivek Kulkarni
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merging from LatestTheano
上级
0ae2dc70
全部展开
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
6 行增加
和
49 行删除
+6
-49
basic.py
theano/sparse/basic.py
+6
-49
basic.py
theano/tensor/basic.py
+0
-0
没有找到文件。
theano/sparse/basic.py
浏览文件 @
04c38218
...
...
@@ -8,11 +8,9 @@ http://www-users.cs.umn.edu/~saad/software/SPARSKIT/paper.ps
# Automatic methods for determining best sparse format?
import
sys
from
itertools
import
izip
import
numpy
import
theano
import
scipy.sparse
from
theano
import
gof
,
tensor
,
compile
,
scalar
,
config
from
theano.gof.python25
import
all
from
theano.gradient
import
DisconnectedType
...
...
@@ -21,13 +19,9 @@ import theano.tests.unittest_tools as utt
from
theano.gradient
import
grad_not_implemented
from
theano.sparse.type
import
SparseType
,
_is_sparse
#Column compressed (CSC)
#Row compressed (CSR)
sparse_formats
=
[
'csc'
,
'csr'
]
#Register an optimization that does a specialization
#Does the same thing but better
# TODO: move this decorator to the compile submodule
def
register_specialize
(
lopt
,
*
tags
,
**
kwargs
):
compile
.
optdb
[
'specialize'
]
.
register
((
kwargs
and
kwargs
.
pop
(
'name'
))
or
...
...
@@ -1714,49 +1708,30 @@ class AddSD(gof.op.Op):
:note: The grad implemented is structured on `x`.
"""
#Constructor of the object
def
__init__
(
self
,
inplace
=
False
,
*
args
,
**
kwargs
):
gof
.
Op
.
__init__
(
self
,
*
args
,
**
kwargs
)
#Should we do inplace addition or not ?
self
.
inplace
=
inplace
if
self
.
inplace
:
#This is a hint to the local optimizer that says that the first
#output is the same as the 3rd input and no intermdiate storage
#needs to be allocated
self
.
destroy_map
=
{
0
:
[
3
]}
def
__eq__
(
self
,
other
):
#Compare the inplace flag as well
return
(
type
(
self
)
==
type
(
other
))
and
self
.
inplace
==
other
.
inplace
def
__hash__
(
self
):
#Now use the hash of inplace as well
return
hash
(
type
(
self
))
^
hash
(
self
.
inplace
)
def
__str__
(
self
):
#If we are running the inplace version, display that
# so that it is useful for debugging
if
self
.
inplace
:
return
self
.
__class__
.
__name__
+
'{inplace}'
return
self
.
__class__
.
__name__
# Op Contract implementation: make_node:
# Should return a Apply object that specifies what:
# 1. Input variables type are etc for the operation
# 2. What are the types of the output variables
# These should be Theano variables
def
make_node
(
self
,
x
,
y
):
# x is a sparse matrix, y is a dense one
# Wrap them around theano variables as this must be symbolic
x
,
y
=
as_sparse_variable
(
x
),
tensor
.
as_tensor_variable
(
y
)
# If the types of both variables are of different types
# this is bad as theres a type mismatch
if
x
.
type
.
dtype
!=
y
.
type
.
dtype
:
raise
NotImplementedError
()
#Obtains the indices, indpt, data of NNZ sparse matrix x
indices
,
indptr
,
data
=
csm_indices
(
x
),
csm_indptr
(
x
),
csm_data
(
x
)
# We either use CSC or CSR depending on the format of input
...
...
@@ -1774,7 +1749,7 @@ class AddSD(gof.op.Op):
inplace
=
int
(
self
.
inplace
)
format
=
{
'csc'
:
0
,
'csr'
:
1
}[
self
.
format
]
code
=
"""
if(
%(z)
s) {Py_XDECREF(
%(z)
s);}
Py_XDECREF(
%(z)
s);
if (!
%(inplace)
s){
%(z)
s = (PyArrayObject *) PyArray_NewCopy(
%(y)
s, NPY_CORDER);
}else{
...
...
@@ -1815,29 +1790,12 @@ class AddSD(gof.op.Op):
def
perform
(
self
,
node
,
(
data
,
indices
,
indptr
,
y
),
(
out
,
)):
assert
_is_dense
(
y
)
if
self
.
inplace
:
#inplace enabled
if
self
.
format
==
'csc'
:
#column compressed
for
c
in
xrange
(
y
.
shape
[
1
]):
#Loop through each column
low
=
indptr
[
c
]
#indptr will pint to slice of indices array for column
high
=
indptr
[
c
+
1
]
for
ind
in
xrange
(
low
,
high
):
y
[(
indices
[
ind
],
c
)]
+=
data
[
ind
]
#Add that data element
elif
self
.
format
==
'csr'
:
#Case for row's. Symmetric to what was done for columns
for
r
in
xrange
(
y
.
shape
[
0
]):
low
=
indptr
[
r
]
high
=
indptr
[
r
+
1
]
for
ind
in
xrange
(
low
,
high
):
y
[(
r
,
indices
[
ind
])]
+=
data
[
ind
]
out
[
0
]
=
y
#Output storage cell is y
else
:
#If in place is not enabled, create back the sparse matrix and
# and just add them normally.
if
self
.
format
==
'csr'
:
x
=
scipy
.
sparse
.
csr_matrix
(
(
data
,
indices
,
indptr
),
shape
=
y
.
shape
)
x
=
scipy
.
sparse
.
csr_matrix
(
(
data
,
indices
,
indptr
),
shape
=
y
.
shape
)
elif
self
.
format
==
'csc'
:
x
=
scipy
.
sparse
.
csc_matrix
(
(
data
,
indices
,
indptr
),
shape
=
y
.
shape
)
x
=
scipy
.
sparse
.
csc_matrix
((
data
,
indices
,
indptr
),
shape
=
y
.
shape
)
# The asarray is needed as in some case, this return a
# numpy.matrixlib.defmatrix.matrix object and not an ndarray.
out
[
0
]
=
theano
.
_asarray
(
x
+
y
,
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
...
...
@@ -1848,7 +1806,6 @@ class AddSD(gof.op.Op):
return
sp_ones_like
(
x
)
*
gz
,
gz
def
infer_shape
(
self
,
node
,
shapes
):
#Shape of output is the shape of y
return
[
shapes
[
3
]]
add_s_d
=
AddSD
()
...
...
theano/tensor/basic.py
浏览文件 @
04c38218
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论