def test_class_weights(self):
    num_classes = 5
    batch_size = 5
    weighted_class = 3
    train_samples = 300
    test_samples = 300
    input_dim = 5
    model = keras.models.Sequential()
    model.add(keras.layers.Dense(10, input_shape=(input_dim,)))
    model.add(keras.layers.Activation('relu'))
    model.add(keras.layers.Dense(num_classes))
    model.add(keras.layers.Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer=RMSPropOptimizer(learning_rate=0.001))
    np.random.seed(1337)
    ((x_train, y_train), (x_test, y_test)) = testing_utils.get_test_data(train_samples=train_samples, test_samples=test_samples, input_shape=(input_dim,), num_classes=num_classes)
    int_y_test = y_test.copy()
    int_y_train = y_train.copy()
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)
    test_ids = np.where((int_y_test == np.array(weighted_class)))[0]
    class_weight = dict([(i, 1.0) for i in range(num_classes)])
    class_weight[weighted_class] = 4.0
    sample_weight = np.ones(y_train.shape[0])
    sample_weight[(int_y_train == weighted_class)] = 4.0
    model.fit(x_train, y_train, batch_size=batch_size, epochs=2, verbose=0, class_weight=class_weight, validation_data=(x_train, y_train, sample_weight))
    model.fit(x_train, y_train, batch_size=batch_size, epochs=2, verbose=0, class_weight=class_weight)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=2, verbose=0, class_weight=class_weight, validation_split=0.1)
    model.train_on_batch(x_train[:batch_size], y_train[:batch_size], class_weight=class_weight)
    ref_score = model.evaluate(x_test, y_test, verbose=0)
    score = model.evaluate(x_test[test_ids, :], y_test[test_ids, :], verbose=0)
    self.assertLess(score, ref_score)
def test_class_weights(self):
    num_classes = 5
    batch_size = 5
    weighted_class = 3
    train_samples = 300
    test_samples = 300
    input_dim = 5
    model = keras.models.Sequential()
    model.add(keras.layers.Dense(10, input_shape=(input_dim,)))
    model.add(keras.layers.Activation('relu'))
    model.add(keras.layers.Dense(num_classes))
    model.add(keras.layers.Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer=RMSPropOptimizer(learning_rate=0.001))
    np.random.seed(1337)
    ((x_train, y_train), (x_test, y_test)) = testing_utils.get_test_data(train_samples=train_samples, test_samples=test_samples, input_shape=(input_dim,), num_classes=num_classes)
    int_y_test = y_test.copy()
    int_y_train = y_train.copy()
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)
    test_ids = np.where((int_y_test == np.array(weighted_class)))[0]
    class_weight = dict([(i, 1.0) for i in range(num_classes)])
    class_weight[weighted_class] = 4.0
    sample_weight = np.ones(y_train.shape[0])
    sample_weight[(int_y_train == weighted_class)] = 4.0
    model.fit(x_train, y_train, batch_size=batch_size, epochs=2, verbose=0, class_weight=class_weight, validation_data=(x_train, y_train, sample_weight))
    model.fit(x_train, y_train, batch_size=batch_size, epochs=2, verbose=0, class_weight=class_weight)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=2, verbose=0, class_weight=class_weight, validation_split=0.1)
    model.train_on_batch(x_train[:batch_size], y_train[:batch_size], class_weight=class_weight)
    ref_score = model.evaluate(x_test, y_test, verbose=0)
    score = model.evaluate(x_test[test_ids, :], y_test[test_ids, :], verbose=0)
    self.assertLess(score, ref_score)