Even faster tests

This commit is contained in:
Francois Chollet 2016-07-19 11:57:57 -07:00
parent 0a108b3fb2
commit 7a56925176
6 changed files with 44 additions and 46 deletions

@ -2,13 +2,14 @@ from __future__ import print_function
import numpy as np import numpy as np
import pytest import pytest
from keras.utils.test_utils import get_test_data from keras.utils.test_utils import get_test_data, keras_test
from keras.models import Sequential from keras.models import Sequential
from keras.layers.core import Dense, Flatten, Activation from keras.layers.core import Dense, Flatten, Activation
from keras.layers.convolutional import Convolution2D, MaxPooling2D from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils.np_utils import to_categorical from keras.utils.np_utils import to_categorical
@keras_test
def test_image_classification(): def test_image_classification():
''' '''
Classify random 16x16 color images into several classes using logistic regression Classify random 16x16 color images into several classes using logistic regression

@ -3,7 +3,7 @@ import numpy as np
import pytest import pytest
import string import string
from keras.utils.test_utils import get_test_data from keras.utils.test_utils import get_test_data, keras_test
from keras.utils.np_utils import to_categorical from keras.utils.np_utils import to_categorical
from keras.models import Sequential from keras.models import Sequential
from keras.layers import TimeDistributedDense from keras.layers import TimeDistributedDense
@ -14,6 +14,7 @@ from keras.layers import LSTM
from keras.layers import Embedding from keras.layers import Embedding
@keras_test
def test_temporal_classification(): def test_temporal_classification():
''' '''
Classify temporal sequences of float numbers Classify temporal sequences of float numbers
@ -43,6 +44,7 @@ def test_temporal_classification():
assert(history.history['val_acc'][-1] >= 0.85) assert(history.history['val_acc'][-1] >= 0.85)
@keras_test
def test_temporal_regression(): def test_temporal_regression():
''' '''
Predict float numbers (regression) based on sequences Predict float numbers (regression) based on sequences
@ -63,6 +65,7 @@ def test_temporal_regression():
assert(history.history['val_loss'][-1] < 0.75) assert(history.history['val_loss'][-1] < 0.75)
@keras_test
def test_sequence_to_sequence(): def test_sequence_to_sequence():
''' '''
Apply a same Dense layer for each element of time dimension of the input Apply a same Dense layer for each element of time dimension of the input
@ -86,6 +89,7 @@ def test_sequence_to_sequence():
assert(history.history['val_loss'][-1] < 0.8) assert(history.history['val_loss'][-1] < 0.8)
@keras_test
def test_stacked_lstm_char_prediction(): def test_stacked_lstm_char_prediction():
''' '''
Learn alphabetical char sequence with stacked LSTM. Learn alphabetical char sequence with stacked LSTM.
@ -135,6 +139,7 @@ def test_stacked_lstm_char_prediction():
assert(generated == alphabet) assert(generated == alphabet)
@keras_test
def test_masked_temporal(): def test_masked_temporal():
''' '''
Confirm that even with masking on both inputs and outputs, cross-entropies are Confirm that even with masking on both inputs and outputs, cross-entropies are
@ -182,5 +187,4 @@ def test_masked_temporal():
assert(np.abs(history.history['val_loss'][-1] - ground_truth) < 0.06) assert(np.abs(history.history['val_loss'][-1] - ground_truth) < 0.06)
if __name__ == '__main__': if __name__ == '__main__':
# pytest.main([__file__]) pytest.main([__file__])
test_temporal_classification()

@ -2,12 +2,13 @@ from __future__ import print_function
import numpy as np import numpy as np
import pytest import pytest
from keras.utils.test_utils import get_test_data from keras.utils.test_utils import get_test_data, keras_test
from keras.models import Sequential from keras.models import Sequential
from keras.layers.core import Dense from keras.layers.core import Dense
from keras.utils.np_utils import to_categorical from keras.utils.np_utils import to_categorical
@keras_test
def test_vector_classification(): def test_vector_classification():
''' '''
Classify random float vectors into 2 classes with logistic regression Classify random float vectors into 2 classes with logistic regression
@ -37,6 +38,7 @@ def test_vector_classification():
assert(history.history['val_acc'][-1] > 0.8) assert(history.history['val_acc'][-1] > 0.8)
@keras_test
def test_vector_regression(): def test_vector_regression():
''' '''
Perform float data prediction (regression) using 2 layer MLP Perform float data prediction (regression) using 2 layer MLP

@ -11,9 +11,9 @@ from keras.layers import convolutional
def test_convolution_1d(): def test_convolution_1d():
nb_samples = 2 nb_samples = 2
nb_steps = 8 nb_steps = 8
input_dim = 5 input_dim = 2
filter_length = 3 filter_length = 3
nb_filter = 4 nb_filter = 3
for border_mode in ['valid', 'same']: for border_mode in ['valid', 'same']:
for subsample_length in [1]: for subsample_length in [1]:
@ -58,8 +58,8 @@ def test_averagepooling_1d():
@keras_test @keras_test
def test_convolution_2d(): def test_convolution_2d():
nb_samples = 2 nb_samples = 2
nb_filter = 3 nb_filter = 2
stack_size = 4 stack_size = 3
nb_row = 10 nb_row = 10
nb_col = 6 nb_col = 6
@ -91,8 +91,8 @@ def test_convolution_2d():
@keras_test @keras_test
def test_atrous_conv_2d(): def test_atrous_conv_2d():
nb_samples = 2 nb_samples = 2
nb_filter = 3 nb_filter = 2
stack_size = 4 stack_size = 3
nb_row = 10 nb_row = 10
nb_col = 6 nb_col = 6
@ -130,8 +130,8 @@ def test_atrous_conv_2d():
@keras_test @keras_test
def test_separable_conv_2d(): def test_separable_conv_2d():
nb_samples = 2 nb_samples = 2
nb_filter = 8 nb_filter = 6
stack_size = 4 stack_size = 3
nb_row = 10 nb_row = 10
nb_col = 6 nb_col = 6
@ -195,8 +195,8 @@ def test_averagepooling_2d():
@keras_test @keras_test
def test_convolution_3d(): def test_convolution_3d():
nb_samples = 2 nb_samples = 2
nb_filter = 5 nb_filter = 2
stack_size = 4 stack_size = 3
kernel_dim1 = 2 kernel_dim1 = 2
kernel_dim2 = 3 kernel_dim2 = 3
kernel_dim3 = 1 kernel_dim3 = 1
@ -261,7 +261,7 @@ def test_averagepooling_3d():
@keras_test @keras_test
def test_zero_padding_2d(): def test_zero_padding_2d():
nb_samples = 2 nb_samples = 2
stack_size = 7 stack_size = 2
input_nb_row = 11 input_nb_row = 11
input_nb_col = 12 input_nb_col = 12
@ -287,7 +287,7 @@ def test_zero_padding_2d():
@pytest.mark.skipif(K._BACKEND != 'theano', reason="Requires Theano backend") @pytest.mark.skipif(K._BACKEND != 'theano', reason="Requires Theano backend")
def test_zero_padding_3d(): def test_zero_padding_3d():
nb_samples = 2 nb_samples = 2
stack_size = 7 stack_size = 2
input_len_dim1 = 10 input_len_dim1 = 10
input_len_dim2 = 11 input_len_dim2 = 11
input_len_dim3 = 12 input_len_dim3 = 12
@ -322,7 +322,7 @@ def test_upsampling_1d():
@keras_test @keras_test
def test_upsampling_2d(): def test_upsampling_2d():
nb_samples = 2 nb_samples = 2
stack_size = 7 stack_size = 2
input_nb_row = 11 input_nb_row = 11
input_nb_col = 12 input_nb_col = 12
@ -363,7 +363,7 @@ def test_upsampling_2d():
@pytest.mark.skipif(K._BACKEND != 'theano', reason="Requires Theano backend") @pytest.mark.skipif(K._BACKEND != 'theano', reason="Requires Theano backend")
def test_upsampling_3d(): def test_upsampling_3d():
nb_samples = 2 nb_samples = 2
stack_size = 7 stack_size = 2
input_len_dim1 = 10 input_len_dim1 = 10
input_len_dim2 = 11 input_len_dim2 = 11
input_len_dim3 = 12 input_len_dim3 = 12

@ -11,7 +11,7 @@ from keras.utils.test_utils import keras_test
from keras import backend as K from keras import backend as K
nb_samples, timesteps, embedding_dim, output_dim = 3, 5, 10, 5 nb_samples, timesteps, embedding_dim, output_dim = 2, 5, 4, 3
embedding_num = 12 embedding_num = 12
@ -24,21 +24,21 @@ def _runner(layer_class):
layer_test(layer_class, layer_test(layer_class,
kwargs={'output_dim': output_dim, kwargs={'output_dim': output_dim,
'return_sequences': True}, 'return_sequences': True},
input_shape=(3, 2, 3)) input_shape=(nb_samples, timesteps, embedding_dim))
# check dropout # check dropout
layer_test(layer_class, layer_test(layer_class,
kwargs={'output_dim': output_dim, kwargs={'output_dim': output_dim,
'dropout_U': 0.1, 'dropout_U': 0.1,
'dropout_W': 0.1}, 'dropout_W': 0.1},
input_shape=(3, 2, 3)) input_shape=(nb_samples, timesteps, embedding_dim))
# check implementation modes # check implementation modes
for mode in ['cpu', 'mem', 'gpu']: for mode in ['cpu', 'mem', 'gpu']:
layer_test(layer_class, layer_test(layer_class,
kwargs={'output_dim': output_dim, kwargs={'output_dim': output_dim,
'consume_less': mode}, 'consume_less': mode},
input_shape=(3, 2, 3)) input_shape=(nb_samples, timesteps, embedding_dim))
# check statefulness # check statefulness
model = Sequential() model = Sequential()
@ -83,7 +83,6 @@ def _runner(layer_class):
left_padded_input = np.ones((nb_samples, timesteps)) left_padded_input = np.ones((nb_samples, timesteps))
left_padded_input[0, :1] = 0 left_padded_input[0, :1] = 0
left_padded_input[1, :2] = 0 left_padded_input[1, :2] = 0
left_padded_input[2, :3] = 0
out6 = model.predict(left_padded_input) out6 = model.predict(left_padded_input)
layer.reset_states() layer.reset_states()
@ -91,7 +90,6 @@ def _runner(layer_class):
right_padded_input = np.ones((nb_samples, timesteps)) right_padded_input = np.ones((nb_samples, timesteps))
right_padded_input[0, -1:] = 0 right_padded_input[0, -1:] = 0
right_padded_input[1, -2:] = 0 right_padded_input[1, -2:] = 0
right_padded_input[2, -3:] = 0
out7 = model.predict(right_padded_input) out7 = model.predict(right_padded_input)
assert_allclose(out7, out6, atol=1e-5) assert_allclose(out7, out6, atol=1e-5)

@ -2,14 +2,16 @@ from __future__ import print_function
import pytest import pytest
import numpy as np import numpy as np
from keras.models import Sequential from keras.models import Sequential
from keras.layers.core import Dense, Activation from keras.layers.core import Dense
from keras.utils.test_utils import keras_test
@keras_test
def test_multiprocessing_training(): def test_multiprocessing_training():
reached_end = False reached_end = False
arr_data = np.random.randint(0,256, (500, 200)) arr_data = np.random.randint(0, 256, (500, 2))
arr_labels = np.random.randint(0, 2, 500) arr_labels = np.random.randint(0, 2, 500)
def myGenerator(): def myGenerator():
@ -27,10 +29,7 @@ def test_multiprocessing_training():
# Build a NN # Build a NN
model = Sequential() model = Sequential()
model.add(Dense(10, input_shape=(200, ))) model.add(Dense(1, input_shape=(2, )))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss='mse', optimizer='adadelta') model.compile(loss='mse', optimizer='adadelta')
model.fit_generator(myGenerator(), model.fit_generator(myGenerator(),
@ -53,11 +52,12 @@ def test_multiprocessing_training():
assert reached_end assert reached_end
@keras_test
def test_multiprocessing_training_fromfile(): def test_multiprocessing_training_fromfile():
reached_end = False reached_end = False
arr_data = np.random.randint(0,256, (500, 200)) arr_data = np.random.randint(0, 256, (500, 2))
arr_labels = np.random.randint(0, 2, 500) arr_labels = np.random.randint(0, 2, 500)
np.savez("data.npz", **{"data": arr_data, "labels": arr_labels}) np.savez("data.npz", **{"data": arr_data, "labels": arr_labels})
@ -78,10 +78,7 @@ def test_multiprocessing_training_fromfile():
# Build a NN # Build a NN
model = Sequential() model = Sequential()
model.add(Dense(10, input_shape=(200, ))) model.add(Dense(1, input_shape=(2, )))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss='mse', optimizer='adadelta') model.compile(loss='mse', optimizer='adadelta')
model.fit_generator(myGenerator(), model.fit_generator(myGenerator(),
@ -103,11 +100,12 @@ def test_multiprocessing_training_fromfile():
assert reached_end assert reached_end
@keras_test
def test_multiprocessing_predicting(): def test_multiprocessing_predicting():
reached_end = False reached_end = False
arr_data = np.random.randint(0,256, (500, 200)) arr_data = np.random.randint(0, 256, (500, 2))
def myGenerator(): def myGenerator():
@ -123,10 +121,7 @@ def test_multiprocessing_predicting():
# Build a NN # Build a NN
model = Sequential() model = Sequential()
model.add(Dense(10, input_shape=(200, ))) model.add(Dense(1, input_shape=(2, )))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss='mse', optimizer='adadelta') model.compile(loss='mse', optimizer='adadelta')
model.predict_generator(myGenerator(), model.predict_generator(myGenerator(),
val_samples=320, val_samples=320,
@ -142,11 +137,12 @@ def test_multiprocessing_predicting():
assert reached_end assert reached_end
@keras_test
def test_multiprocessing_evaluating(): def test_multiprocessing_evaluating():
reached_end = False reached_end = False
arr_data = np.random.randint(0,256, (500, 200)) arr_data = np.random.randint(0, 256, (500, 2))
arr_labels = np.random.randint(0, 2, 500) arr_labels = np.random.randint(0, 2, 500)
def myGenerator(): def myGenerator():
@ -164,10 +160,7 @@ def test_multiprocessing_evaluating():
# Build a NN # Build a NN
model = Sequential() model = Sequential()
model.add(Dense(10, input_shape=(200, ))) model.add(Dense(1, input_shape=(2, )))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss='mse', optimizer='adadelta') model.compile(loss='mse', optimizer='adadelta')
model.evaluate_generator(myGenerator(), model.evaluate_generator(myGenerator(),