PEP8 fixes in tests.

This commit is contained in:
Francois Chollet 2017-01-11 11:40:57 -08:00
parent 309f586424
commit c10945f53a
8 changed files with 20 additions and 14 deletions

@ -108,8 +108,8 @@ def test_stacked_lstm_char_prediction():
y = np.zeros((len(sentences), number_of_chars), dtype=np.bool) y = np.zeros((len(sentences), number_of_chars), dtype=np.bool)
for i, sentence in enumerate(sentences): for i, sentence in enumerate(sentences):
for t, char in enumerate(sentence): for t, char in enumerate(sentence):
X[i, t, ord(char)-ord('a')] = 1 X[i, t, ord(char) - ord('a')] = 1
y[i, ord(next_chars[i])-ord('a')] = 1 y[i, ord(next_chars[i]) - ord('a')] = 1
# learn the alphabet with stacked LSTM # learn the alphabet with stacked LSTM
model = Sequential([ model = Sequential([
@ -123,7 +123,7 @@ def test_stacked_lstm_char_prediction():
# prime the model with 'ab' sequence and let it generate the learned alphabet # prime the model with 'ab' sequence and let it generate the learned alphabet
sentence = alphabet[:sequence_length] sentence = alphabet[:sequence_length]
generated = sentence generated = sentence
for iteration in range(number_of_chars-sequence_length): for iteration in range(number_of_chars - sequence_length):
x = np.zeros((1, sequence_length, number_of_chars)) x = np.zeros((1, sequence_length, number_of_chars))
for t, char in enumerate(sentence): for t, char in enumerate(sentence):
x[0, t, ord(char) - ord('a')] = 1. x[0, t, ord(char) - ord('a')] = 1.

@ -790,7 +790,7 @@ class TestBackend(object):
# len max_time_steps array of batch_size x depth matrices # len max_time_steps array of batch_size x depth matrices
inputs = ([input_prob_matrix_0[t, :][np.newaxis, :] inputs = ([input_prob_matrix_0[t, :][np.newaxis, :]
for t in range(seq_len_0)] + # Pad to max_time_steps = 8 for t in range(seq_len_0)] + # Pad to max_time_steps = 8
2 * [np.zeros((1, depth), dtype=np.float32)]) 2 * [np.zeros((1, depth), dtype=np.float32)])
inputs = KTF.variable(np.asarray(inputs).transpose((1, 0, 2))) inputs = KTF.variable(np.asarray(inputs).transpose((1, 0, 2)))
@ -899,7 +899,7 @@ class TestBackend(object):
def test_foldl(self): def test_foldl(self):
x = np.random.rand(10, 3).astype(np.float32) x = np.random.rand(10, 3).astype(np.float32)
for K in [KTF, KTH]: for K in [KTF, KTH]:
kx = K.eval(K.foldl(lambda a, b: a+b, x)) kx = K.eval(K.foldl(lambda a, b: a + b, x))
assert (3,) == kx.shape assert (3,) == kx.shape
assert_allclose(x.sum(axis=0), kx, atol=1e-05) assert_allclose(x.sum(axis=0), kx, atol=1e-05)
@ -911,8 +911,8 @@ class TestBackend(object):
# right to left we have no such problem and the result is larger # right to left we have no such problem and the result is larger
x = np.array([1e-20, 1e-20, 10, 10, 10], dtype=np.float32) x = np.array([1e-20, 1e-20, 10, 10, 10], dtype=np.float32)
for K in [KTF, KTH]: for K in [KTF, KTH]:
p1 = K.eval(K.foldl(lambda a, b: a*b, x)) p1 = K.eval(K.foldl(lambda a, b: a * b, x))
p2 = K.eval(K.foldr(lambda a, b: a*b, x)) p2 = K.eval(K.foldr(lambda a, b: a * b, x))
assert p1 < p2 assert p1 < p2
assert 9e-38 < p2 <= 1e-37 assert 9e-38 < p2 <= 1e-37

@ -9,6 +9,7 @@ from keras import backend as K
from keras.models import model_from_json, model_from_yaml from keras.models import model_from_json, model_from_yaml
from keras.utils.test_utils import keras_test from keras.utils.test_utils import keras_test
@keras_test @keras_test
def test_get_updates_for(): def test_get_updates_for():
a = Input(shape=(2,)) a = Input(shape=(2,))
@ -26,7 +27,7 @@ def test_get_losses_for():
dense_layer = Dense(1) dense_layer = Dense(1)
dense_layer.add_loss(0, inputs=a) dense_layer.add_loss(0, inputs=a)
dense_layer.add_loss(1, inputs=None) dense_layer.add_loss(1, inputs=None)
assert dense_layer.get_losses_for(a) == [0] assert dense_layer.get_losses_for(a) == [0]
assert dense_layer.get_losses_for(None) == [1] assert dense_layer.get_losses_for(None) == [1]

@ -15,7 +15,7 @@ class TestImage:
gray_images = [] gray_images = []
for n in range(8): for n in range(8):
bias = np.random.rand(img_w, img_h, 1) * 64 bias = np.random.rand(img_w, img_h, 1) * 64
variance = np.random.rand(img_w, img_h, 1) * (255-64) variance = np.random.rand(img_w, img_h, 1) * (255 - 64)
imarray = np.random.rand(img_w, img_h, 3) * variance + bias imarray = np.random.rand(img_w, img_h, 3) * variance + bias
im = Image.fromarray(imarray.astype('uint8')).convert('RGB') im = Image.fromarray(imarray.astype('uint8')).convert('RGB')
rgb_images.append(im) rgb_images.append(im)

@ -96,6 +96,7 @@ def test_ModelCheckpoint():
os.remove(filepath.format(epoch=1)) os.remove(filepath.format(epoch=1))
os.remove(filepath.format(epoch=3)) os.remove(filepath.format(epoch=3))
def test_EarlyStopping(): def test_EarlyStopping():
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=train_samples, (X_train, y_train), (X_test, y_test) = get_test_data(nb_train=train_samples,
nb_test=test_samples, nb_test=test_samples,

@ -86,10 +86,10 @@ def test_identity(tensor_shape):
if len(tensor_shape) > 2: if len(tensor_shape) > 2:
with pytest.raises(Exception): with pytest.raises(Exception):
_runner(initializations.identity, tensor_shape, _runner(initializations.identity, tensor_shape,
target_mean=1./SHAPE[0], target_max=1.) target_mean=1. / SHAPE[0], target_max=1.)
else: else:
_runner(initializations.identity, tensor_shape, _runner(initializations.identity, tensor_shape,
target_mean=1./SHAPE[0], target_max=1.) target_mean=1. / SHAPE[0], target_max=1.)
@pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV']) @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])

@ -105,13 +105,13 @@ def test_top_k_categorical_accuracy():
y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]])) y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]]))
y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]])) y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]]))
success_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, success_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred,
k=3)) k=3))
assert success_result == 1 assert success_result == 1
partial_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, partial_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred,
k=2)) k=2))
assert partial_result == 0.5 assert partial_result == 0.5
failure_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, failure_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred,
k=1)) k=1))
assert failure_result == 0 assert failure_result == 0

@ -49,6 +49,7 @@ def test_clasify_build_fn():
def test_clasify_class_build_fn(): def test_clasify_class_build_fn():
class ClassBuildFnClf(object): class ClassBuildFnClf(object):
def __call__(self, hidden_dims): def __call__(self, hidden_dims):
return build_fn_clf(hidden_dims) return build_fn_clf(hidden_dims)
@ -61,6 +62,7 @@ def test_clasify_class_build_fn():
def test_clasify_inherit_class_build_fn(): def test_clasify_inherit_class_build_fn():
class InheritClassBuildFnClf(KerasClassifier): class InheritClassBuildFnClf(KerasClassifier):
def __call__(self, hidden_dims): def __call__(self, hidden_dims):
return build_fn_clf(hidden_dims) return build_fn_clf(hidden_dims)
@ -110,6 +112,7 @@ def test_regression_build_fn():
def test_regression_class_build_fn(): def test_regression_class_build_fn():
class ClassBuildFnReg(object): class ClassBuildFnReg(object):
def __call__(self, hidden_dims): def __call__(self, hidden_dims):
return build_fn_reg(hidden_dims) return build_fn_reg(hidden_dims)
@ -122,6 +125,7 @@ def test_regression_class_build_fn():
def test_regression_inherit_class_build_fn(): def test_regression_inherit_class_build_fn():
class InheritClassBuildFnReg(KerasRegressor): class InheritClassBuildFnReg(KerasRegressor):
def __call__(self, hidden_dims): def __call__(self, hidden_dims):
return build_fn_reg(hidden_dims) return build_fn_reg(hidden_dims)