提交 15db4a1f authored 作者: Christos Tsirigotis's avatar Christos Tsirigotis

Fix flake8 in extra_ops and tests

- Resolve :215 of test_extra_ops to int explicitly Reason: numpy.uint64 + int -> numpy.float64 for some reason (numpy/numpy@3509704)
上级 458a5ccd
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import unittest
import numpy as np import numpy as np
import numpy import numpy
...@@ -62,8 +61,7 @@ class TestSearchsortedOp(utt.InferShapeTester): ...@@ -62,8 +61,7 @@ class TestSearchsortedOp(utt.InferShapeTester):
def test_searchsortedOp_on_sorted_input(self): def test_searchsortedOp_on_sorted_input(self):
f = theano.function([self.x, self.v], searchsorted(self.x, self.v)) f = theano.function([self.x, self.v], searchsorted(self.x, self.v))
assert np.allclose( assert np.allclose(np.searchsorted(self.a[self.idx_sorted], self.b),
np.searchsorted(self.a[self.idx_sorted], self.b),
f(self.a[self.idx_sorted], self.b)) f(self.a[self.idx_sorted], self.b))
def test_searchsortedOp_on_float_sorter(self): def test_searchsortedOp_on_float_sorter(self):
...@@ -79,15 +77,13 @@ class TestSearchsortedOp(utt.InferShapeTester): ...@@ -79,15 +77,13 @@ class TestSearchsortedOp(utt.InferShapeTester):
f = theano.function([self.x, self.v, sorter], f = theano.function([self.x, self.v, sorter],
searchsorted(self.x, self.v, sorter=sorter), searchsorted(self.x, self.v, sorter=sorter),
allow_input_downcast=True) allow_input_downcast=True)
assert np.allclose( assert np.allclose(np.searchsorted(self.a, self.b, sorter=self.idx_sorted),
np.searchsorted(self.a, self.b, sorter=self.idx_sorted),
f(self.a, self.b, self.idx_sorted)) f(self.a, self.b, self.idx_sorted))
def test_searchsortedOp_on_right_side(self): def test_searchsortedOp_on_right_side(self):
f = theano.function([self.x, self.v], f = theano.function([self.x, self.v],
searchsorted(self.x, self.v, side='right')) searchsorted(self.x, self.v, side='right'))
assert np.allclose( assert np.allclose(np.searchsorted(self.a, self.b, side='right'),
np.searchsorted(self.a, self.b, side='right'),
f(self.a, self.b)) f(self.a, self.b))
def test_infer_shape(self): def test_infer_shape(self):
...@@ -218,8 +214,9 @@ class TestBinCountOp(utt.InferShapeTester): ...@@ -218,8 +214,9 @@ class TestBinCountOp(utt.InferShapeTester):
def test_bincountFn(self): def test_bincountFn(self):
w = T.vector('w') w = T.vector('w')
def ref(data, w=None, minlength=None): def ref(data, w=None, minlength=None):
size = data.max() + 1 size = int(data.max() + 1)
if minlength: if minlength:
size = max(size, minlength) size = max(size, minlength)
if w is not None: if w is not None:
...@@ -231,6 +228,7 @@ class TestBinCountOp(utt.InferShapeTester): ...@@ -231,6 +228,7 @@ class TestBinCountOp(utt.InferShapeTester):
for i in range(data.shape[0]): for i in range(data.shape[0]):
out[data[i]] += 1 out[data[i]] += 1
return out return out
for dtype in ('int8', 'int16', 'int32', 'int64', for dtype in ('int8', 'int16', 'int32', 'int64',
'uint8', 'uint16', 'uint32', 'uint64'): 'uint8', 'uint16', 'uint32', 'uint64'):
x = T.vector('x', dtype=dtype) x = T.vector('x', dtype=dtype)
...@@ -304,16 +302,14 @@ class TestBinCountOp(utt.InferShapeTester): ...@@ -304,16 +302,14 @@ class TestBinCountOp(utt.InferShapeTester):
self.assertRaises(TypeError, BinCountOp(), x) self.assertRaises(TypeError, BinCountOp(), x)
else: else:
self._compile_and_check( self._compile_and_check([x],
[x], [BinCountOp()(x, None)],
[BinCountOp()(x,None)],
[np.random.random_integers( [np.random.random_integers(
50, size=(25,)).astype(dtype)], 50, size=(25,)).astype(dtype)],
self.op_class) self.op_class)
weights = np.random.random((25,)).astype(config.floatX) weights = np.random.random((25,)).astype(config.floatX)
self._compile_and_check( self._compile_and_check([x],
[x],
[BinCountOp()(x, weights=weights)], [BinCountOp()(x, weights=weights)],
[np.random.random_integers( [np.random.random_integers(
50, size=(25,)).astype(dtype)], 50, size=(25,)).astype(dtype)],
...@@ -321,15 +317,13 @@ class TestBinCountOp(utt.InferShapeTester): ...@@ -321,15 +317,13 @@ class TestBinCountOp(utt.InferShapeTester):
if not numpy_16: if not numpy_16:
continue continue
self._compile_and_check( self._compile_and_check([x],
[x],
[BinCountOp(minlength=60)(x, weights=weights)], [BinCountOp(minlength=60)(x, weights=weights)],
[np.random.random_integers( [np.random.random_integers(
50, size=(25,)).astype(dtype)], 50, size=(25,)).astype(dtype)],
self.op_class) self.op_class)
self._compile_and_check( self._compile_and_check([x],
[x],
[BinCountOp(minlength=5)(x, weights=weights)], [BinCountOp(minlength=5)(x, weights=weights)],
[np.random.random_integers( [np.random.random_integers(
50, size=(25,)).astype(dtype)], 50, size=(25,)).astype(dtype)],
...@@ -508,9 +502,9 @@ class TestRepeatOp(utt.InferShapeTester): ...@@ -508,9 +502,9 @@ class TestRepeatOp(utt.InferShapeTester):
r_var = T.scalar(dtype=dtype) r_var = T.scalar(dtype=dtype)
r = numpy.asarray(3, dtype=dtype) r = numpy.asarray(3, dtype=dtype)
if (dtype == 'uint64' or if (dtype == 'uint64' or
(dtype in self.numpy_unsupported_dtypes and r_var.ndim == 1)): (dtype in self.numpy_unsupported_dtypes and
self.assertRaises(TypeError, r_var.ndim == 1)):
repeat, x, r_var, axis=axis) self.assertRaises(TypeError, repeat, x, r_var, axis=axis)
else: else:
f = theano.function([x, r_var], f = theano.function([x, r_var],
repeat(x, r_var, axis=axis)) repeat(x, r_var, axis=axis))
...@@ -534,8 +528,9 @@ class TestRepeatOp(utt.InferShapeTester): ...@@ -534,8 +528,9 @@ class TestRepeatOp(utt.InferShapeTester):
assert np.allclose(np.repeat(a, r, axis=axis), assert np.allclose(np.repeat(a, r, axis=axis),
f(a, r)) f(a, r))
#check when r is a list of single integer, e.g. [3]. # check when r is a list of single integer, e.g. [3].
r = np.random.random_integers(10, size=()).astype(dtype) + 2 r = np.random.random_integers(
10, size=()).astype(dtype) + 2
f = theano.function([x], f = theano.function([x],
repeat(x, [r], axis=axis)) repeat(x, [r], axis=axis))
assert np.allclose(np.repeat(a, r, axis=axis), assert np.allclose(np.repeat(a, r, axis=axis),
...@@ -569,8 +564,7 @@ class TestRepeatOp(utt.InferShapeTester): ...@@ -569,8 +564,7 @@ class TestRepeatOp(utt.InferShapeTester):
r_var = T.vector(dtype=dtype) r_var = T.vector(dtype=dtype)
self.assertRaises(TypeError, repeat, x, r_var) self.assertRaises(TypeError, repeat, x, r_var)
else: else:
self._compile_and_check( self._compile_and_check([x, r_var],
[x, r_var],
[RepeatOp(axis=axis)(x, r_var)], [RepeatOp(axis=axis)(x, r_var)],
[a, r], [a, r],
self.op_class) self.op_class)
...@@ -717,17 +711,17 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -717,17 +711,17 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
# We can't use numpy.fill_diagonal as it is bugged. # We can't use numpy.fill_diagonal as it is bugged.
assert numpy.allclose(numpy.diag(out, test_offset), val) assert numpy.allclose(numpy.diag(out, test_offset), val)
if test_offset >= 0: if test_offset >= 0:
assert (out == val).sum() == min( min(a.shape), assert (out == val).sum() == min(min(a.shape),
a.shape[1]-test_offset ) a.shape[1] - test_offset)
else: else:
assert (out == val).sum() == min( min(a.shape), assert (out == val).sum() == min(min(a.shape),
a.shape[0]+test_offset ) a.shape[0] + test_offset)
def test_gradient(self): def test_gradient(self):
for test_offset in (-5, -4, -1, 0, 1, 4, 5): for test_offset in (-5, -4, -1, 0, 1, 4, 5):
# input 'offset' will not be tested # input 'offset' will not be tested
def fill_diagonal_with_fix_offset( a, val): def fill_diagonal_with_fix_offset(a, val):
return fill_diagonal_offset( a, val, test_offset) return fill_diagonal_offset(a, val, test_offset)
utt.verify_grad(fill_diagonal_with_fix_offset, utt.verify_grad(fill_diagonal_with_fix_offset,
[numpy.random.rand(5, 8), numpy.random.rand()], [numpy.random.rand(5, 8), numpy.random.rand()],
...@@ -748,12 +742,12 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -748,12 +742,12 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
[numpy.random.rand(8, 5), [numpy.random.rand(8, 5),
numpy.random.rand(), numpy.random.rand(),
test_offset], test_offset],
self.op_class ) self.op_class)
self._compile_and_check([x, y, z], [self.op(x, y, z)], self._compile_and_check([x, y, z], [self.op(x, y, z)],
[numpy.random.rand(5, 8), [numpy.random.rand(5, 8),
numpy.random.rand(), numpy.random.rand(),
test_offset], test_offset],
self.op_class ) self.op_class)
def test_to_one_hot(): def test_to_one_hot():
...@@ -783,6 +777,7 @@ def test_to_one_hot(): ...@@ -783,6 +777,7 @@ def test_to_one_hot():
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]]) [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]])
class test_Unique(utt.InferShapeTester): class test_Unique(utt.InferShapeTester):
def setUp(self): def setUp(self):
...@@ -792,7 +787,7 @@ class test_Unique(utt.InferShapeTester): ...@@ -792,7 +787,7 @@ class test_Unique(utt.InferShapeTester):
Unique(True), Unique(True),
Unique(False, True), Unique(False, True),
Unique(True, True)] Unique(True, True)]
if bool(numpy_ver >= [1, 9]) : if bool(numpy_ver >= [1, 9]):
self.ops.extend([ self.ops.extend([
Unique(False, False, True), Unique(False, False, True),
Unique(True, False, True), Unique(True, False, True),
...@@ -805,18 +800,18 @@ class test_Unique(utt.InferShapeTester): ...@@ -805,18 +800,18 @@ class test_Unique(utt.InferShapeTester):
Done by using the op and checking that it returns the right answer. Done by using the op and checking that it returns the right answer.
""" """
x = theano.tensor.vector() x = theano.tensor.vector()
inp = np.asarray([2,1,3,2], dtype=config.floatX) inp = np.asarray([2, 1, 3, 2], dtype=config.floatX)
list_outs_expected = [[np.unique(inp)], list_outs_expected = [[np.unique(inp)],
np.unique(inp, True), np.unique(inp, True),
np.unique(inp, False, True), np.unique(inp, False, True),
np.unique(inp, True, True)] np.unique(inp, True, True)]
if bool(numpy_ver >= [1, 9]) : if bool(numpy_ver >= [1, 9]):
list_outs_expected.extend([ list_outs_expected.extend([
np.unique(inp, False, False, True), np.unique(inp, False, False, True),
np.unique(inp, True, False, True), np.unique(inp, True, False, True),
np.unique(inp, False, True, True), np.unique(inp, False, True, True),
np.unique(inp, True, True, True)]) np.unique(inp, True, True, True)])
for op, outs_expected in zip(self.ops, list_outs_expected) : for op, outs_expected in zip(self.ops, list_outs_expected):
f = theano.function(inputs=[x], outputs=op(x, return_list=True)) f = theano.function(inputs=[x], outputs=op(x, return_list=True))
outs = f(inp) outs = f(inp)
# Compare the result computed to the expected value. # Compare the result computed to the expected value.
...@@ -833,7 +828,7 @@ class test_Unique(utt.InferShapeTester): ...@@ -833,7 +828,7 @@ class test_Unique(utt.InferShapeTester):
np.unique(inp, True), np.unique(inp, True),
np.unique(inp, False, True), np.unique(inp, False, True),
np.unique(inp, True, True)] np.unique(inp, True, True)]
if bool(numpy_ver >= [1, 9]) : if bool(numpy_ver >= [1, 9]):
list_outs_expected.extend([ list_outs_expected.extend([
np.unique(inp, False, False, True), np.unique(inp, False, False, True),
np.unique(inp, True, False, True), np.unique(inp, True, False, True),
...@@ -855,13 +850,13 @@ class test_Unique(utt.InferShapeTester): ...@@ -855,13 +850,13 @@ class test_Unique(utt.InferShapeTester):
for op in self.ops: for op in self.ops:
if not op.return_inverse: if not op.return_inverse:
continue continue
if op.return_index : if op.return_index:
f = op(x)[2] f = op(x)[2]
else: else:
f = op(x)[1] f = op(x)[1]
self._compile_and_check([x], self._compile_and_check([x],
[f], [f],
[np.asarray(np.array([2,1,3,2]), [np.asarray(np.array([2, 1, 3, 2]),
dtype=config.floatX)], dtype=config.floatX)],
self.op_class) self.op_class)
...@@ -874,13 +869,12 @@ class test_Unique(utt.InferShapeTester): ...@@ -874,13 +869,12 @@ class test_Unique(utt.InferShapeTester):
for op in self.ops: for op in self.ops:
if not op.return_inverse: if not op.return_inverse:
continue continue
if op.return_index : if op.return_index:
f = op(x)[2] f = op(x)[2]
else: else:
f = op(x)[1] f = op(x)[1]
self._compile_and_check([x], self._compile_and_check([x],
[f], [f],
[np.asarray(np.array([[2, 1], [3, 2],[2, 3]]), [np.asarray(np.array([[2, 1], [3, 2], [2, 3]]),
dtype=config.floatX)], dtype=config.floatX)],
self.op_class) self.op_class)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论