提交 f5801429 authored 作者: james@mackie's avatar james@mackie

bringing back sparse

上级 a7e53331
from sparse import *
import unittest
import compile
class _testCase_transpose(unittest.TestCase):
class T_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)))
def test_transpose_csc(self):
sp = sparse.csc_matrix(sparse.speye(5,3))
a = assparse(sp)
self.failUnless(a.data is sp)
self.failUnless(a.data.shape == (5,3))
self.failUnless(a.dtype == 'float64')
self.failUnless(a.format == 'csc', a.format)
ta = transpose(a)
self.failUnless(ta.data.shape == (3,5))
self.failUnless(ta.dtype == 'float64', ta.dtype)
self.failUnless(ta.format == 'csr', ta.format)
vta = compile.eval_outputs([ta])
self.failUnless(vta.shape == (3,5))
def test_transpose_csr(self):
a = assparse(sparse.csr_matrix(sparse.speye(5,3)))
self.failUnless(a.data.shape == (5,3))
self.failUnless(a.dtype == 'float64')
self.failUnless(a.format == 'csr')
ta = transpose(a)
self.failUnless(ta.dtype == 'float64', ta.dtype)
self.failUnless(ta.format == 'csc', ta.format)
vta = compile.eval_outputs([ta])
self.failUnless(vta.shape == (3,5))
class T_Add(unittest.TestCase):
def test0(self):
sp_a = sparse.csc_matrix(sparse.speye(5,3))
a = assparse(sp_a)
sp_b = sparse.csc_matrix(sparse.speye(5,3))
b = assparse(sp_b)
self.failUnless(a.data is sp_a)
apb = add_s_s(a, b)
self.failUnless(apb.dtype == a.dtype, apb.dtype)
self.failUnless(apb.format == a.format, apb.format)
val = compile.eval_outputs([apb])
self.failUnless(val.shape == (5,3))
self.failUnless(numpy.all(val.todense() == (sp_a + sp_b).todense()))
class T_conversion(unittest.TestCase):
def setUp(self):
numpy.random.seed(44)
def test0(self):
a = tensor.astensor(numpy.random.rand(5))
s = sparse_from_dense(a,'csc')
val = compile.eval_outputs([s])
self.failUnless(str(val.dtype)=='float64')
self.failUnless(val.format == 'csc')
def test1(self):
a = tensor.astensor(numpy.random.rand(5))
s = sparse_from_dense(a,'csr')
val = compile.eval_outputs([s])
self.failUnless(str(val.dtype)=='float64')
self.failUnless(val.format == 'csr')
def test2(self):
csr = sparse.csr_matrix((2,5))
d = dense_from_sparse(csr)
csr[0,0] = 1.0
val = compile.eval_outputs([d])
self.failUnless(str(val.dtype)=='float64')
self.failUnless(numpy.all(val[0] == [1,0,0,0,0]))
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.wrap(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),core.ResultBase(data=b))
def test(self):
"""Bring back the tests for sparse dot"""
raise NotImplementedError()
if 0:
def test_basic0(self):
for mtype in [sparse.csc_matrix, sparse.csr_matrix]:
x = assparse(mtype(sparse.speye(5,3)))
y = astensor(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),core.ResultBase(data=b))
self.failUnless(z.data.shape == ab.shape)
self.failUnless(type(z.data) == type(ab))
except Exception, e:
print 'cccc', 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(core.ResultBase(data=a),SparseR(mtype(b)))
self.failUnless(z.data.shape == ab.shape)
self.failUnless(type(z.data) == type(ab))
except Exception, e:
print 'cccc', 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(core.ResultBase(data=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.wrap(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.data))
def test_graph_bprop1(self):
x = core.wrap(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.data))
def test_graph_bprop0(self):
x = core.wrap(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.data))
def test_graph_bprop1(self):
x = core.wrap(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.data))
if __name__ == '__main__':
unittest.main()
......@@ -144,7 +144,7 @@ class Op(object):
TODO: consider moving this function to the python linker.
"""
res = self.impl(*[input.data for input in self.inputs])
if self.nout == 1:
if len(self.outputs) == 1:
self.outputs[0].data = res
else:
assert len(res) == len(self.outputs)
......
差异被折叠。
......@@ -70,6 +70,8 @@ class Tensor(BaseTensor):
# alternate Tensor constructor
def astensor(data, broadcastable=None, role=None, name=None):
"""Return a Tensor containing given data"""
if isinstance(data, Tensor) and broadcastable is None and role is None and name is None:
return data
data = numpy.asarray(data)
if broadcastable is None:
broadcastable = [s==1 for s in data.shape]
......@@ -116,11 +118,9 @@ def _assert_tensor_scalar(x, a):
if numpy.product(a.shape) != 1:
raise ValueError("The second argument must be a scalar.")
def _as_tensor(obj):
if isinstance(obj, Tensor):
return obj
else:
return astensor(obj)
# this has a different name, because _as_tensor is the function which ops use
# to upcast their arguments... this internal-use function is a good place to put debugging stuff, better than the global astensor.
_as_tensor = astensor
class _Op(BaseTensorOp):
"""A convenient base for the ops in this file"""
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论