Skip to content

Commit

Permalink
Use named constant num_classes instead of literals (keras-team#8129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozabluda authored and fchollet committed Oct 13, 2017
1 parent b9bda37 commit 16e869a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
14 changes: 8 additions & 6 deletions tests/integration_tests/test_vector_data_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import keras
from keras.utils.np_utils import to_categorical

num_classes = 2


@keras_test
def test_vector_classification():
Expand All @@ -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)

Expand All @@ -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',
Expand All @@ -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(),
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/keras/legacy/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions tests/keras/optimizers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
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)
(x_train, y_train), _ = test_utils.get_test_data(num_train=1000,
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

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/keras/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions tests/keras/test_sequential_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 16e869a

Please sign in to comment.