提交 ccc52aea authored 作者: Rami Al-Rfou's avatar Rami Al-Rfou

index the sparse matrices according to their format instead of converting them…

index the sparse matrices according to their format instead of converting them to coo, add a flag for inplace operation
上级 8551830d
...@@ -1710,6 +1710,12 @@ class AddSD(gof.op.Op): ...@@ -1710,6 +1710,12 @@ class AddSD(gof.op.Op):
:note: The grad implemented is structured on `x`. :note: The grad implemented is structured on `x`.
""" """
def __init__(self, inplace=False, *args, **kwargs):
gof.Op.__init__(self, *args, **kwargs)
self.inplace = inplace
if self.inplace:
self.destroy_map = {0: [0]}
def __eq__(self, other): def __eq__(self, other):
return (type(self) == type(other)) return (type(self) == type(other))
...@@ -1735,21 +1741,31 @@ class AddSD(gof.op.Op): ...@@ -1735,21 +1741,31 @@ class AddSD(gof.op.Op):
def perform(self, node, (x, y), (out, )): def perform(self, node, (x, y), (out, )):
assert _is_sparse(x) and _is_dense(y) assert _is_sparse(x) and _is_dense(y)
# # The asarray is needed as in some case, this return a self.inplace = True
# # numpy.matrixlib.defmatrix.matrix object and not an ndarray. if self.inplace:
# out[0] = theano._asarray(x + y, dtype=node.outputs[0].type.dtype) if x.format == 'csc':
for c in xrange(x.shape[1]):
coo_x = x.tocoo(copy=False) low = x.indptr[c]
for row, col, data in izip(coo_x.row, coo_x.col, coo_x.data): high = x.indptr[c+1]
y[(row,col)] += data for ind in xrange(low, high):
out[0] = y y[(x.indices[ind], c)] += x.data[ind]
elif x.format == 'csr':
# rows, cols = x.nonzero() for r in xrange(x.shape[0]):
# for row, col in izip(*x.nonzero()): low = x.indptr[r]
# y[(row,col)] += x[(row,col)] high = x.indptr[r+1]
# out[0] = y for ind in xrange(low, high):
y[(r, x.indices[ind])] += x.data[ind]
else:
coo_x = x.tocoo(copy=False)
for row, col, data in izip(coo_x.row, coo_x.col, coo_x.data):
y[(row,col)] += data
out[0] = y
else:
# 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)
def grad(self, (x, y), (gz,)): def grad(self, (x, y), (gz,)):
assert _is_sparse_variable(x) and _is_dense_variable(y) assert _is_sparse_variable(x) and _is_dense_variable(y)
assert _is_dense_variable(gz) assert _is_dense_variable(gz)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论