提交 5c05a90c authored 作者: james@X40's avatar james@X40

sparse tests pass

上级 8e620d7b
...@@ -6,6 +6,7 @@ To read about different sparse formats, see U{http://www-users.cs.umn.edu/~saad/ ...@@ -6,6 +6,7 @@ To read about different sparse formats, see U{http://www-users.cs.umn.edu/~saad/
@todo: Automatic methods for determining best sparse format? @todo: Automatic methods for determining best sparse format?
""" """
import sys
import numpy import numpy
from scipy import sparse from scipy import sparse
...@@ -211,12 +212,13 @@ class CSMProperties(gof.Op): ...@@ -211,12 +212,13 @@ class CSMProperties(gof.Op):
[data, tensor.ivector(), tensor.ivector(), tensor.ivector()]) [data, tensor.ivector(), tensor.ivector(), tensor.ivector()])
def perform(self, node, (csm,), out): def perform(self, node, (csm,), out):
print '******* sp:CSMProperties:perform *******' if 0:
print 'self.map = ', self.map print '******* sp:CSMProperties:perform *******'
print 'csm.data = ', csm.data print 'self.map = ', self.map
print 'size(csm.data) = ', numpy.size(csm.data) print 'csm.data = ', csm.data
print 'csm.todense.shape = ', csm.todense().shape print 'size(csm.data) = ', numpy.size(csm.data)
print 'type(csm) = ', type(csm) print 'csm.todense.shape = ', csm.todense().shape
print 'type(csm) = ', type(csm)
out[0][0] = csm.data if self.map is None else csm.data[self.map] out[0][0] = csm.data if self.map is None else csm.data[self.map]
out[1][0] = numpy.asarray(csm.indices, dtype='int32') out[1][0] = numpy.asarray(csm.indices, dtype='int32')
out[2][0] = numpy.asarray(csm.indptr, dtype='int32') out[2][0] = numpy.asarray(csm.indptr, dtype='int32')
...@@ -295,12 +297,14 @@ class CSM(gof.Op): ...@@ -295,12 +297,14 @@ class CSM(gof.Op):
"""Build a csc_matrix""" """Build a csc_matrix"""
#assert len(data.flatten()) == len(indices.flatten()) #assert len(data.flatten()) == len(indices.flatten())
print '********** sp:CSM:perform ***********' if 0:
print 'data =', data.__repr__() print '********** sp:CSM:perform ***********'
print 'size(data) = ', numpy.size(data) print 'data =', data.__repr__()
print 'kmap =', self.map.__repr__() print 'size(data) = ', numpy.size(data)
print 'kmap =', self.map.__repr__()
data = data[self.map] if self.map!=None else data data = data[self.map] if self.map!=None else data
print 'data[kmap] =', data.__repr__() if 0:
print 'data[kmap] =', data.__repr__()
if len(shape) != 2: if len(shape) != 2:
raise ValueError('Shape should be an array of length 2') raise ValueError('Shape should be an array of length 2')
...@@ -502,7 +506,37 @@ class MulSD(gof.op.Op): ...@@ -502,7 +506,37 @@ class MulSD(gof.op.Op):
elif len(y.shape) == 2: elif len(y.shape) == 2:
#if we have enough memory to fit y, maybe we can fit x.asarray() too? #if we have enough memory to fit y, maybe we can fit x.asarray() too?
#TODO: change runtime from O(M*N) to O(nonzeros) #TODO: change runtime from O(M*N) to O(nonzeros)
out[0] = type(x)(x.toarray() * y) M, N = x.shape
assert x.shape == y.shape
if x.format == 'csc':
x_data = x.data
indices = x.indices
indptr = x.indptr
z = x.copy()
z_data = z.data
for j in xrange(0, N):
for i_idx in xrange(indptr[j], indptr[j+1]):
i = indices[i_idx]
z_data[i_idx] *= y[i,j]
out[0] = z
elif x.format == 'csr':
x_data = x.data
indices = x.indices
indptr = x.indptr
z = x.copy()
z_data = z.data
for i in xrange(0, M):
for j_idx in xrange(indptr[i], indptr[i+1]):
j = indices[j_idx]
z_data[j_idx] *= y[i,j]
out[0] = z
else:
print >> sys.stderr, "WARNING: crappy implementation of MulSD", x.format
out[0] = type(x)(x.toarray() * y)
def grad(self, (x, y), (gz,)): def grad(self, (x, y), (gz,)):
assert _is_sparse_result(x) and _is_dense_result(y) assert _is_sparse_result(x) and _is_dense_result(y)
assert _is_sparse_result(gz) assert _is_sparse_result(gz)
...@@ -563,8 +597,6 @@ class Dot(gof.op.Op): ...@@ -563,8 +597,6 @@ class Dot(gof.op.Op):
@todo: Verify that output is sufficiently sparse, and raise a warning if it is not @todo: Verify that output is sufficiently sparse, and raise a warning if it is not
@todo: Also determine that we are storing the output in the best storage format? @todo: Also determine that we are storing the output in the best storage format?
""" """
print 'x type is', type(x)
print 'y type is', type(y)
out[0] = x.dot(y) out[0] = x.dot(y)
def grad(self, (x, y), (gz,)): def grad(self, (x, y), (gz,)):
assert _is_sparse_result(gz) assert _is_sparse_result(gz)
......
...@@ -274,9 +274,10 @@ class test_dot(unittest.TestCase): ...@@ -274,9 +274,10 @@ class test_dot(unittest.TestCase):
for epoch in xrange(50): for epoch in xrange(50):
y, loss, gw = trainfn(x, w) y, loss, gw = trainfn(x, w)
w = w - (lr * gw) w = w - (lr * gw)
print loss
self.failUnless(origloss > loss) self.failUnless(origloss > loss)
self.failUnless('1.0543172285' == str(loss)) self.failUnless('1.05191241115' == str(loss))
def test_graph_bprop_rand(self): def test_graph_bprop_rand(self):
for i in range(10): for i in range(10):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论