提交 ca670ce6 authored 作者: Nicolas Bouchard's avatar Nicolas Bouchard 提交者: Frederic

Made correction and remove dupplicate of eliminate_zeros

上级 e70f9594
import theano
import numpy import numpy
import scipy.sparse import scipy.sparse
from theano import gof, tensor, scalar from theano import gof, tensor, scalar
from theano.tensor import blas from theano.tensor import blas
from theano import tensor as T
from theano.sparse.basic import ( from theano.sparse.basic import (
as_sparse_variable, SparseType, add_s_s, neg, as_sparse_variable, SparseType, add_s_s, neg,
...@@ -11,10 +11,14 @@ from theano.sparse.basic import ( ...@@ -11,10 +11,14 @@ from theano.sparse.basic import (
CSMProperties, CSM, register_specialize, CSMProperties, CSM, register_specialize,
_is_sparse_variable, _is_dense_variable, CSC, CSR, _is_sparse_variable, _is_dense_variable, CSC, CSR,
csm_properties, csm_data, csm_indices, csm_indptr, csm_shape, csm_properties, csm_data, csm_indices, csm_indptr, csm_shape,
_is_sparse) _is_sparse, Remove0, remove0)
from theano.sparse.sandbox.sp import sp_sum from theano.sparse.sandbox.sp import sp_sum
EliminateZeros = Remove0
eliminate_zeros = remove0
class Cast(gof.op.Op): class Cast(gof.op.Op):
"""Cast sparse variable to the desired dtype. """Cast sparse variable to the desired dtype.
...@@ -42,7 +46,7 @@ class Cast(gof.op.Op): ...@@ -42,7 +46,7 @@ class Cast(gof.op.Op):
out[0] = x.astype(self.out_type) out[0] = x.astype(self.out_type)
def grad(self, inputs, outputs_gradients): def grad(self, inputs, outputs_gradients):
if inputs[0].dtype in T.continuous_dtypes: if inputs[0].dtype in tensor.continuous_dtypes:
gz = outputs_gradients[0] gz = outputs_gradients[0]
return [Cast(inputs[0].dtype)(gz)] return [Cast(inputs[0].dtype)(gz)]
else: else:
...@@ -52,7 +56,7 @@ class Cast(gof.op.Op): ...@@ -52,7 +56,7 @@ class Cast(gof.op.Op):
return ins_shapes return ins_shapes
def __str__(self): def __str__(self):
return self.__class__.__name__ return "%s(%s)" % (self.__class__.__name__, self.out_type)
def cast(x, t): def cast(x, t):
...@@ -341,6 +345,10 @@ mul_s_d_csr = MulSDCSR() ...@@ -341,6 +345,10 @@ mul_s_d_csr = MulSDCSR()
class Poisson(gof.op.Op): class Poisson(gof.op.Op):
"""Return a sparse having random values from a poisson density
with mean from the input.
"""
def __eq__(self, other): def __eq__(self, other):
return (type(self) == type(other)) return (type(self) == type(other))
...@@ -357,10 +365,28 @@ class Poisson(gof.op.Op): ...@@ -357,10 +365,28 @@ class Poisson(gof.op.Op):
out[0].data = numpy.asarray(numpy.random.poisson(out[0].data), out[0].data = numpy.asarray(numpy.random.poisson(out[0].data),
dtype=x.dtype) dtype=x.dtype)
out[0].eliminate_zeros() out[0].eliminate_zeros()
def grad(self, inputs, outputs_gradients):
return [None]
def infer_shape(self, node, ins_shapes):
return ins_shapes
def __str__(self):
return self.__class__.__name__
poisson = Poisson() poisson = Poisson()
class Multinomial(gof.op.Op): class Multinomial(gof.op.Op):
"""Return a sparse matrix having random values from a multinomial
density having number of experiment `n` and probability of succes
`p`.
:Parameters:
- `n`: Number of experiment.
- `p`: Sparse probability of each of the different outcomes.
"""
def __eq__(self, other): def __eq__(self, other):
return (type(self) == type(other)) return (type(self) == type(other))
...@@ -383,38 +409,16 @@ class Multinomial(gof.op.Op): ...@@ -383,38 +409,16 @@ class Multinomial(gof.op.Op):
for i in xrange(p.shape[0]): for i in xrange(p.shape[0]):
k, l = p.indptr[i], p.indptr[i + 1] k, l = p.indptr[i], p.indptr[i + 1]
out[0].data[k:l] = numpy.random.multinomial(n[i], p.data[k:l]) out[0].data[k:l] = numpy.random.multinomial(n[i], p.data[k:l])
multinomial = Multinomial()
class EliminateZeros(gof.op.Op):
"""Eliminate zeros from the data of the matrix.
This wrap the method eliminate_zeros from scipy.
"""
def __eq__(self, other):
return (type(self) == type(other))
def __hash__(self):
return hash(type(self))
def make_node(self, x):
x = as_sparse_variable(x)
return gof.Apply(self, [x], [x.type()])
def perform(self, node, (x, ), (out, )):
assert _is_sparse(x)
out[0] = x.copy()
out[0].eliminate_zeros()
def grad(self, inputs, outputs_gradients): def grad(self, inputs, outputs_gradients):
return outputs_gradients return [None, None]
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return ins_shapes return ins_shapes
def __str__(self): def __str__(self):
return self.__class__.__name__ return self.__class__.__name__
eliminate_zeros = EliminateZeros() multinomial = Multinomial()
class Binomial(gof.op.Op): class Binomial(gof.op.Op):
...@@ -450,6 +454,12 @@ class Binomial(gof.op.Op): ...@@ -450,6 +454,12 @@ class Binomial(gof.op.Op):
def grad(self, (n, p, shape, ), (gz,)): def grad(self, (n, p, shape, ), (gz,)):
return None, None, None return None, None, None
def infer_shape(self, node, ins_shapes):
return ins_shapes
def __str__(self):
return self.__class__.__name__
csr_fbinomial = Binomial('csr', 'float32') csr_fbinomial = Binomial('csr', 'float32')
csc_fbinomial = Binomial('csc', 'float32') csc_fbinomial = Binomial('csc', 'float32')
csr_dbinomial = Binomial('csr', 'float64') csr_dbinomial = Binomial('csr', 'float64')
......
...@@ -1320,15 +1320,21 @@ def test_size(): ...@@ -1320,15 +1320,21 @@ def test_size():
check() check()
def test_remove0(): class Remove0Tester(utt.InferShapeTester):
def setUp(self):
super(Remove0Tester, self).setUp()
self.op_class = Remove0
def test_remove0(self):
configs = [ configs = [
# structure type, numpy matching class # structure type, numpy matching class
('csc', scipy.sparse.csc_matrix), ('csc', scipy.sparse.csc_matrix),
('csr', scipy.sparse.csr_matrix), ('csr', scipy.sparse.csr_matrix), ]
]
for format, matrix_class in configs: for format, matrix_class in configs:
# real # real
origin = (numpy.arange(9) + 1).reshape((3, 3)).astype(config.floatX) origin = (numpy.arange(9) + 1).reshape((3, 3))
origin.astype(config.floatX)
mat = matrix_class(origin).astype(theano.config.floatX) mat = matrix_class(origin).astype(theano.config.floatX)
mat[0, 1] = mat[1, 0] = mat[2, 2] = 0 mat[0, 1] = mat[1, 0] = mat[2, 2] = 0
...@@ -1347,9 +1353,10 @@ def test_remove0(): ...@@ -1347,9 +1353,10 @@ def test_remove0():
if theano.config.mode not in ['FAST_COMPILE']: if theano.config.mode not in ['FAST_COMPILE']:
# list of apply nodes in the optimized graph. # list of apply nodes in the optimized graph.
nodes = f.maker.env.toposort() nodes = f.maker.env.toposort()
v = [True for node in nodes v = [True for node in nodes]
if isinstance(node.op, Remove0) and node.op.inplace] if isinstance(node.op, Remove0) and node.op.inplace:
assert len(v), 'Inplacing optimization should have been applied.' assert len(v), \
'Inplacing optimization should have been applied.'
# checking # checking
# makes sense to change its name # makes sense to change its name
...@@ -1359,6 +1366,34 @@ def test_remove0(): ...@@ -1359,6 +1366,34 @@ def test_remove0():
msg = 'Matrices sizes differ. Have zeros been removed ?' msg = 'Matrices sizes differ. Have zeros been removed ?'
assert result.size == target.size, msg assert result.size == target.size, msg
def test_infer_shape(self):
mat = (numpy.arange(9) + 1).reshape((3, 3))
mat[0, 1] = mat[1, 0] = mat[2, 2] = 0
x_csc = theano.sparse.csc_matrix(dtype=theano.config.floatX)
mat_csc = sp.csc_matrix(mat, dtype=theano.config.floatX)
self._compile_and_check([x_csc],
[Remove0()(x_csc)],
[mat_csc],
self.op_class)
x_csr = theano.sparse.csr_matrix(dtype=theano.config.floatX)
mat_csr = sp.csr_matrix(mat, dtype=theano.config.floatX)
self._compile_and_check([x_csr],
[Remove0()(x_csr)],
[mat_csr],
self.op_class)
def test_grad(self):
mat = (numpy.arange(9) + 1).reshape((3, 3))
mat[0, 1] = mat[1, 0] = mat[2, 2] = 0
mat_csc = sp.csc_matrix(mat, dtype=theano.config.floatX)
verify_grad_sparse(Remove0(), [mat_csc])
mat_csr = sp.csr_matrix(mat, dtype=theano.config.floatX)
verify_grad_sparse(Remove0(), [mat_csr])
class Test_getitem(unittest.TestCase): class Test_getitem(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -10,10 +10,10 @@ except ImportError: ...@@ -10,10 +10,10 @@ except ImportError:
pass # The variable enable_sparse will be used to disable the test file. pass # The variable enable_sparse will be used to disable the test file.
import theano import theano
from theano import tensor
from theano import sparse
from theano import tensor as T if not theano.sparse.enable_sparse:
from theano import sparse as S
if not S.enable_sparse:
raise SkipTest('Optional package sparse disabled') raise SkipTest('Optional package sparse disabled')
from theano.sparse.sandbox import sp2 as S2 from theano.sparse.sandbox import sp2 as S2
...@@ -52,9 +52,10 @@ def random_lil(shape, dtype, nnz): ...@@ -52,9 +52,10 @@ def random_lil(shape, dtype, nnz):
class TestCast(utt.InferShapeTester): class TestCast(utt.InferShapeTester):
compatible_types = T.int_dtypes + T.continuous_dtypes compatible_types = (tensor.int_dtypes +
x_csc = [S.csc_matrix(dtype=t) for t in compatible_types] tensor.continuous_dtypes)
x_csr = [S.csr_matrix(dtype=t) for t in compatible_types] x_csc = [theano.sparse.csc_matrix(dtype=t) for t in compatible_types]
x_csr = [theano.sparse.csr_matrix(dtype=t) for t in compatible_types]
indptr = np.array([0, 2, 3, 6]) indptr = np.array([0, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2]) indices = np.array([0, 2, 2, 0, 1, 2])
...@@ -78,7 +79,7 @@ class TestCast(utt.InferShapeTester): ...@@ -78,7 +79,7 @@ class TestCast(utt.InferShapeTester):
for x in self.x_csc: for x in self.x_csc:
for f, t in zip(cast_csc[x], self.compatible_types): for f, t in zip(cast_csc[x], self.compatible_types):
a = sp.csc_matrix(self.properties, dtype=x.dtype) a = sp.csc_matrix(self.properties, dtype=x.dtype).copy()
assert f(a).dtype == t assert f(a).dtype == t
for x in self.x_csr: for x in self.x_csr:
...@@ -104,63 +105,19 @@ class TestCast(utt.InferShapeTester): ...@@ -104,63 +105,19 @@ class TestCast(utt.InferShapeTester):
self.op_class) self.op_class)
def test_grad(self): def test_grad(self):
for dtype in T.float_dtypes: # TODO Find the problem with the grad of downcast
# a = sp.csc_matrix(self.properties, dtype='float64')
# verify_grad_sparse(S2.Cast('float32'), [a], cast_to_output_type=True)
for dtype in tensor.float_dtypes:
a = sp.csc_matrix(self.properties, dtype=dtype) a = sp.csc_matrix(self.properties, dtype=dtype)
verify_grad_sparse(S2.Cast('float64'), [a]) verify_grad_sparse(S2.Cast('float64'), [a])
for dtype in T.float_dtypes: for dtype in tensor.float_dtypes:
a = sp.csr_matrix(self.properties, dtype=dtype) a = sp.csr_matrix(self.properties, dtype=dtype)
verify_grad_sparse(S2.Cast('float64'), [a]) verify_grad_sparse(S2.Cast('float64'), [a])
class EliminateZerosTester(utt.InferShapeTester):
indptr = np.array([0, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 0, 3, 0, 5, 6], dtype='float32')
properties = (data, indices, indptr)
x_csc = S.csc_matrix('csc', dtype='float32')
x_csr = S.csr_matrix('csr', dtype='float32')
def setUp(self):
super(EliminateZerosTester, self).setUp()
self.op_class = S2.EliminateZeros
def test_eliminate_zeros(self):
f_csc = theano.function([self.x_csc], S2.eliminate_zeros(self.x_csc))
f_csr = theano.function([self.x_csr], S2.eliminate_zeros(self.x_csr))
a = sp.csc_matrix(self.properties, dtype='float32')
b = a.copy()
b.eliminate_zeros()
assert np.all(f_csc(a).todense() == b.todense())
a = sp.csr_matrix(self.properties)
b = a.copy()
b.eliminate_zeros()
assert np.all(f_csr(a).todense() == b.todense())
def test_infer_shape(self):
a = sp.csc_matrix(self.properties, dtype='float32')
self._compile_and_check([self.x_csc],
[S2.eliminate_zeros(self.x_csc)],
[a],
self.op_class)
a = sp.csr_matrix(self.properties, dtype='float32')
self._compile_and_check([self.x_csr],
[S2.eliminate_zeros(self.x_csr)],
[a],
self.op_class)
def test_grad(self):
a = sp.csc_matrix(self.properties, dtype='float32')
verify_grad_sparse(S2.eliminate_zeros, [a])
a = sp.csr_matrix(self.properties, dtype='float32')
verify_grad_sparse(S2.eliminate_zeros, [a])
class test_structured_add_s_v(unittest.TestCase): class test_structured_add_s_v(unittest.TestCase):
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
...@@ -174,7 +131,7 @@ class test_structured_add_s_v(unittest.TestCase): ...@@ -174,7 +131,7 @@ class test_structured_add_s_v(unittest.TestCase):
spmat = sp_types[format](random_lil((4, 3), dtype, 3)) spmat = sp_types[format](random_lil((4, 3), dtype, 3))
mat = np.asarray(np.random.rand(3), dtype=dtype) mat = np.asarray(np.random.rand(3), dtype=dtype)
S.verify_grad_sparse(S2.structured_add_s_v, theano.sparse.verify_grad_sparse(S2.structured_add_s_v,
[spmat, mat], structured=True) [spmat, mat], structured=True)
def test_structured_add_s_v(self): def test_structured_add_s_v(self):
...@@ -183,8 +140,8 @@ class test_structured_add_s_v(unittest.TestCase): ...@@ -183,8 +140,8 @@ class test_structured_add_s_v(unittest.TestCase):
for format in ['csr', 'csc']: for format in ['csr', 'csc']:
for dtype in ['float32', 'float64']: for dtype in ['float32', 'float64']:
x = S.SparseType(format, dtype=dtype)() x = theano.sparse.SparseType(format, dtype=dtype)()
y = T.vector(dtype=dtype) y = tensor.vector(dtype=dtype)
f = theano.function([x, y], S2.structured_add_s_v(x, y)) f = theano.function([x, y], S2.structured_add_s_v(x, y))
spmat = sp_types[format](random_lil((4, 3), dtype, 3)) spmat = sp_types[format](random_lil((4, 3), dtype, 3))
...@@ -210,7 +167,7 @@ class test_mul_s_v(unittest.TestCase): ...@@ -210,7 +167,7 @@ class test_mul_s_v(unittest.TestCase):
spmat = sp_types[format](random_lil((4, 3), dtype, 3)) spmat = sp_types[format](random_lil((4, 3), dtype, 3))
mat = np.asarray(np.random.rand(3), dtype=dtype) mat = np.asarray(np.random.rand(3), dtype=dtype)
S.verify_grad_sparse(S2.mul_s_v, theano.sparse.verify_grad_sparse(S2.mul_s_v,
[spmat, mat], structured=True) [spmat, mat], structured=True)
def test_mul_s_v(self): def test_mul_s_v(self):
...@@ -219,8 +176,8 @@ class test_mul_s_v(unittest.TestCase): ...@@ -219,8 +176,8 @@ class test_mul_s_v(unittest.TestCase):
for format in ['csr', 'csc']: for format in ['csr', 'csc']:
for dtype in ['float32', 'float64']: for dtype in ['float32', 'float64']:
x = S.SparseType(format, dtype=dtype)() x = theano.sparse.SparseType(format, dtype=dtype)()
y = T.vector(dtype=dtype) y = tensor.vector(dtype=dtype)
f = theano.function([x, y], S2.mul_s_v(x, y)) f = theano.function([x, y], S2.mul_s_v(x, y))
spmat = sp_types[format](random_lil((4, 3), dtype, 3)) spmat = sp_types[format](random_lil((4, 3), dtype, 3))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论