提交 4e04febf authored 作者: Brandon T. Willard's avatar Brandon T. Willard

Apply pyupgrade to theano.sparse

上级 64db99f7
...@@ -15,7 +15,6 @@ import sys ...@@ -15,7 +15,6 @@ import sys
import numpy as np import numpy as np
import scipy.sparse import scipy.sparse
from numpy.lib.stride_tricks import as_strided from numpy.lib.stride_tricks import as_strided
from six import integer_types
import theano import theano
from theano import config, gof, scalar, tensor from theano import config, gof, scalar, tensor
...@@ -331,7 +330,7 @@ class SparseVariable(_sparse_py_operators, gof.Variable): ...@@ -331,7 +330,7 @@ class SparseVariable(_sparse_py_operators, gof.Variable):
format = property(lambda self: self.type.format) format = property(lambda self: self.type.format)
def __str__(self): def __str__(self):
return "%s{%s,%s}" % (self.__class__.__name__, self.format, self.dtype) return "{}{{{},{}}}".format(self.__class__.__name__, self.format, self.dtype)
def __repr__(self): def __repr__(self):
return str(self) return str(self)
...@@ -369,7 +368,7 @@ class SparseConstant(gof.Constant, _sparse_py_operators): ...@@ -369,7 +368,7 @@ class SparseConstant(gof.Constant, _sparse_py_operators):
return SparseConstantSignature((self.type, self.data)) return SparseConstantSignature((self.type, self.data))
def __str__(self): def __str__(self):
return "%s{%s,%s,shape=%s,nnz=%s}" % ( return "{}{{{},{},shape={},nnz={}}}".format(
self.__class__.__name__, self.__class__.__name__,
self.format, self.format,
self.dtype, self.dtype,
...@@ -844,7 +843,7 @@ class Cast(gof.op.Op): ...@@ -844,7 +843,7 @@ class Cast(gof.op.Op):
return ins_shapes return ins_shapes
def __str__(self): def __str__(self):
return "%s(%s)" % (self.__class__.__name__, self.out_type) return "{}({})".format(self.__class__.__name__, self.out_type)
bcast = Cast("int8") bcast = Cast("int8")
...@@ -893,7 +892,9 @@ class DenseFromSparse(gof.op.Op): ...@@ -893,7 +892,9 @@ class DenseFromSparse(gof.op.Op):
self.sparse_grad = structured self.sparse_grad = structured
def __str__(self): def __str__(self):
return "%s{structured_grad=%s}" % (self.__class__.__name__, self.sparse_grad) return "{}{{structured_grad={}}}".format(
self.__class__.__name__, self.sparse_grad
)
def make_node(self, x): def make_node(self, x):
x = as_sparse_variable(x) x = as_sparse_variable(x)
...@@ -971,7 +972,7 @@ class SparseFromDense(gof.op.Op): ...@@ -971,7 +972,7 @@ class SparseFromDense(gof.op.Op):
self.format = format self.format = format
def __str__(self): def __str__(self):
return "%s{%s}" % (self.__class__.__name__, self.format) return "{}{{{}}}".format(self.__class__.__name__, self.format)
def make_node(self, x): def make_node(self, x):
x = tensor.as_tensor_variable(x) x = tensor.as_tensor_variable(x)
...@@ -1322,10 +1323,8 @@ class GetItem2d(gof.op.Op): ...@@ -1322,10 +1323,8 @@ class GetItem2d(gof.op.Op):
) )
else: else:
raise ValueError( raise ValueError(
( "Advanced indexing is not implemented for sparse "
"Advanced indexing is not implemented for sparse " "matrices. Argument not supported: %s" % ind
"matrices. Argument not supported: %s" % ind
)
) )
input_op += [start, stop, step] input_op += [start, stop, step]
if len(index) == 1: if len(index) == 1:
...@@ -1397,7 +1396,7 @@ class GetItemScalar(gof.op.Op): ...@@ -1397,7 +1396,7 @@ class GetItemScalar(gof.op.Op):
raise Exception("GetItemScalar called with a slice as index!") raise Exception("GetItemScalar called with a slice as index!")
# in case of indexing using int instead of theano variable # in case of indexing using int instead of theano variable
elif isinstance(ind, integer_types): elif isinstance(ind, int):
ind = theano.tensor.constant(ind) ind = theano.tensor.constant(ind)
input_op += [ind] input_op += [ind]
...@@ -1714,7 +1713,7 @@ class SpSum(gof.op.Op): ...@@ -1714,7 +1713,7 @@ class SpSum(gof.op.Op):
# by the merge optimization and this requires them to compare equal. # by the merge optimization and this requires them to compare equal.
def __init__(self, axis=None, sparse_grad=True): def __init__(self, axis=None, sparse_grad=True):
super(SpSum, self).__init__() super().__init__()
self.axis = axis self.axis = axis
self.structured = sparse_grad self.structured = sparse_grad
if self.axis not in (None, 0, 1): if self.axis not in (None, 0, 1):
...@@ -2978,7 +2977,7 @@ class HStack(gof.op.Op): ...@@ -2978,7 +2977,7 @@ class HStack(gof.op.Op):
return [(ins_shapes[0][0], d)] return [(ins_shapes[0][0], d)]
def __str__(self): def __str__(self):
return "%s(%s,%s)" % (self.__class__.__name__, self.format, self.dtype) return "{}({},{})".format(self.__class__.__name__, self.format, self.dtype)
def hstack(blocks, format=None, dtype=None): def hstack(blocks, format=None, dtype=None):
......
...@@ -111,7 +111,7 @@ class AddSD_ccode(gof.op.Op): ...@@ -111,7 +111,7 @@ class AddSD_ccode(gof.op.Op):
inp = "" inp = ""
if self.inplace: if self.inplace:
inp = ",inplace" inp = ",inplace"
return "%s{%s%s}" % (self.__class__.__name__, self.format, inp) return "{}{{{}{}}}".format(self.__class__.__name__, self.format, inp)
def make_node(self, x, y): def make_node(self, x, y):
x, y = sparse.as_sparse_variable(x), tensor.as_tensor_variable(y) x, y = sparse.as_sparse_variable(x), tensor.as_tensor_variable(y)
......
...@@ -3,78 +3,16 @@ import scipy.sparse ...@@ -3,78 +3,16 @@ import scipy.sparse
import theano import theano
from theano import gof, tensor from theano import gof, tensor
from theano.sparse.basic import ( # To maintain compatibility from theano.sparse.basic import (
CSC,
CSM,
CSR,
AddSSData,
Cast,
CSMProperties,
HStack,
MulSV,
Remove0, Remove0,
SamplingDot,
SparseType, SparseType,
StructuredAddSV,
VStack,
_is_dense_variable,
_is_sparse, _is_sparse,
_is_sparse_variable,
add_s_s,
add_s_s_data,
as_sparse_variable, as_sparse_variable,
bcast,
ccast,
csm_data,
csm_indices,
csm_indptr,
csm_properties,
csm_shape,
dcast,
dot,
fcast,
hstack,
icast,
lcast,
mul_s_d,
mul_s_s,
mul_s_v,
neg,
remove0, remove0,
sampling_dot,
structured_add,
structured_add_s_v,
structured_exp,
structured_log,
structured_maximum,
structured_minimum,
structured_monoid,
structured_pow,
structured_sigmoid,
vstack,
wcast,
zcast,
) )
# Also for compatibility # Also for compatibility
from theano.sparse.opt import (
MulSDCSC,
MulSDCSR,
MulSVCSR,
SamplingDotCSR,
StructuredAddSVCSR,
local_mul_s_d,
local_mul_s_v,
local_sampling_dot_csr,
local_structured_add_s_v,
mul_s_d_csc,
mul_s_d_csr,
mul_s_v_csr,
sampling_dot_csr,
structured_add_s_v_csr,
)
from theano.tensor import discrete_dtypes, float_dtypes from theano.tensor import discrete_dtypes, float_dtypes
from theano.tensor.opt import register_specialize
# Probability Ops are currently back in sandbox, because they do not respect # Probability Ops are currently back in sandbox, because they do not respect
......
...@@ -8,7 +8,6 @@ try: ...@@ -8,7 +8,6 @@ try:
except ImportError: except ImportError:
imported_scipy = False imported_scipy = False
from six import string_types
import theano import theano
from theano import gof from theano import gof
...@@ -61,22 +60,20 @@ class SparseType(gof.Type): ...@@ -61,22 +60,20 @@ class SparseType(gof.Type):
"csc": scipy.sparse.csc_matrix, "csc": scipy.sparse.csc_matrix,
"bsr": scipy.sparse.bsr_matrix, "bsr": scipy.sparse.bsr_matrix,
} }
dtype_set = set( dtype_set = {
[ "int8",
"int8", "int16",
"int16", "int32",
"int32", "int64",
"int64", "float32",
"float32", "uint8",
"uint8", "uint16",
"uint16", "uint32",
"uint32", "uint64",
"uint64", "float64",
"float64", "complex64",
"complex64", "complex128",
"complex128", }
]
)
ndim = 2 ndim = 2
# Will be set to SparseVariable SparseConstant later. # Will be set to SparseVariable SparseConstant later.
...@@ -96,7 +93,7 @@ class SparseType(gof.Type): ...@@ -96,7 +93,7 @@ class SparseType(gof.Type):
'unsupported dtype "%s" not in list' % dtype, list(self.dtype_set) 'unsupported dtype "%s" not in list' % dtype, list(self.dtype_set)
) )
assert isinstance(format, string_types) assert isinstance(format, str)
if format in self.format_cls: if format in self.format_cls:
self.format = format self.format = format
else: else:
...@@ -123,7 +120,7 @@ class SparseType(gof.Type): ...@@ -123,7 +120,7 @@ class SparseType(gof.Type):
sp = self.format_cls[self.format](value) sp = self.format_cls[self.format](value)
if str(sp.dtype) != self.dtype: if str(sp.dtype) != self.dtype:
raise NotImplementedError( raise NotImplementedError(
"Expected %s dtype but got %s" % (self.dtype, str(sp.dtype)) "Expected {} dtype but got {}".format(self.dtype, str(sp.dtype))
) )
if sp.format != self.format: if sp.format != self.format:
raise NotImplementedError() raise NotImplementedError()
...@@ -165,10 +162,10 @@ class SparseType(gof.Type): ...@@ -165,10 +162,10 @@ class SparseType(gof.Type):
return hash(self.dtype) ^ hash(self.format) return hash(self.dtype) ^ hash(self.format)
def __str__(self): def __str__(self):
return "Sparse[%s, %s]" % (str(self.dtype), str(self.format)) return "Sparse[{}, {}]".format(str(self.dtype), str(self.format))
def __repr__(self): def __repr__(self):
return "Sparse[%s, %s]" % (str(self.dtype), str(self.format)) return "Sparse[{}, {}]".format(str(self.dtype), str(self.format))
def values_eq_approx(self, a, b, eps=1e-6): def values_eq_approx(self, a, b, eps=1e-6):
# WARNING: equality comparison of sparse matrices is not fast or easy # WARNING: equality comparison of sparse matrices is not fast or easy
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论