sparse backprop seems to work

上级 42752db1
...@@ -9,7 +9,7 @@ from sparse import _is_dense, _is_sparse, _is_dense_result, _is_sparse_result ...@@ -9,7 +9,7 @@ from sparse import _is_dense, _is_sparse, _is_dense_result, _is_sparse_result
""" Types of sparse matrices to use for testing """ """ Types of sparse matrices to use for testing """
_mtypes = [sparse.csc_matrix, sparse.csr_matrix] _mtypes = [sparse.csc_matrix, sparse.csr_matrix]
#_mtypes = [sparse.csc_matrix, sparse.csr_matrix, sparse.dok_matrix, sparse.lil_matrix, sparse.coo_matrix] #_mtypes = [sparse.csc_matrix, sparse.csr_matrix, sparse.dok_matrix, sparse.lil_matrix, sparse.coo_matrix]
_mtypes_str = ["csc", "csr"] _mtype_to_str = {sparse.csc_matrix: "csc", sparse.csr_matrix: "csr"}
class T_transpose(unittest.TestCase): class T_transpose(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -252,59 +252,54 @@ class _testCase_dot(unittest.TestCase): ...@@ -252,59 +252,54 @@ class _testCase_dot(unittest.TestCase):
w = w.todense() w = w.todense()
self.failUnless((z == w).all() == True) self.failUnless((z == w).all() == True)
def test_missing(self):
raise NotImplementedError('tests commented out.')
def test_graph_bprop0(self): def test_graph_bprop0(self):
# x = tensor.astensor(numpy.random.rand(10,2)) for mtype in _mtypes:
# w = assparse(sparse.csr_matrix(
# numpy.asarray([[1, 0, 3, 0, 5], [0, 0, -2, 0,0]],dtype='float64')
# ))
for mtype in _mtypes_str:
# x = tensor.astensor([[1., 2], [3, 4], [2, 1]])
# w = assparse(mtype((500,3)))
# w.data[(10, 1)] = 1
# w.data[(20, 2)] = 2
x = tensor.Tensor('float64', broadcastable=[False,False], name='x') x = tensor.Tensor('float64', broadcastable=[False,False], name='x')
w = SparseResult('float64', mtype) w = SparseResult('float64', _mtype_to_str[mtype])
xw = dense_from_sparse(dot(x, w)) xw = dense_from_sparse(dot(w, x))
y = dense_from_sparse(dot(xw, w.T)) y = dense_from_sparse(dot(w.T, xw))
diff = x-y diff = x-y
loss = tensor.sum(tensor.sqr(diff)) loss = tensor.sum(tensor.sqr(diff))
gw = gradient.grad(loss, w) gw = gradient.grad(loss, w)
trainfn = compile.Function([x, w], [y, loss, gw]) trainfn = compile.Function([x, w], [y, loss, gw])
# for epoch in xrange(50): x = numpy.asarray([[1., 2], [3, 4], [2, 1]])
# gy = y-x w = mtype((500,3))
# g = grad.Grad({y:gy}) w[(10, 1)] = 1
# g.bprop() w[(20, 2)] = 2
# lr = 0.002 lr = 0.001
# g(w).data[1,0] = 0 y, origloss, gw = trainfn(x, w)
# g(w).data[1,4] = 0 for epoch in xrange(50):
# w.data = -lr * g(w).data + w.data y, loss, gw = trainfn(x, w)
# print loss.data w = w - (lr * gw)
self.failUnless('3.08560636025' == str(loss.data)) self.failUnless(origloss > loss)
self.failUnless('1.0543172285' == str(loss))
# def test_graph_bprop1(self):
# x = tensor.astensor(numpy.random.rand(10,2)) def test_graph_bprop_rand(self):
# w = assparse(sparse.csr_matrix( for i in range(10):
# numpy.asarray([[1, 0, 3, 0, 5], [0, 0, -2, 0,0]],dtype='float64') xorig = numpy.random.rand(3,2)
# )) for mtype in _mtypes:
# x = tensor.Tensor('float64', broadcastable=[False,False], name='x')
# for epoch in xrange(50): w = SparseResult('float64', _mtype_to_str[mtype])
# xw = dense_from_sparse(dot(x, w)) xw = dense_from_sparse(dot(w, x))
# y = dense_from_sparse(dot(xw, transpose(w))) y = dense_from_sparse(dot(w.T, xw))
# loss = core.sum(core.sqr(x-y)) diff = x-y
# g = grad.grad(loss) loss = tensor.sum(tensor.sqr(diff))
# lr = 0.001 gw = gradient.grad(loss, w)
# trainfn = compile.Function([x, w], [y, loss, gw])
# g(w).data[1,0] = 0
# g(w).data[1,4] = 0 x = xorig
# w.data = -lr * g(w).data + w.data w = mtype((500,3))
# w[(10, 1)] = 1
# self.failUnless('3.08560636025' == str(loss.data)) w[(20, 2)] = 2
lr = 0.001
y, origloss, gw = trainfn(x, w)
for epoch in xrange(50):
y, loss, gw = trainfn(x, w)
w = w - (lr * gw)
self.failUnless(origloss > loss)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论