diff --git a/tests/integration_tests/test_vector_data_tasks.py b/tests/integration_tests/test_vector_data_tasks.py index 3ec153d3665..4e4cda88292 100644 --- a/tests/integration_tests/test_vector_data_tasks.py +++ b/tests/integration_tests/test_vector_data_tasks.py @@ -7,6 +7,8 @@ import keras from keras.utils.np_utils import to_categorical +num_classes = 2 + @keras_test def test_vector_classification(): @@ -18,7 +20,7 @@ def test_vector_classification(): num_test=200, input_shape=(20,), classification=True, - num_classes=2) + num_classes=num_classes) y_train = to_categorical(y_train) y_test = to_categorical(y_test) @@ -27,7 +29,7 @@ def test_vector_classification(): layers.Dense(16, input_shape=(x_train.shape[-1],), activation='relu'), layers.Dense(8), layers.Activation('relu'), - layers.Dense(y_train.shape[-1], activation='softmax') + layers.Dense(num_classes, activation='softmax') ]) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', @@ -47,13 +49,13 @@ def test_vector_classification_functional(): num_test=200, input_shape=(20,), classification=True, - num_classes=2) + num_classes=num_classes) # Test with functional API inputs = layers.Input(shape=(x_train.shape[-1],)) x = layers.Dense(16, activation=keras.activations.relu)(inputs) x = layers.Dense(8)(x) x = layers.Activation('relu')(x) - outputs = layers.Dense(y_train.shape[-1], activation='softmax')(x) + outputs = layers.Dense(num_classes, activation='softmax')(x) model = keras.models.Model(inputs, outputs) model.compile(loss=keras.losses.sparse_categorical_crossentropy, optimizer=keras.optimizers.RMSprop(), @@ -73,12 +75,12 @@ def test_vector_regression(): (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500, num_test=200, input_shape=(20,), - output_shape=(2,), + output_shape=(num_classes,), classification=False) model = Sequential([ layers.Dense(16, input_shape=(x_train.shape[-1],), activation='tanh'), - layers.Dense(y_train.shape[-1]) + layers.Dense(num_classes) ]) model.compile(loss='hinge', optimizer='adagrad') diff --git a/tests/keras/legacy/models_test.py b/tests/keras/legacy/models_test.py index 8672fcaf54b..d0c23e18134 100644 --- a/tests/keras/legacy/models_test.py +++ b/tests/keras/legacy/models_test.py @@ -40,7 +40,7 @@ def _get_test_data(): num_test=test_samples, input_shape=(input_dim,), classification=True, - num_classes=4) + num_classes=num_classes) y_test = np_utils.to_categorical(y_test) y_train = np_utils.to_categorical(y_train) return (x_train, y_train), (x_test, y_test) diff --git a/tests/keras/optimizers_test.py b/tests/keras/optimizers_test.py index 7af1d072d95..70ddc31daf2 100644 --- a/tests/keras/optimizers_test.py +++ b/tests/keras/optimizers_test.py @@ -11,6 +11,8 @@ from keras.utils.np_utils import to_categorical from keras import backend as K +num_classes = 2 + def get_test_data(): np.random.seed(1337) @@ -18,7 +20,7 @@ def get_test_data(): num_test=200, input_shape=(10,), classification=True, - num_classes=2) + num_classes=num_classes) y_train = to_categorical(y_train) return x_train, y_train @@ -123,9 +125,9 @@ def test_tfoptimizer(): from tensorflow import train optimizer = optimizers.TFOptimizer(train.AdamOptimizer()) model = Sequential() - model.add(Dense(2, input_shape=(3,), kernel_constraint=constraints.MaxNorm(1))) + model.add(Dense(num_classes, input_shape=(3,), kernel_constraint=constraints.MaxNorm(1))) model.compile(loss='mean_squared_error', optimizer=optimizer) - model.fit(np.random.random((5, 3)), np.random.random((5, 2)), + model.fit(np.random.random((5, 3)), np.random.random((5, num_classes)), epochs=1, batch_size=5, verbose=0) # not supported with pytest.raises(NotImplementedError): diff --git a/tests/keras/test_callbacks.py b/tests/keras/test_callbacks.py index 2f34ed6110c..51cd9ff8050 100644 --- a/tests/keras/test_callbacks.py +++ b/tests/keras/test_callbacks.py @@ -634,7 +634,7 @@ def test_TensorBoard_convnet(tmpdir): num_test=200, input_shape=input_shape, classification=True, - num_classes=4) + num_classes=num_classes) y_train = np_utils.to_categorical(y_train) y_test = np_utils.to_categorical(y_test) @@ -646,7 +646,7 @@ def test_TensorBoard_convnet(tmpdir): Conv2D(filters=4, kernel_size=(3, 3), activation='relu', padding='same'), GlobalAveragePooling2D(), - Dense(y_test.shape[-1], activation='softmax') + Dense(num_classes, activation='softmax') ]) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', diff --git a/tests/keras/test_sequential_model.py b/tests/keras/test_sequential_model.py index afc42878c96..26861ae8887 100644 --- a/tests/keras/test_sequential_model.py +++ b/tests/keras/test_sequential_model.py @@ -60,7 +60,7 @@ def _get_test_data(): num_test=test_samples, input_shape=(input_dim,), classification=True, - num_classes=4) + num_classes=num_classes) y_test = np_utils.to_categorical(y_test) y_train = np_utils.to_categorical(y_train) return (x_train, y_train), (x_test, y_test) @@ -276,8 +276,8 @@ def test_clone_functional_model(): input_a = keras.Input(shape=(4,)) input_b = keras.Input(shape=(4,)) - dense_1 = keras.layers.Dense(4,) - dense_2 = keras.layers.Dense(4,) + dense_1 = keras.layers.Dense(4) + dense_2 = keras.layers.Dense(4) x_a = dense_1(input_a) x_a = keras.layers.Dropout(0.5)(x_a)