提交 b8fad4ca authored 作者: Iban Harlouchet's avatar Iban Harlouchet

flake8 theano/sparse/basic.py

上级 11a78c73
...@@ -16,7 +16,7 @@ from six.moves import xrange ...@@ -16,7 +16,7 @@ from six.moves import xrange
import scipy.sparse import scipy.sparse
import theano import theano
from theano import gof, tensor, compile, scalar, config from theano import gof, tensor, scalar, config
from theano.gradient import DisconnectedType from theano.gradient import DisconnectedType
from theano.sparse.utils import hash_from_sparse from theano.sparse.utils import hash_from_sparse
import theano.tests.unittest_tools as utt import theano.tests.unittest_tools as utt
...@@ -28,10 +28,10 @@ sparse_formats = ['csc', 'csr'] ...@@ -28,10 +28,10 @@ sparse_formats = ['csc', 'csr']
""" Types of sparse matrices to use for testing """ """ Types of sparse matrices to use for testing """
_mtypes = [scipy.sparse.csc_matrix, scipy.sparse.csr_matrix] _mtypes = [scipy.sparse.csc_matrix, scipy.sparse.csr_matrix]
#_mtypes = [sparse.csc_matrix, sparse.csr_matrix, sparse.dok_matrix, # _mtypes = [sparse.csc_matrix, sparse.csr_matrix, sparse.dok_matrix,
# sparse.lil_matrix, sparse.coo_matrix] # sparse.lil_matrix, sparse.coo_matrix]
#* new class ``dia_matrix`` : the sparse DIAgonal format # * new class ``dia_matrix`` : the sparse DIAgonal format
#* new class ``bsr_matrix`` : the Block CSR format # * new class ``bsr_matrix`` : the Block CSR format
_mtype_to_str = {scipy.sparse.csc_matrix: "csc", _mtype_to_str = {scipy.sparse.csc_matrix: "csc",
scipy.sparse.csr_matrix: "csr"} scipy.sparse.csr_matrix: "csr"}
...@@ -153,7 +153,8 @@ def verify_grad_sparse(op, pt, structured=False, *args, **kwargs): ...@@ -153,7 +153,8 @@ def verify_grad_sparse(op, pt, structured=False, *args, **kwargs):
:return: None :return: None
""" """
conv_none = lambda x: x def conv_none(x):
return x
def conv_csr(ind, indptr, shp): def conv_csr(ind, indptr, shp):
def f(spdata): def f(spdata):
...@@ -358,9 +359,9 @@ class SparseVariable(_sparse_py_operators, gof.Variable): ...@@ -358,9 +359,9 @@ class SparseVariable(_sparse_py_operators, gof.Variable):
def __str__(self): def __str__(self):
return '%s{%s,%s}' % ( return '%s{%s,%s}' % (
self.__class__.__name__, self.__class__.__name__,
self.format, self.format,
self.dtype) self.dtype)
def __repr__(self): def __repr__(self):
return str(self) return str(self)
...@@ -369,11 +370,11 @@ class SparseVariable(_sparse_py_operators, gof.Variable): ...@@ -369,11 +370,11 @@ class SparseVariable(_sparse_py_operators, gof.Variable):
class SparseConstantSignature(tuple): class SparseConstantSignature(tuple):
def __eq__(self, other): def __eq__(self, other):
(a, b), (x, y) = self, other (a, b), (x, y) = self, other
return a == x \ return a == x and\
and (b.dtype == y.dtype)\ (b.dtype == y.dtype) and\
and (type(b) == type(y))\ (type(b) == type(y)) and\
and (b.shape == y.shape)\ (b.shape == y.shape) and\
and (abs(b - y).sum() < 1e-6 * b.nnz) (abs(b - y).sum() < 1e-6 * b.nnz)
def __hash__(self): def __hash__(self):
(a, b) = self (a, b) = self
...@@ -394,11 +395,11 @@ class SparseConstant(gof.Constant, _sparse_py_operators): ...@@ -394,11 +395,11 @@ class SparseConstant(gof.Constant, _sparse_py_operators):
def __str__(self): def __str__(self):
return '%s{%s,%s,shape=%s,nnz=%s}' % ( return '%s{%s,%s,shape=%s,nnz=%s}' % (
self.__class__.__name__, self.__class__.__name__,
self.format, self.format,
self.dtype, self.dtype,
self.data.shape, self.data.shape,
self.data.nnz) self.data.nnz)
def __repr__(self): def __repr__(self):
return str(self) return str(self)
...@@ -490,7 +491,8 @@ class CSMProperties(gof.Op): ...@@ -490,7 +491,8 @@ class CSMProperties(gof.Op):
data = tensor.TensorType(dtype=csm.type.dtype, data = tensor.TensorType(dtype=csm.type.dtype,
broadcastable=(False,)).make_variable() broadcastable=(False,)).make_variable()
return gof.Apply(self, [csm], return gof.Apply(self, [csm],
[data, tensor.ivector(), tensor.ivector(), tensor.ivector()]) [data, tensor.ivector(),
tensor.ivector(), tensor.ivector()])
def perform(self, node, inputs, out): def perform(self, node, inputs, out):
(csm,) = inputs (csm,) = inputs
...@@ -658,7 +660,7 @@ class CSM(gof.Op): ...@@ -658,7 +660,7 @@ class CSM(gof.Op):
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')
if (data.shape != indices.shape and numpy.size(data) != if (data.shape != indices.shape and numpy.size(data) !=
numpy.size(self.kmap)): numpy.size(self.kmap)):
errmsg = ('Data (shape ' + repr(data.shape) + errmsg = ('Data (shape ' + repr(data.shape) +
' must have the same number of elements ' + ' must have the same number of elements ' +
'as indices (shape' + repr(indices.shape) + 'as indices (shape' + repr(indices.shape) +
...@@ -684,7 +686,7 @@ class CSM(gof.Op): ...@@ -684,7 +686,7 @@ class CSM(gof.Op):
g_data, g_indices, g_indptr, g_shape = csm_properties(g_out) g_data, g_indices, g_indptr, g_shape = csm_properties(g_out)
# unpack the data vector and wrap it as a 1d TensorType # unpack the data vector and wrap it as a 1d TensorType
g_data = csm_grad(self.kmap)(x_data, x_indices, x_indptr, x_shape, g_data = csm_grad(self.kmap)(x_data, x_indices, x_indptr, x_shape,
g_data, g_indices, g_indptr, g_shape) g_data, g_indices, g_indptr, g_shape)
return [g_data, DisconnectedType()(), DisconnectedType()(), DisconnectedType()()] return [g_data, DisconnectedType()(), DisconnectedType()(), DisconnectedType()()]
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
...@@ -776,14 +778,14 @@ class CSMGrad(gof.op.Op): ...@@ -776,14 +778,14 @@ class CSMGrad(gof.op.Op):
self.kmap) self.kmap)
def make_node(self, x_data, x_indices, x_indptr, x_shape, def make_node(self, x_data, x_indices, x_indptr, x_shape,
g_data, g_indices, g_indptr, g_shape): g_data, g_indices, g_indptr, g_shape):
gout_data = g_data.type() gout_data = g_data.type()
return gof.Apply(self, [x_data, x_indices, x_indptr, x_shape, return gof.Apply(self, [x_data, x_indices, x_indptr, x_shape,
g_data, g_indices, g_indptr, g_shape], [gout_data]) g_data, g_indices, g_indptr, g_shape], [gout_data])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x_data, x_indices, x_indptr, x_shape, (x_data, x_indices, x_indptr, x_shape,
g_data, g_indices, g_indptr, g_shape) = inputs g_data, g_indices, g_indptr, g_shape) = inputs
(g_out,) = outputs (g_out,) = outputs
if len(x_indptr) - 1 == x_shape[0]: if len(x_indptr) - 1 == x_shape[0]:
sp_dim = x_shape[1] sp_dim = x_shape[1]
...@@ -919,7 +921,7 @@ class DenseFromSparse(gof.op.Op): ...@@ -919,7 +921,7 @@ class DenseFromSparse(gof.op.Op):
[x], [x],
[tensor.TensorType(dtype=x.type.dtype, [tensor.TensorType(dtype=x.type.dtype,
broadcastable=(False, False) broadcastable=(False, False)
).make_variable()]) ).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x,) = inputs (x,) = inputs
...@@ -990,8 +992,8 @@ class SparseFromDense(gof.op.Op): ...@@ -990,8 +992,8 @@ class SparseFromDense(gof.op.Op):
x = tensor.as_tensor_variable(x) x = tensor.as_tensor_variable(x)
if x.ndim > 2: if x.ndim > 2:
raise TypeError( raise TypeError(
"Theano does not have sparse tensor types with more " "Theano does not have sparse tensor types with more "
"than 2 dimensions, but %s.ndim = %i" % (x, x.ndim)) "than 2 dimensions, but %s.ndim = %i" % (x, x.ndim))
elif x.ndim == 1: elif x.ndim == 1:
x = x.dimshuffle('x', 0) x = x.dimshuffle('x', 0)
elif x.ndim == 0: elif x.ndim == 0:
...@@ -1003,7 +1005,7 @@ class SparseFromDense(gof.op.Op): ...@@ -1003,7 +1005,7 @@ class SparseFromDense(gof.op.Op):
[x], [x],
[SparseType(dtype=x.type.dtype, [SparseType(dtype=x.type.dtype,
format=self.format format=self.format
).make_variable()]) ).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x,) = inputs (x,) = inputs
...@@ -1243,7 +1245,7 @@ class GetItem2d(gof.op.Op): ...@@ -1243,7 +1245,7 @@ class GetItem2d(gof.op.Op):
# def infer_shape(self, node, i0_shapes): # def infer_shape(self, node, i0_shapes):
# return i0_shapes # return i0_shapes
def make_node(self, x, index): def make_node(self, x, index):
scipy_ver = [ int(n) for n in scipy.__version__.split('.')[:2]] scipy_ver = [int(n) for n in scipy.__version__.split('.')[:2]]
x = as_sparse_variable(x) x = as_sparse_variable(x)
assert x.format in ["csr", "csc"] assert x.format in ["csr", "csc"]
assert len(index) in [1, 2] assert len(index) in [1, 2]
...@@ -1260,11 +1262,11 @@ class GetItem2d(gof.op.Op): ...@@ -1260,11 +1262,11 @@ class GetItem2d(gof.op.Op):
# If start or stop or step are None, make them a Generic # If start or stop or step are None, make them a Generic
# constant. Else, they should be converted to Tensor Variables # constant. Else, they should be converted to Tensor Variables
# of dimension 1 and int/uint dtype. # of dimension 1 and int/uint dtype.
if scipy_ver < [0, 14] and ind.step != None: if scipy_ver < [0, 14] and ind.step is not None:
raise ValueError( raise ValueError(
'Slice with step is not support with current' 'Slice with step is not support with current'
' version of Scipy.') ' version of Scipy.')
if ind.step is None or ind.step == 1: if ind.step is None or ind.step == 1:
step = generic_None step = generic_None
else: else:
if not isinstance(step, gof.Variable): if not isinstance(step, gof.Variable):
...@@ -1301,8 +1303,8 @@ class GetItem2d(gof.op.Op): ...@@ -1301,8 +1303,8 @@ class GetItem2d(gof.op.Op):
stop.ndim, stop.dtype) stop.ndim, stop.dtype)
elif ((isinstance(ind, gof.Variable) and elif ((isinstance(ind, gof.Variable) and
getattr(ind, 'ndim', -1) == 0) getattr(ind, 'ndim', -1) == 0) or
or numpy.isscalar(ind)): numpy.isscalar(ind)):
raise NotImplementedError( raise NotImplementedError(
'Theano has no sparse vector' + 'Theano has no sparse vector' +
'Use X[a:b, c:d], X[a:b, c:c+1] or X[a:b] instead.') 'Use X[a:b, c:d], X[a:b, c:c+1] or X[a:b] instead.')
...@@ -1439,7 +1441,7 @@ class Transpose(gof.op.Op): ...@@ -1439,7 +1441,7 @@ class Transpose(gof.op.Op):
[x], [x],
[SparseType(dtype=x.type.dtype, [SparseType(dtype=x.type.dtype,
format=self.format_map[x.type.format] format=self.format_map[x.type.format]
).make_variable()]) ).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x,) = inputs (x,) = inputs
...@@ -1832,11 +1834,11 @@ class SquareDiagonal(gof.op.Op): ...@@ -1832,11 +1834,11 @@ class SquareDiagonal(gof.op.Op):
raise TypeError('data argument must be a vector', diag.type) raise TypeError('data argument must be a vector', diag.type)
return gof.Apply(self, [diag], return gof.Apply(self, [diag],
[SparseType(dtype=diag.dtype, format='csc')()]) [SparseType(dtype=diag.dtype, format='csc')()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(z,) = outputs (z,) = outputs
diag, o_shape = inputs[0], inputs[0].shape * 2 diag = inputs[0]
N = len(diag) N = len(diag)
data = diag[:N] data = diag[:N]
...@@ -1960,7 +1962,7 @@ class AddSS(gof.op.Op): ...@@ -1960,7 +1962,7 @@ class AddSS(gof.op.Op):
[x, y], [x, y],
[SparseType(dtype=out_dtype, [SparseType(dtype=out_dtype,
format=x.type.format format=x.type.format
).make_variable()]) ).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
...@@ -2001,7 +2003,8 @@ class AddSSData(gof.op.Op): ...@@ -2001,7 +2003,8 @@ class AddSSData(gof.op.Op):
return gof.Apply(self, return gof.Apply(self,
[x, y], [x, y],
[SparseType(dtype=x.type.dtype, [SparseType(dtype=x.type.dtype,
format=x.type.format).make_variable()]) format=x.type.format
).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
...@@ -2067,10 +2070,10 @@ class AddSD(gof.op.Op): ...@@ -2067,10 +2070,10 @@ class AddSD(gof.op.Op):
[x, y], [x, y],
[tensor.TensorType(dtype=out_dtype, [tensor.TensorType(dtype=out_dtype,
broadcastable=y.type.broadcastable broadcastable=y.type.broadcastable
).make_variable()]) ).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
(out,) = outputs (out,) = outputs
assert _is_dense(y) assert _is_dense(y)
...@@ -2110,7 +2113,8 @@ class StructuredAddSV(gof.op.Op): ...@@ -2110,7 +2113,8 @@ class StructuredAddSV(gof.op.Op):
return gof.Apply(self, return gof.Apply(self,
[x, y], [x, y],
[SparseType(dtype=x.type.dtype, [SparseType(dtype=x.type.dtype,
format=x.type.format).make_variable()]) format=x.type.format
).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
...@@ -2222,10 +2226,10 @@ class MulSS(gof.op.Op): ...@@ -2222,10 +2226,10 @@ class MulSS(gof.op.Op):
assert x.format in ["csr", "csc"] assert x.format in ["csr", "csc"]
assert y.format in ["csr", "csc"] assert y.format in ["csr", "csc"]
out_dtype = scalar.upcast(x.type.dtype, y.type.dtype) out_dtype = scalar.upcast(x.type.dtype, y.type.dtype)
return gof.Apply(self, [x, y], return gof.Apply(self,
[x, y],
[SparseType(dtype=out_dtype, [SparseType(dtype=out_dtype,
format=x.type.format format=x.type.format)()])
)()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
...@@ -2300,7 +2304,6 @@ class MulSD(gof.op.Op): ...@@ -2300,7 +2304,6 @@ class MulSD(gof.op.Op):
out_dtype = node.outputs[0].dtype out_dtype = node.outputs[0].dtype
if x.format == 'csc': if x.format == 'csc':
x_data = x.data
indices = x.indices indices = x.indices
indptr = x.indptr indptr = x.indptr
if x.dtype == out_dtype: if x.dtype == out_dtype:
...@@ -2315,7 +2318,6 @@ class MulSD(gof.op.Op): ...@@ -2315,7 +2318,6 @@ class MulSD(gof.op.Op):
z_data[i_idx] *= y[i, j] z_data[i_idx] *= y[i, j]
out[0] = z out[0] = z
elif x.format == 'csr': elif x.format == 'csr':
x_data = x.data
indices = x.indices indices = x.indices
indptr = x.indptr indptr = x.indptr
if x.dtype == out_dtype: if x.dtype == out_dtype:
...@@ -2363,12 +2365,13 @@ class MulSV(gof.op.Op): ...@@ -2363,12 +2365,13 @@ class MulSV(gof.op.Op):
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
raise NotImplementedError( raise NotImplementedError(
"MulSV not implemented for differing dtypes." "MulSV not implemented for differing dtypes."
"Got %s and %s." % (str(x.type.dtype), str(y.type.dtype))) "Got %s and %s." % (str(x.type.dtype), str(y.type.dtype)))
return gof.Apply(self, return gof.Apply(self,
[x, y], [x, y],
[SparseType(dtype=x.type.dtype, [SparseType(dtype=x.type.dtype,
format=x.type.format).make_variable()]) format=x.type.format
).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
...@@ -2483,7 +2486,8 @@ class __ComparisonOpSS(gof.op.Op): ...@@ -2483,7 +2486,8 @@ class __ComparisonOpSS(gof.op.Op):
return gof.Apply(self, return gof.Apply(self,
[x, y], [x, y],
[SparseType(dtype='uint8', [SparseType(dtype='uint8',
format=x.type.format).make_variable()]) format=x.type.format
).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
...@@ -2527,7 +2531,8 @@ class __ComparisonOpSD(gof.op.Op): ...@@ -2527,7 +2531,8 @@ class __ComparisonOpSD(gof.op.Op):
return gof.Apply(self, return gof.Apply(self,
[x, y], [x, y],
[SparseType(dtype='uint8', [SparseType(dtype='uint8',
format=x.type.format).make_variable()]) format=x.type.format
).make_variable()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
(x, y) = inputs (x, y) = inputs
...@@ -2765,9 +2770,11 @@ class HStack(gof.op.Op): ...@@ -2765,9 +2770,11 @@ class HStack(gof.op.Op):
for x in var: for x in var:
assert x.format in ["csr", "csc"] assert x.format in ["csr", "csc"]
return gof.Apply( return gof.Apply(self,
self, var, var,
[SparseType(dtype=self.dtype, format=self.format).make_variable()]) [SparseType(dtype=self.dtype,
format=self.format
).make_variable()])
def perform(self, node, block, outputs): def perform(self, node, block, outputs):
(out,) = outputs (out,) = outputs
...@@ -2852,7 +2859,7 @@ class VStack(HStack): ...@@ -2852,7 +2859,7 @@ class VStack(HStack):
def grad(self, inputs, gout): def grad(self, inputs, gout):
(gz,) = gout (gz,) = gout
is_continuous = [(inputs[i].dtype in tensor.continuous_dtypes) is_continuous = [(inputs[i].dtype in tensor.continuous_dtypes)
for i in range(len(inputs))] for i in range(len(inputs))]
if _is_sparse_variable(gz): if _is_sparse_variable(gz):
gz = dense_from_sparse(gz) gz = dense_from_sparse(gz)
...@@ -3729,11 +3736,10 @@ def structured_dot_grad(sparse_A, dense_B, ga): ...@@ -3729,11 +3736,10 @@ def structured_dot_grad(sparse_A, dense_B, ga):
sdgcsx = sdg_csr sdgcsx = sdg_csr
CSx = CSR CSx = CSR
g_A_data = sdgcsx(csm_indices(sparse_A), \ g_A_data = sdgcsx(csm_indices(sparse_A),
csm_indptr(sparse_A), dense_B, ga) csm_indptr(sparse_A), dense_B, ga)
return CSx(g_A_data, csm_indices(sparse_A), \ return CSx(g_A_data, csm_indices(sparse_A),
csm_indptr(sparse_A), \ csm_indptr(sparse_A), csm_shape(sparse_A))
csm_shape(sparse_A))
else: else:
raise NotImplementedError() raise NotImplementedError()
...@@ -3756,7 +3762,7 @@ class SamplingDot(gof.op.Op): ...@@ -3756,7 +3762,7 @@ class SamplingDot(gof.op.Op):
raise TypeError(p) raise TypeError(p)
# TODO: use it. # TODO: use it.
dtype_out = scalar.upcast(x.type.dtype, y.type.dtype, p.type.dtype) dtype_out = scalar.upcast(x.type.dtype, y.type.dtype, p.type.dtype) # noqa
return gof.Apply(self, [x, y, p], [p.type()]) return gof.Apply(self, [x, y, p], [p.type()])
...@@ -3857,7 +3863,8 @@ class Dot(gof.op.Op): ...@@ -3857,7 +3863,8 @@ class Dot(gof.op.Op):
y_is_sparse_var = _is_sparse_variable(y) y_is_sparse_var = _is_sparse_variable(y)
if not x_is_sparse_var and not y_is_sparse_var: if not x_is_sparse_var and not y_is_sparse_var:
raise TypeError("Sparse dot product should have at least one " raise TypeError(
"Sparse dot product should have at least one "
"sparse variable as inputs, but the inputs are " "sparse variable as inputs, but the inputs are "
"%s (%s) and %s (%s)." % (x, x.type, y, y.type)) "%s (%s) and %s (%s)." % (x, x.type, y, y.type))
......
...@@ -231,7 +231,6 @@ whitelist_flake8 = [ ...@@ -231,7 +231,6 @@ whitelist_flake8 = [
"sparse/type.py", "sparse/type.py",
"sparse/__init__.py", "sparse/__init__.py",
"sparse/opt.py", "sparse/opt.py",
"sparse/basic.py",
"sparse/tests/test_utils.py", "sparse/tests/test_utils.py",
"sparse/tests/test_opt.py", "sparse/tests/test_opt.py",
"sparse/tests/test_basic.py", "sparse/tests/test_basic.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论