提交 d0821805 authored 作者: James Bergstra's avatar James Bergstra

replaced sparse dot with * operator

上级 b2b4fed8
import numpy import sys
import numpy, scipy
import scipy.sparse import scipy.sparse
from theano import gof from theano import gof
...@@ -34,9 +35,8 @@ class BROKEN_ON_PURPOSE_StructuredDotCSC(gof.Op): ...@@ -34,9 +35,8 @@ class BROKEN_ON_PURPOSE_StructuredDotCSC(gof.Op):
(a_nrows, b.shape[0]), (a_nrows, b.shape[0]),
copy = False) copy = False)
# TODO: todense() is automatic in 0.7.0, just remove the following line: # TODO: todense() is automatic in 0.7.0, just remove the following line:
# out[0] = numpy.asarray(a.dot(b).todense()) z = a * b
out[0] = a.dot(b)+0.5 if self.py_offset else a.dot(b) #ERROR TO ADD THIS CRAPPY OFFSET out[0] = z+0.5 if self.py_offset else z #ERROR TO ADD THIS CRAPPY OFFSET
#assert _is_dense(out[0])
def c_code(self, node, name, (a_val, a_ind, a_ptr, a_nrows, b), (z,), sub): def c_code(self, node, name, (a_val, a_ind, a_ptr, a_nrows, b), (z,), sub):
return """ return """
......
...@@ -455,9 +455,15 @@ class T_picklefunction(unittest.TestCase): ...@@ -455,9 +455,15 @@ class T_picklefunction(unittest.TestCase):
g = cPickle.loads(str_f) g = cPickle.loads(str_f)
#print g.maker.mode #print g.maker.mode
#print compile.mode.default_mode #print compile.mode.default_mode
except NotImplementedError, e:
if e[0].startswith('DebugMode is not pickl'):
g = 'ok'
finally: finally:
compile.mode.default_mode = old_default_mode compile.mode.default_mode = old_default_mode
if g == 'ok':
return
assert f.maker is not g.maker assert f.maker is not g.maker
assert f.maker.env is not g.maker.env assert f.maker.env is not g.maker.env
tf = f.maker.env.toposort() tf = f.maker.env.toposort()
......
...@@ -676,7 +676,8 @@ class StructuredDot(gof.Op): ...@@ -676,7 +676,8 @@ class StructuredDot(gof.Op):
if a.shape[1] != b.shape[0]: if a.shape[1] != b.shape[0]:
raise ValueError('shape mismatch in StructuredDot.perform', (a.shape, b.shape)) raise ValueError('shape mismatch in StructuredDot.perform', (a.shape, b.shape))
variable = a.dot(b) #variable = a.dot(b) # deprecated
variable = a * b
assert _is_dense(variable) # scipy 0.7 automatically converts to dense assert _is_dense(variable) # scipy 0.7 automatically converts to dense
# dot of an NxM sparse matrix, with a Mx1 dense matrix, returns vector not matrix # dot of an NxM sparse matrix, with a Mx1 dense matrix, returns vector not matrix
...@@ -738,7 +739,8 @@ class StructuredDotCSC(gof.Op): ...@@ -738,7 +739,8 @@ class StructuredDotCSC(gof.Op):
a = sparse.csc_matrix((a_val, a_ind, a_ptr), a = sparse.csc_matrix((a_val, a_ind, a_ptr),
(a_nrows, b.shape[0]), (a_nrows, b.shape[0]),
copy = False) copy = False)
out[0] = a.dot(b) #out[0] = a.dot(b)
out[0] = a * b
assert _is_dense(out[0]) # scipy 0.7 automatically converts to dense assert _is_dense(out[0]) # scipy 0.7 automatically converts to dense
def c_code(self, node, name, (a_val, a_ind, a_ptr, a_nrows, b), (z,), sub): def c_code(self, node, name, (a_val, a_ind, a_ptr, a_nrows, b), (z,), sub):
...@@ -881,7 +883,8 @@ class StructuredDotCSR(gof.Op): ...@@ -881,7 +883,8 @@ class StructuredDotCSR(gof.Op):
a = sparse.csr_matrix((a_val, a_ind, a_ptr), a = sparse.csr_matrix((a_val, a_ind, a_ptr),
(len(a_ptr)-1, b.shape[0]), (len(a_ptr)-1, b.shape[0]),
copy = True) #use view_map before setting this to False copy = True) #use view_map before setting this to False
out[0] = a.dot(b) #out[0] = a.dot(b)
out[0] = a * b
assert _is_dense(out[0]) # scipy 0.7 automatically converts to dense, but not .6 sometimes assert _is_dense(out[0]) # scipy 0.7 automatically converts to dense, but not .6 sometimes
def c_code(self, node, name, (a_val, a_ind, a_ptr, b), (z,), sub): def c_code(self, node, name, (a_val, a_ind, a_ptr, b), (z,), sub):
......
...@@ -185,11 +185,11 @@ class test_structureddot(unittest.TestCase): ...@@ -185,11 +185,11 @@ class test_structureddot(unittest.TestCase):
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1])
outvals = f(kernvals,imvals) outvals = f(kernvals,imvals)
# compare to scipy # compare to scipy
c = spmat.dot(imvals.T) c = spmat * (imvals.T)
assert _is_dense(c) assert _is_dense(c)
assert numpy.all(outvals == c) assert numpy.all(outvals == c)
tensor.verify_grad(None, buildgraphCSC, [kernvals,imvals]) tensor.verify_grad(buildgraphCSC, [kernvals,imvals])
## ##
# Test compressed-sparse row matrices ### # Test compressed-sparse row matrices ###
...@@ -207,7 +207,7 @@ class test_structureddot(unittest.TestCase): ...@@ -207,7 +207,7 @@ class test_structureddot(unittest.TestCase):
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1])
outvals = f(kernvals,imvals) outvals = f(kernvals,imvals)
# compare to scipy # compare to scipy
c = spmat.dot(imvals.T) c = spmat * (imvals.T)
assert _is_dense(c) assert _is_dense(c)
assert numpy.all(outvals == c) assert numpy.all(outvals == c)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论