def test_class_weight_invalid_use_case(self):
    num_classes = 5
    train_samples = 1000
    test_samples = 1000
    input_dim = 5
    timesteps = 3
    model = keras.models.Sequential()
    model.add(keras.layers.TimeDistributed(keras.layers.Dense(num_classes), input_shape=(timesteps, input_dim)))
    model.add(keras.layers.Activation('softmax'))
    model.compile(loss='binary_crossentropy', optimizer=RMSPropOptimizer(learning_rate=0.001))
    ((x_train, y_train), _) = testing_utils.get_test_data(train_samples=train_samples, test_samples=test_samples, input_shape=(input_dim,), num_classes=num_classes)
    y_train = keras.utils.to_categorical(y_train, num_classes)
    class_weight = dict([(i, 1.0) for i in range(num_classes)])
    del class_weight[1]
    with self.assertRaises(ValueError):
        model.fit(x_train, y_train, epochs=0, verbose=0, class_weight=class_weight)
    with self.assertRaises(ValueError):
        model.compile(loss='binary_crossentropy', optimizer=RMSPropOptimizer(learning_rate=0.001), sample_weight_mode=[])
    x = keras.Input((3,))
    y1 = keras.layers.Dense(4, name='1')(x)
    y2 = keras.layers.Dense(4, name='2')(x)
    model = keras.models.Model(x, [y1, y2])
    model.compile(optimizer=RMSPropOptimizer(learning_rate=0.001), loss='mse')
    x_np = np.random.random((10, 3))
    y_np = np.random.random((10, 4))
    w_np = np.random.random((10,))
    model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': w_np})
    with self.assertRaises(ValueError):
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight=[w_np])
    with self.assertRaises(TypeError):
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight=w_np)
    with self.assertRaises(ValueError):
        bad_w_np = np.random.random((11,))
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': bad_w_np})
    with self.assertRaises(ValueError):
        bad_w_np = np.random.random((10, 2))
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': bad_w_np})
    with self.assertRaises(ValueError):
        bad_w_np = np.random.random((10, 2, 2))
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': bad_w_np})
def test_class_weight_invalid_use_case(self):
    num_classes = 5
    train_samples = 1000
    test_samples = 1000
    input_dim = 5
    timesteps = 3
    model = keras.models.Sequential()
    model.add(keras.layers.TimeDistributed(keras.layers.Dense(num_classes), input_shape=(timesteps, input_dim)))
    model.add(keras.layers.Activation('softmax'))
    model.compile(loss='binary_crossentropy', optimizer=RMSPropOptimizer(learning_rate=0.001))
    ((x_train, y_train), _) = testing_utils.get_test_data(train_samples=train_samples, test_samples=test_samples, input_shape=(input_dim,), num_classes=num_classes)
    y_train = keras.utils.to_categorical(y_train, num_classes)
    class_weight = dict([(i, 1.0) for i in range(num_classes)])
    del class_weight[1]
    with self.assertRaises(ValueError):
        model.fit(x_train, y_train, epochs=0, verbose=0, class_weight=class_weight)
    with self.assertRaises(ValueError):
        model.compile(loss='binary_crossentropy', optimizer=RMSPropOptimizer(learning_rate=0.001), sample_weight_mode=[])
    x = keras.Input((3,))
    y1 = keras.layers.Dense(4, name='1')(x)
    y2 = keras.layers.Dense(4, name='2')(x)
    model = keras.models.Model(x, [y1, y2])
    model.compile(optimizer=RMSPropOptimizer(learning_rate=0.001), loss='mse')
    x_np = np.random.random((10, 3))
    y_np = np.random.random((10, 4))
    w_np = np.random.random((10,))
    model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': w_np})
    with self.assertRaises(ValueError):
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight=[w_np])
    with self.assertRaises(TypeError):
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight=w_np)
    with self.assertRaises(ValueError):
        bad_w_np = np.random.random((11,))
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': bad_w_np})
    with self.assertRaises(ValueError):
        bad_w_np = np.random.random((10, 2))
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': bad_w_np})
    with self.assertRaises(ValueError):
        bad_w_np = np.random.random((10, 2, 2))
        model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': bad_w_np})