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

flake8 for tensor/type.py ; one E left

上级 acd37bd7
import logging import logging
_logger = logging.getLogger("theano.tensor.type") import warnings
import numpy import numpy
import theano import theano
from theano import config from theano import config
from theano.gof import Constant, hashtype, Type, Variable from theano.gof import hashtype, Type, Variable
from theano.gof.utils import MethodNotDefined
from theano import scalar as scal from theano import scalar as scal
_logger = logging.getLogger("theano.tensor.type")
class TensorType(Type): class TensorType(Type):
"""Symbolic `Type` representing a numpy.ndarray value.""" """Symbolic `Type` representing a numpy.ndarray value."""
...@@ -39,7 +40,7 @@ class TensorType(Type): ...@@ -39,7 +40,7 @@ class TensorType(Type):
if self.dtype == 'floatX': if self.dtype == 'floatX':
self.dtype = config.floatX self.dtype = config.floatX
# broadcastable is immutable, and all elements are either # broadcastable is immutable, and all elements are either
### True or False # True or False
self.broadcastable = tuple(bool(b) for b in broadcastable) self.broadcastable = tuple(bool(b) for b in broadcastable)
self.dtype_specs() # error checking is done there self.dtype_specs() # error checking is done there
self.name = name self.name = name
...@@ -74,17 +75,17 @@ class TensorType(Type): ...@@ -74,17 +75,17 @@ class TensorType(Type):
# input (typical mistake, especially with shared variables). # input (typical mistake, especially with shared variables).
if isinstance(data, Variable): if isinstance(data, Variable):
raise TypeError( raise TypeError(
'Expected an array-like object, but found a Variable: ' 'Expected an array-like object, but found a Variable: '
'maybe you are trying to call a function on a (possibly ' 'maybe you are trying to call a function on a (possibly '
'shared) variable instead of a numeric array?') 'shared) variable instead of a numeric array?')
if ((type(data) is numpy.ndarray) if ((type(data) is numpy.ndarray) and
and (data.dtype == self.numpy_dtype)): (data.dtype == self.numpy_dtype)):
if data.dtype.num != self.numpy_dtype.num: if data.dtype.num != self.numpy_dtype.num:
data = theano._asarray(data, dtype=self.dtype) data = theano._asarray(data, dtype=self.dtype)
# -- now fall through to ndim check # -- now fall through to ndim check
elif((type(data) is numpy.memmap) elif ((type(data) is numpy.memmap) and
and (data.dtype == self.numpy_dtype)): (data.dtype == self.numpy_dtype)):
# numpy.memmap is a "safe" subclass of ndarray, # numpy.memmap is a "safe" subclass of ndarray,
# so we can use it whereever we expect a base ndarray. # so we can use it whereever we expect a base ndarray.
# however, casting it would defeat the purpose of not # however, casting it would defeat the purpose of not
...@@ -95,11 +96,11 @@ class TensorType(Type): ...@@ -95,11 +96,11 @@ class TensorType(Type):
# we raise a meaningful TypeError. # we raise a meaningful TypeError.
if not (type(data) is numpy.ndarray): if not (type(data) is numpy.ndarray):
raise TypeError("%s expected a ndarray object." % self, raise TypeError("%s expected a ndarray object." % self,
data, type(data)) data, type(data))
if data.dtype != self.numpy_dtype: if data.dtype != self.numpy_dtype:
raise TypeError(("%s expected a ndarray object with " raise TypeError(("%s expected a ndarray object with "
"dtype = %s (got %s).") % ( "dtype = %s (got %s).") %
self, self.numpy_dtype, data.dtype)) (self, self.numpy_dtype, data.dtype))
assert False, "This point should never be reached." assert False, "This point should never be reached."
else: else:
if allow_downcast: if allow_downcast:
...@@ -185,7 +186,7 @@ class TensorType(Type): ...@@ -185,7 +186,7 @@ class TensorType(Type):
" dimension.", data.shape, self.broadcastable) " dimension.", data.shape, self.broadcastable)
i += 1 i += 1
if (self.filter_checks_isfinite and if (self.filter_checks_isfinite and
not numpy.all(numpy.isfinite(data))): not numpy.all(numpy.isfinite(data))):
raise ValueError("non-finite elements not allowed") raise ValueError("non-finite elements not allowed")
return data return data
...@@ -208,14 +209,12 @@ class TensorType(Type): ...@@ -208,14 +209,12 @@ class TensorType(Type):
return other return other
raise TypeError( raise TypeError(
'Cannot convert Type %(othertype)s ' 'Cannot convert Type %(othertype)s '
'(of Variable %(other)s) into Type %(self)s. ' '(of Variable %(other)s) into Type %(self)s. '
'You can try to manually convert %(other)s into a %(self)s.' 'You can try to manually convert %(other)s into a %(self)s.' %
% dict( dict(othertype=other.type,
othertype=other.type, other=other,
other=other, self=self))
self=self)
)
def value_validity_msg(self, a): def value_validity_msg(self, a):
try: try:
...@@ -247,10 +246,10 @@ class TensorType(Type): ...@@ -247,10 +246,10 @@ class TensorType(Type):
'int64': (int, 'npy_int64', 'NPY_INT64'), 'int64': (int, 'npy_int64', 'NPY_INT64'),
'complex128': (complex, 'theano_complex128', 'NPY_COMPLEX128'), 'complex128': (complex, 'theano_complex128', 'NPY_COMPLEX128'),
'complex64': (complex, 'theano_complex64', 'NPY_COMPLEX64') 'complex64': (complex, 'theano_complex64', 'NPY_COMPLEX64')
}[self.dtype] }[self.dtype]
except KeyError: except KeyError:
raise TypeError("Unsupported dtype for %s: %s" raise TypeError("Unsupported dtype for %s: %s"
% (self.__class__.__name__, self.dtype)) % (self.__class__.__name__, self.dtype))
def to_scalar_type(self): def to_scalar_type(self):
return scal.get_scalar_type(dtype=self.dtype) return scal.get_scalar_type(dtype=self.dtype)
...@@ -265,7 +264,7 @@ class TensorType(Type): ...@@ -265,7 +264,7 @@ class TensorType(Type):
self.dtype == var.type.dtype and self.dtype == var.type.dtype and
self.ndim == var.type.ndim and self.ndim == var.type.ndim and
all(sb == ob or ob for sb, ob in zip(self.broadcastable, all(sb == ob or ob for sb, ob in zip(self.broadcastable,
var.type.broadcastable))): var.type.broadcastable))):
return theano.tensor.patternbroadcast(var, self.broadcastable) return theano.tensor.patternbroadcast(var, self.broadcastable)
@staticmethod @staticmethod
...@@ -351,7 +350,7 @@ class TensorType(Type): ...@@ -351,7 +350,7 @@ class TensorType(Type):
rtol = 1.0000000000000001e-05 rtol = 1.0000000000000001e-05
atol = 1e-8 atol = 1e-8
cmp_elemwise = (numpy.absolute(a - b) <= cmp_elemwise = (numpy.absolute(a - b) <=
(atol + rtol * numpy.absolute(b))) (atol + rtol * numpy.absolute(b)))
# Find places where both a and b have missing values. # Find places where both a and b have missing values.
both_missing = a_missing * numpy.isnan(b) both_missing = a_missing * numpy.isnan(b)
...@@ -361,9 +360,9 @@ class TensorType(Type): ...@@ -361,9 +360,9 @@ class TensorType(Type):
# cmp_elemwise is weird when we have inf and -inf. # cmp_elemwise is weird when we have inf and -inf.
# set it to False # set it to False
cmp_elemwise = numpy.where( cmp_elemwise = numpy.where(
both_inf & cmp_elemwise, both_inf & cmp_elemwise,
a == b, a == b,
cmp_elemwise) cmp_elemwise)
# check the sign of the inf # check the sign of the inf
both_inf = numpy.where(both_inf, (a == b), both_inf) both_inf = numpy.where(both_inf, (a == b), both_inf)
...@@ -383,7 +382,7 @@ class TensorType(Type): ...@@ -383,7 +382,7 @@ class TensorType(Type):
return hashtype(self) ^ hash(self.dtype) ^ hash(self.broadcastable) return hashtype(self) ^ hash(self.dtype) ^ hash(self.broadcastable)
ndim = property(lambda self: len(self.broadcastable), ndim = property(lambda self: len(self.broadcastable),
doc="number of dimensions") doc="number of dimensions")
"""Number of dimensions """Number of dimensions
This read-only property is the preferred way to get the number of This read-only property is the preferred way to get the number of
...@@ -407,10 +406,10 @@ class TensorType(Type): ...@@ -407,10 +406,10 @@ class TensorType(Type):
else: else:
b = self.broadcastable b = self.broadcastable
named_broadcastable = {(): 'scalar', named_broadcastable = {(): 'scalar',
(False,): 'vector', (False,): 'vector',
(False, True): 'col', (False, True): 'col',
(True, False): 'row', (True, False): 'row',
(False, False): 'matrix'} (False, False): 'matrix'}
if b in named_broadcastable: if b in named_broadcastable:
bcast = named_broadcastable[b] bcast = named_broadcastable[b]
else: else:
...@@ -422,7 +421,7 @@ class TensorType(Type): ...@@ -422,7 +421,7 @@ class TensorType(Type):
def __repr__(self): def __repr__(self):
return str(self) return str(self)
#"TensorType{%s, %s}" % (str(self.dtype), str(self.broadcastable)) # "TensorType{%s, %s}" % (str(self.dtype), str(self.broadcastable))
def c_declare(self, name, sub, check_input=True): def c_declare(self, name, sub, check_input=True):
"""Override `CLinkerType.c_declare` """ """Override `CLinkerType.c_declare` """
...@@ -636,13 +635,13 @@ def values_eq_approx_always_true(a, b): ...@@ -636,13 +635,13 @@ def values_eq_approx_always_true(a, b):
# Register TensorType C code for ViewOp. # Register TensorType C code for ViewOp.
theano.compile.register_view_op_c_code( theano.compile.register_view_op_c_code(
TensorType, TensorType,
""" """
Py_XDECREF(%(oname)s); Py_XDECREF(%(oname)s);
%(oname)s = %(iname)s; %(oname)s = %(iname)s;
Py_XINCREF(%(oname)s); Py_XINCREF(%(oname)s);
""", """,
version=1) version=1)
# Register TensorType C code for Shape Op. # Register TensorType C code for Shape Op.
...@@ -665,51 +664,51 @@ theano.compile.register_shape_c_code( ...@@ -665,51 +664,51 @@ theano.compile.register_shape_c_code(
# Register TensorType C code for ViewOp. # Register TensorType C code for ViewOp.
theano.compile.register_shape_i_c_code( theano.compile.register_shape_i_c_code(
TensorType, TensorType,
""" """
if(!%(oname)s) if(!%(oname)s)
%(oname)s=(PyArrayObject*)PyArray_EMPTY(0, NULL, NPY_INT64, 0); %(oname)s=(PyArrayObject*)PyArray_EMPTY(0, NULL, NPY_INT64, 0);
((npy_int64*)PyArray_DATA(%(oname)s))[0]=PyArray_DIMS(%(iname)s)[%(i)s]; ((npy_int64*)PyArray_DATA(%(oname)s))[0]=PyArray_DIMS(%(iname)s)[%(i)s];
""", """,
""" """
if (%(i)s>=PyArray_NDIM(%(iname)s)){ if (%(i)s>=PyArray_NDIM(%(iname)s)){
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"Number of dimensions lower than expected"); "Number of dimensions lower than expected");
%(fail)s %(fail)s
} }
""", """,
version=3) version=3)
# Register TensorType C code for DeepCopyOp # Register TensorType C code for DeepCopyOp
theano.compile.register_deep_copy_op_c_code( theano.compile.register_deep_copy_op_c_code(
TensorType, TensorType,
""" """
int alloc = %(oname)s == NULL; int alloc = %(oname)s == NULL;
for(int i=0; !alloc && i<PyArray_NDIM(%(oname)s); i++) { for(int i=0; !alloc && i<PyArray_NDIM(%(oname)s); i++) {
if(PyArray_DIMS(%(iname)s)[i] != PyArray_DIMS(%(oname)s)[i]) { if(PyArray_DIMS(%(iname)s)[i] != PyArray_DIMS(%(oname)s)[i]) {
alloc = true; alloc = true;
break; break;
} }
}
if(alloc) {
Py_XDECREF(%(oname)s);
%(oname)s = (PyArrayObject*)PyArray_NewCopy(%(iname)s,
NPY_ANYORDER);
if (!%(oname)s)
{
PyErr_SetString(PyExc_ValueError,
"DeepCopyOp: the copy failed!");
%(fail)s;
} }
if(alloc) { } else {
Py_XDECREF(%(oname)s); if(PyArray_CopyInto(%(oname)s, %(iname)s)){
%(oname)s = (PyArrayObject*)PyArray_NewCopy(%(iname)s, PyErr_SetString(PyExc_ValueError,
NPY_ANYORDER); "DeepCopyOp: the copy failed into already allocated space!");
if (!%(oname)s) %(fail)s;
{
PyErr_SetString(PyExc_ValueError,
"DeepCopyOp: the copy failed!");
%(fail)s;
}
} else {
if(PyArray_CopyInto(%(oname)s, %(iname)s)){
PyErr_SetString(PyExc_ValueError,
"DeepCopyOp: the copy failed into already allocated space!");
%(fail)s;
}
} }
""", }
version=2) """,
version=2)
theano.compile.register_rebroadcast_c_code( theano.compile.register_rebroadcast_c_code(
...@@ -723,7 +722,7 @@ theano.compile.register_rebroadcast_c_code( ...@@ -723,7 +722,7 @@ theano.compile.register_rebroadcast_c_code(
%(fail)s %(fail)s
} }
""", """,
version=1) version=1)
theano.compile.register_specify_shape_c_code( theano.compile.register_specify_shape_c_code(
......
...@@ -57,7 +57,6 @@ whitelist_flake8 = [ ...@@ -57,7 +57,6 @@ whitelist_flake8 = [
"typed_list/tests/test_type.py", "typed_list/tests/test_type.py",
"typed_list/tests/test_opt.py", "typed_list/tests/test_opt.py",
"typed_list/tests/test_basic.py", "typed_list/tests/test_basic.py",
"tensor/type.py",
"tensor/fourier.py", "tensor/fourier.py",
"tensor/__init__.py", "tensor/__init__.py",
"tensor/opt_uncanonicalize.py", "tensor/opt_uncanonicalize.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论