Пример #1
0
  def test_video_classification_functional(self):
    with self.test_session():
      np.random.seed(1337)
      (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
          train_samples=200,
          test_samples=100,
          input_shape=(4, 8, 8, 3),
          num_classes=3)
      y_train = keras.utils.to_categorical(y_train)
      y_test = keras.utils.to_categorical(y_test)

      inputs = keras.layers.Input(shape=x_train.shape[1:])
      x = keras.layers.TimeDistributed(
          keras.layers.Conv2D(4, 3, activation='relu'))(inputs)
      x = keras.layers.BatchNormalization()(x)
      x = keras.layers.TimeDistributed(keras.layers.GlobalMaxPooling2D())(x)
      x = keras.layers.Conv1D(8, 3, activation='relu')(x)
      x = keras.layers.Flatten()(x)
      outputs = keras.layers.Dense(y_train.shape[-1], activation='softmax')(x)

      model = keras.models.Model(inputs, outputs)
      model.compile(loss='categorical_crossentropy',
                    optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.8),
                    metrics=['accuracy'])
      history = model.fit(x_train, y_train, epochs=10, batch_size=16,
                          validation_data=(x_test, y_test),
                          verbose=2)
      self.assertGreater(history.history['val_acc'][-1], 0.70)
Пример #2
0
    def test_TerminateOnNaN(self):
        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)

        y_test = keras.utils.to_categorical(y_test)
        y_train = keras.utils.to_categorical(y_train)
        cbks = [keras.callbacks.TerminateOnNaN()]
        model = keras.models.Sequential()
        initializer = keras.initializers.Constant(value=1e5)
        for _ in range(5):
            model.add(
                keras.layers.Dense(2,
                                   input_dim=INPUT_DIM,
                                   activation='relu',
                                   kernel_initializer=initializer))
        model.add(keras.layers.Dense(NUM_CLASSES))
        model.compile(loss='mean_squared_error', optimizer='rmsprop')

        history = model.fit(x_train,
                            y_train,
                            batch_size=BATCH_SIZE,
                            validation_data=(x_test, y_test),
                            callbacks=cbks,
                            epochs=20)
        loss = history.history['loss']
        assert len(loss) == 1
        assert loss[0] == np.inf
Пример #3
0
    def test_invalid_loss_or_metrics(self):
        num_classes = 5
        train_samples = 1000
        test_samples = 1000
        input_dim = 5

        with self.test_session():
            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='rmsprop')
            np.random.seed(1337)
            (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)
            with self.assertRaises(ValueError):
                model.fit(x_train, y_train)

            with self.assertRaises(ValueError):
                model.fit(x_train, np.concatenate([y_train, y_train], axis=-1))

            with self.assertRaises(TypeError):
                model.compile(loss='categorical_crossentropy',
                              optimizer='rmsprop',
                              metrics=set(0))

            with self.assertRaises(RuntimeError):
                model.compile(loss=None, optimizer='rmsprop')
Пример #4
0
  def test_vector_classification_shared_model(self):
    # Test that functional models that feature internal updates
    # and internal losses can be shared.
    with self.test_session():
      np.random.seed(1337)
      (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
          train_samples=200,
          test_samples=100,
          input_shape=(10,),
          num_classes=2)
      y_train = keras.utils.to_categorical(y_train)
      y_test = keras.utils.to_categorical(y_test)

      inputs = keras.layers.Input(x_train.shape[1:])
      x = keras.layers.Dense(16,
                             activation='relu',
                             kernel_regularizer=keras.regularizers.l2(1e-5),
                             bias_regularizer=keras.regularizers.l2(1e-5),
                             input_shape=x_train.shape[1:])(inputs)
      x = keras.layers.BatchNormalization()(x)
      base_model = keras.models.Model(inputs, x)

      x = keras.layers.Input(x_train.shape[1:])
      y = base_model(x)
      y = keras.layers.Dense(y_train.shape[-1], activation='softmax')(y)
      model = keras.models.Model(x, y)
      model.compile(loss='categorical_crossentropy',
                    optimizer='rmsprop',
                    metrics=['accuracy'])
      history = model.fit(x_train, y_train, epochs=10, batch_size=16,
                          validation_data=(x_test, y_test),
                          verbose=2)
      self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #5
0
  def test_vector_classification_declarative(self):
    with self.test_session():
      np.random.seed(1337)
      (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
          train_samples=200,
          test_samples=100,
          input_shape=(10,),
          num_classes=2)
      y_train = keras.utils.to_categorical(y_train)
      y_test = keras.utils.to_categorical(y_test)

      model = keras.models.Sequential([
          keras.layers.Dense(16,
                             activation='relu',
                             input_shape=x_train.shape[1:]),
          keras.layers.Dropout(0.1),
          keras.layers.Dense(y_train.shape[-1], activation='softmax')
      ])
      model.compile(loss='categorical_crossentropy',
                    optimizer='rmsprop',
                    metrics=['accuracy'])
      history = model.fit(x_train, y_train, epochs=10, batch_size=16,
                          validation_data=(x_test, y_test),
                          verbose=2)
      self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #6
0
  def test_image_classification_declarative(self):
    with self.test_session():
      np.random.seed(1337)
      (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
          train_samples=200,
          test_samples=100,
          input_shape=(8, 8, 3),
          num_classes=2)
      y_train = keras.utils.to_categorical(y_train)
      y_test = keras.utils.to_categorical(y_test)

      model = keras.models.Sequential()
      model.add(keras.layers.Conv2D(
          8, 3,
          activation='relu',
          input_shape=x_train.shape[1:]))
      model.add(keras.layers.BatchNormalization())
      model.add(keras.layers.Conv2D(
          8, 3,
          padding='same',
          activation='relu'))
      model.add(keras.layers.GlobalMaxPooling2D())
      model.add(keras.layers.Dense(y_train.shape[-1], activation='softmax'))
      model.compile(loss='categorical_crossentropy',
                    optimizer='adam',
                    metrics=['accuracy'])
      history = model.fit(x_train, y_train, epochs=10, batch_size=16,
                          validation_data=(x_test, y_test),
                          verbose=2)
      self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #7
0
    def test_vector_classification_declarative(self):
        with self.test_session():
            np.random.seed(1337)
            (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
                train_samples=200,
                test_samples=100,
                input_shape=(8, ),
                num_classes=2)
            y_train = keras.utils.to_categorical(y_train)
            y_test = keras.utils.to_categorical(y_test)

            model = keras.models.Sequential([
                keras.layers.Dense(8,
                                   activation='relu',
                                   input_shape=x_train.shape[1:]),
                keras.layers.Dropout(0.1),
                keras.layers.Dense(y_train.shape[-1], activation='softmax')
            ])
            model.compile(loss='categorical_crossentropy',
                          optimizer='rmsprop',
                          metrics=['accuracy'])
            history = model.fit(x_train,
                                y_train,
                                epochs=10,
                                batch_size=16,
                                validation_data=(x_test, y_test),
                                verbose=2)
            self.assertTrue(history.history['val_acc'][-1] > 0.85)
Пример #8
0
    def test_vector_classification_functional(self):
        with self.test_session():
            np.random.seed(1337)
            (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
                train_samples=200,
                test_samples=100,
                input_shape=(10, ),
                num_classes=2)
            y_train = keras.utils.to_categorical(y_train)
            y_test = keras.utils.to_categorical(y_test)

            inputs = keras.layers.Input(shape=x_train.shape[1:])
            x = keras.layers.Dense(16, activation='relu')(inputs)
            x = keras.layers.Dropout(0.1)(x)
            outputs = keras.layers.Dense(y_train.shape[-1],
                                         activation='softmax')(x)

            model = keras.models.Model(inputs, outputs)
            model.compile(loss='categorical_crossentropy',
                          optimizer='rmsprop',
                          metrics=['accuracy'])
            history = model.fit(x_train,
                                y_train,
                                epochs=10,
                                batch_size=16,
                                validation_data=(x_test, y_test),
                                verbose=2)
            self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #9
0
    def test_LearningRateScheduler(self):
        with self.test_session():
            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)
            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)
            model = keras.models.Sequential()
            model.add(
                keras.layers.Dense(NUM_HIDDEN,
                                   input_dim=INPUT_DIM,
                                   activation='relu'))
            model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
            model.compile(loss='categorical_crossentropy',
                          optimizer='sgd',
                          metrics=['accuracy'])

            cbks = [
                keras.callbacks.LearningRateScheduler(lambda x: 1. / (1. + x))
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=5,
                      verbose=0)
            assert (float(keras.backend.get_value(model.optimizer.lr)) -
                    0.2) < keras.backend.epsilon()
Пример #10
0
def _test_optimizer(optimizer, target=0.75):
  np.random.seed(1337)
  (x_train, y_train), _ = testing_utils.get_test_data(train_samples=1000,
                                                      test_samples=200,
                                                      input_shape=(10,),
                                                      num_classes=2)
  y_train = keras.utils.to_categorical(y_train)
  model = _get_model(x_train.shape[1], 20, y_train.shape[1])
  model.compile(loss='categorical_crossentropy',
                optimizer=optimizer,
                metrics=['accuracy'])
  history = model.fit(x_train, y_train, epochs=2, batch_size=16, verbose=0)
  assert history.history['acc'][-1] >= target
  config = keras.optimizers.serialize(optimizer)
  optim = keras.optimizers.deserialize(config)
  new_config = keras.optimizers.serialize(optim)
  new_config['class_name'] = new_config['class_name'].lower()
  assert config == new_config

  # Test constraints.
  model = keras.models.Sequential()
  dense = keras.layers.Dense(10,
                             input_shape=(x_train.shape[1],),
                             kernel_constraint=lambda x: 0. * x + 1.,
                             bias_constraint=lambda x: 0. * x + 2.,
                             activation='relu')
  model.add(dense)
  model.add(keras.layers.Dense(y_train.shape[1], activation='softmax'))
  model.compile(loss='categorical_crossentropy',
                optimizer=optimizer,
                metrics=['accuracy'])
  model.train_on_batch(x_train[:10], y_train[:10])
  kernel, bias = dense.get_weights()
  np.testing.assert_allclose(kernel, 1., atol=1e-3)
  np.testing.assert_allclose(bias, 2., atol=1e-3)
Пример #11
0
    def test_video_classification_functional(self):
        with self.test_session():
            np.random.seed(1337)
            (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
                train_samples=200,
                test_samples=100,
                input_shape=(4, 8, 8, 3),
                num_classes=3)
            y_train = keras.utils.to_categorical(y_train)
            y_test = keras.utils.to_categorical(y_test)

            inputs = keras.layers.Input(shape=x_train.shape[1:])
            x = keras.layers.TimeDistributed(
                keras.layers.Conv2D(4, 3, activation='relu'))(inputs)
            x = keras.layers.BatchNormalization()(x)
            x = keras.layers.TimeDistributed(
                keras.layers.GlobalMaxPooling2D())(x)
            x = keras.layers.Conv1D(8, 3, activation='relu')(x)
            x = keras.layers.Flatten()(x)
            outputs = keras.layers.Dense(y_train.shape[-1],
                                         activation='softmax')(x)

            model = keras.models.Model(inputs, outputs)
            model.compile(loss='categorical_crossentropy',
                          optimizer=keras.optimizers.SGD(lr=0.01,
                                                         momentum=0.8),
                          metrics=['accuracy'])
            history = model.fit(x_train,
                                y_train,
                                epochs=10,
                                batch_size=16,
                                validation_data=(x_test, y_test),
                                verbose=2)
            self.assertGreater(history.history['val_acc'][-1], 0.70)
Пример #12
0
  def test_class_weight_wrong_classes(self):
    num_classes = 5
    train_samples = 1000
    test_samples = 1000
    input_dim = 5
    timesteps = 3

    with self.test_session():
      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='rmsprop')

      (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)
      # convert class vectors to binary class matrices
      y_train = keras.utils.to_categorical(y_train, num_classes)
      class_weight = dict([(i, 1.) 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)
    def test_image_classification_declarative(self):
        with self.test_session():
            np.random.seed(1337)
            (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
                train_samples=200,
                test_samples=100,
                input_shape=(8, 8, 3),
                num_classes=2)
            y_train = keras.utils.to_categorical(y_train)
            y_test = keras.utils.to_categorical(y_test)

            model = keras.models.Sequential()
            model.add(
                keras.layers.Conv2D(8,
                                    3,
                                    activation='relu',
                                    input_shape=x_train.shape[1:]))
            model.add(keras.layers.BatchNormalization())
            model.add(
                keras.layers.Conv2D(8, 3, padding='same', activation='relu'))
            model.add(keras.layers.GlobalMaxPooling2D())
            model.add(
                keras.layers.Dense(y_train.shape[-1], activation='softmax'))
            model.compile(loss='categorical_crossentropy',
                          optimizer='adam',
                          metrics=['accuracy'])
            history = model.fit(x_train,
                                y_train,
                                epochs=10,
                                batch_size=16,
                                validation_data=(x_test, y_test),
                                verbose=2)
            self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #14
0
  def test_LearningRateScheduler(self):
    with self.test_session():
      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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)
      model = keras.models.Sequential()
      model.add(
          keras.layers.Dense(
              NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
      model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
      model.compile(
          loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

      cbks = [keras.callbacks.LearningRateScheduler(lambda x: 1. / (1. + x))]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=5,
          verbose=0)
      assert (float(keras.backend.get_value(model.optimizer.lr)) - 0.2
             ) < keras.backend.epsilon()
Пример #15
0
  def test_TerminateOnNaN(self):
    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)

    y_test = keras.utils.to_categorical(y_test)
    y_train = keras.utils.to_categorical(y_train)
    cbks = [keras.callbacks.TerminateOnNaN()]
    model = keras.models.Sequential()
    initializer = keras.initializers.Constant(value=1e5)
    for _ in range(5):
      model.add(keras.layers.Dense(2,
                                   input_dim=INPUT_DIM,
                                   activation='relu',
                                   kernel_initializer=initializer))
    model.add(keras.layers.Dense(NUM_CLASSES))
    model.compile(loss='mean_squared_error',
                  optimizer='rmsprop')

    history = model.fit(x_train, y_train, batch_size=BATCH_SIZE,
                        validation_data=(x_test, y_test),
                        callbacks=cbks, epochs=20)
    loss = history.history['loss']
    assert len(loss) == 1
    assert loss[0] == np.inf
Пример #16
0
  def test_invalid_loss_or_metrics(self):
    num_classes = 5
    train_samples = 1000
    test_samples = 1000
    input_dim = 5

    with self.test_session():
      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='rmsprop')
      np.random.seed(1337)
      (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)
      with self.assertRaises(ValueError):
        model.fit(x_train, y_train)

      with self.assertRaises(ValueError):
        model.fit(x_train, np.concatenate([y_train, y_train], axis=-1))

      with self.assertRaises(TypeError):
        model.compile(loss='categorical_crossentropy',
                      optimizer='rmsprop',
                      metrics=set(0))

      with self.assertRaises(RuntimeError):
        model.compile(loss=None,
                      optimizer='rmsprop')
Пример #17
0
    def test_temporal_classification_declarative(self):
        with self.test_session():
            np.random.seed(1336)
            (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
                train_samples=200,
                test_samples=100,
                input_shape=(4, 8),
                num_classes=2)
            y_train = keras.utils.to_categorical(y_train)
            y_test = keras.utils.to_categorical(y_test)

            model = keras.models.Sequential()
            model.add(
                keras.layers.LSTM(3,
                                  return_sequences=True,
                                  input_shape=x_train.shape[1:]))
            model.add(keras.layers.GRU(y_train.shape[-1],
                                       activation='softmax'))
            model.compile(loss='categorical_crossentropy',
                          optimizer='adam',
                          metrics=['accuracy'])
            history = model.fit(x_train,
                                y_train,
                                epochs=10,
                                batch_size=16,
                                validation_data=(x_test, y_test),
                                verbose=2)
            self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #18
0
    def test_stop_training_csv(self):
        # Test that using the CSVLogger callback with the TerminateOnNaN callback
        # does not result in invalid CSVs.
        np.random.seed(1337)
        tmpdir = self.get_temp_dir()
        self.addCleanup(shutil.rmtree, tmpdir)

        with self.test_session():
            fp = os.path.join(tmpdir, 'test.csv')
            (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)

            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)
            cbks = [
                keras.callbacks.TerminateOnNaN(),
                keras.callbacks.CSVLogger(fp)
            ]
            model = keras.models.Sequential()
            for _ in range(5):
                model.add(
                    keras.layers.Dense(2,
                                       input_dim=INPUT_DIM,
                                       activation='relu'))
            model.add(keras.layers.Dense(NUM_CLASSES, activation='linear'))
            model.compile(loss='mean_squared_error', optimizer='rmsprop')

            def data_generator():
                i = 0
                max_batch_index = len(x_train) // BATCH_SIZE
                tot = 0
                while 1:
                    if tot > 3 * len(x_train):
                        yield (np.ones([BATCH_SIZE, INPUT_DIM]) * np.nan,
                               np.ones([BATCH_SIZE, NUM_CLASSES]) * np.nan)
                    else:
                        yield (x_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE],
                               y_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE])
                    i += 1
                    tot += 1
                    i %= max_batch_index

            history = model.fit_generator(data_generator(),
                                          len(x_train) // BATCH_SIZE,
                                          validation_data=(x_test, y_test),
                                          callbacks=cbks,
                                          epochs=20)
            loss = history.history['loss']
            assert len(loss) > 1
            assert loss[-1] == np.inf or np.isnan(loss[-1])

            values = []
            with open(fp) as f:
                for x in csv.reader(f):
                    values.append(x)
            assert 'nan' in values[-1], 'The last epoch was not logged.'
Пример #19
0
def get_data():
  (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
      train_samples=10,
      test_samples=10,
      input_shape=(DATA_DIM,),
      num_classes=NUM_CLASSES)
  y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)
  y_test = keras.utils.to_categorical(y_test, NUM_CLASSES)
  return (x_train, y_train), (x_test, y_test)
Пример #20
0
  def test_stop_training_csv(self):
    # Test that using the CSVLogger callback with the TerminateOnNaN callback
    # does not result in invalid CSVs.
    np.random.seed(1337)
    tmpdir = self.get_temp_dir()
    self.addCleanup(shutil.rmtree, tmpdir)

    with self.test_session():
      fp = os.path.join(tmpdir, 'test.csv')
      (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)

      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)
      cbks = [keras.callbacks.TerminateOnNaN(), keras.callbacks.CSVLogger(fp)]
      model = keras.models.Sequential()
      for _ in range(5):
        model.add(keras.layers.Dense(2, input_dim=INPUT_DIM, activation='relu'))
      model.add(keras.layers.Dense(NUM_CLASSES, activation='linear'))
      model.compile(loss='mean_squared_error',
                    optimizer='rmsprop')

      def data_generator():
        i = 0
        max_batch_index = len(x_train) // BATCH_SIZE
        tot = 0
        while 1:
          if tot > 3 * len(x_train):
            yield (np.ones([BATCH_SIZE, INPUT_DIM]) * np.nan,
                   np.ones([BATCH_SIZE, NUM_CLASSES]) * np.nan)
          else:
            yield (x_train[i * BATCH_SIZE: (i + 1) * BATCH_SIZE],
                   y_train[i * BATCH_SIZE: (i + 1) * BATCH_SIZE])
          i += 1
          tot += 1
          i %= max_batch_index

      history = model.fit_generator(data_generator(),
                                    len(x_train) // BATCH_SIZE,
                                    validation_data=(x_test, y_test),
                                    callbacks=cbks,
                                    epochs=20)
      loss = history.history['loss']
      assert len(loss) > 1
      assert loss[-1] == np.inf or np.isnan(loss[-1])

      values = []
      with open(fp) as f:
        for x in csv.reader(f):
          values.append(x)
      assert 'nan' in values[-1], 'The last epoch was not logged.'
Пример #21
0
    def test_EarlyStopping(self):
        with self.test_session():
            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)
            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)
            model = keras.models.Sequential()
            model.add(
                keras.layers.Dense(NUM_HIDDEN,
                                   input_dim=INPUT_DIM,
                                   activation='relu'))
            model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
            model.compile(loss='categorical_crossentropy',
                          optimizer='rmsprop',
                          metrics=['accuracy'])
            mode = 'max'
            monitor = 'val_acc'
            patience = 0
            cbks = [
                keras.callbacks.EarlyStopping(patience=patience,
                                              monitor=monitor,
                                              mode=mode)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=20,
                      verbose=0)

            mode = 'auto'
            monitor = 'val_acc'
            patience = 2
            cbks = [
                keras.callbacks.EarlyStopping(patience=patience,
                                              monitor=monitor,
                                              mode=mode)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=20,
                      verbose=0)
Пример #22
0
  def test_EarlyStopping(self):
    with self.test_session():
      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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)
      model = keras.models.Sequential()
      model.add(
          keras.layers.Dense(
              NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
      model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
      model.compile(
          loss='categorical_crossentropy',
          optimizer='rmsprop',
          metrics=['accuracy'])
      mode = 'max'
      monitor = 'val_acc'
      patience = 0
      cbks = [
          keras.callbacks.EarlyStopping(
              patience=patience, monitor=monitor, mode=mode)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=20,
          verbose=0)

      mode = 'auto'
      monitor = 'val_acc'
      patience = 2
      cbks = [
          keras.callbacks.EarlyStopping(
              patience=patience, monitor=monitor, mode=mode)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=20,
          verbose=0)
Пример #23
0
def assert_regression_works(reg):
  np.random.seed(42)
  (x_train, y_train), (x_test, _) = testing_utils.get_test_data(
      train_samples=TRAIN_SAMPLES,
      test_samples=TEST_SAMPLES,
      input_shape=(INPUT_DIM,),
      num_classes=NUM_CLASSES)

  reg.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS)

  score = reg.score(x_train, y_train, batch_size=BATCH_SIZE)
  assert np.isscalar(score) and np.isfinite(score)

  preds = reg.predict(x_test, batch_size=BATCH_SIZE)
  assert preds.shape == (TEST_SAMPLES,)
Пример #24
0
def assert_regression_works(reg):
    np.random.seed(42)
    (x_train,
     y_train), (x_test,
                _) = testing_utils.get_test_data(train_samples=TRAIN_SAMPLES,
                                                 test_samples=TEST_SAMPLES,
                                                 input_shape=(INPUT_DIM, ),
                                                 num_classes=NUM_CLASSES)

    reg.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS)

    score = reg.score(x_train, y_train, batch_size=BATCH_SIZE)
    assert np.isscalar(score) and np.isfinite(score)

    preds = reg.predict(x_test, batch_size=BATCH_SIZE)
    assert preds.shape == (TEST_SAMPLES, )
Пример #25
0
  def test_ReduceLROnPlateau(self):
    with self.test_session():
      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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)

      def make_model():
        np.random.seed(1337)
        model = keras.models.Sequential()
        model.add(
            keras.layers.Dense(
                NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
        model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))

        model.compile(
            loss='categorical_crossentropy',
            optimizer=keras.optimizers.SGD(lr=0.1),
            metrics=['accuracy'])
        return model

      model = make_model()
      # This should reduce the LR after the first epoch (due to high epsilon).
      cbks = [
          keras.callbacks.ReduceLROnPlateau(
              monitor='val_loss',
              factor=0.1,
              epsilon=10,
              patience=1,
              cooldown=5)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=5,
          verbose=0)
      self.assertAllClose(
          float(keras.backend.get_value(model.optimizer.lr)),
          0.01,
          atol=1e-4)
Пример #26
0
    def test_ReduceLROnPlateau(self):
        with self.test_session():
            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)
            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)

            def make_model():
                np.random.seed(1337)
                model = keras.models.Sequential()
                model.add(
                    keras.layers.Dense(NUM_HIDDEN,
                                       input_dim=INPUT_DIM,
                                       activation='relu'))
                model.add(keras.layers.Dense(NUM_CLASSES,
                                             activation='softmax'))

                model.compile(loss='categorical_crossentropy',
                              optimizer=keras.optimizers.SGD(lr=0.1),
                              metrics=['accuracy'])
                return model

            model = make_model()
            # This should reduce the LR after the first epoch (due to high epsilon).
            cbks = [
                keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                                  factor=0.1,
                                                  epsilon=10,
                                                  patience=1,
                                                  cooldown=5)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=5,
                      verbose=0)
            self.assertAllClose(float(
                keras.backend.get_value(model.optimizer.lr)),
                                0.01,
                                atol=1e-4)
Пример #27
0
def _test_optimizer(optimizer, target=0.75):
  np.random.seed(1337)
  (x_train, y_train), _ = testing_utils.get_test_data(train_samples=1000,
                                                      test_samples=200,
                                                      input_shape=(10,),
                                                      num_classes=2)
  y_train = keras.utils.to_categorical(y_train)
  model = _get_model(x_train.shape[1], 20, y_train.shape[1])
  model.compile(loss='categorical_crossentropy',
                optimizer=optimizer,
                metrics=['accuracy'])
  history = model.fit(x_train, y_train, epochs=2, batch_size=16, verbose=0)
  assert history.history['acc'][-1] >= target
  config = keras.optimizers.serialize(optimizer)
  optim = keras.optimizers.deserialize(config)
  new_config = keras.optimizers.serialize(optim)
  new_config['class_name'] = new_config['class_name'].lower()
  assert config == new_config
Пример #28
0
def _test_optimizer(optimizer, target=0.75):
  np.random.seed(1337)
  (x_train, y_train), _ = testing_utils.get_test_data(train_samples=1000,
                                                      test_samples=200,
                                                      input_shape=(10,),
                                                      num_classes=2)
  y_train = keras.utils.to_categorical(y_train)
  model = _get_model(x_train.shape[1], 20, y_train.shape[1])
  model.compile(loss='categorical_crossentropy',
                optimizer=optimizer,
                metrics=['accuracy'])
  history = model.fit(x_train, y_train, epochs=2, batch_size=16, verbose=0)
  assert history.history['acc'][-1] >= target
  config = keras.optimizers.serialize(optimizer)
  optim = keras.optimizers.deserialize(config)
  new_config = keras.optimizers.serialize(optim)
  new_config['class_name'] = new_config['class_name'].lower()
  assert config == new_config
  def test_LambdaCallback(self):
    with self.test_session():
      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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)
      model = keras.models.Sequential()
      model.add(
          keras.layers.Dense(
              NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
      model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
      model.compile(
          loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

      # Start an arbitrary process that should run during model
      # training and be terminated after training has completed.
      def target():
        while True:
          pass

      p = multiprocessing.Process(target=target)
      p.start()
      cleanup_callback = keras.callbacks.LambdaCallback(
          on_train_end=lambda logs: p.terminate())

      cbks = [cleanup_callback]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=5,
          verbose=0)
      p.join()
      assert not p.is_alive()
Пример #30
0
def assert_classification_works(clf):
  np.random.seed(42)
  (x_train, y_train), (x_test, _) = testing_utils.get_test_data(
      train_samples=TRAIN_SAMPLES,
      test_samples=TEST_SAMPLES,
      input_shape=(INPUT_DIM,),
      num_classes=NUM_CLASSES)

  clf.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS)

  score = clf.score(x_train, y_train, batch_size=BATCH_SIZE)
  assert np.isscalar(score) and np.isfinite(score)

  preds = clf.predict(x_test, batch_size=BATCH_SIZE)
  assert preds.shape == (TEST_SAMPLES,)
  for prediction in np.unique(preds):
    assert prediction in range(NUM_CLASSES)

  proba = clf.predict_proba(x_test, batch_size=BATCH_SIZE)
  assert proba.shape == (TEST_SAMPLES, NUM_CLASSES)
  assert np.allclose(np.sum(proba, axis=1), np.ones(TEST_SAMPLES))
Пример #31
0
    def test_TensorBoard_with_ReduceLROnPlateau(self):
        with self.test_session():
            temp_dir = self.get_temp_dir()
            self.addCleanup(shutil.rmtree, temp_dir)

            (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)
            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)

            model = keras.models.Sequential()
            model.add(
                keras.layers.Dense(NUM_HIDDEN,
                                   input_dim=INPUT_DIM,
                                   activation='relu'))
            model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
            model.compile(loss='binary_crossentropy',
                          optimizer='sgd',
                          metrics=['accuracy'])

            cbks = [
                keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                                  factor=0.5,
                                                  patience=4,
                                                  verbose=1),
                keras.callbacks.TensorBoard(log_dir=temp_dir)
            ]

            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=2,
                      verbose=0)

            assert os.path.exists(temp_dir)
Пример #32
0
def assert_classification_works(clf):
    np.random.seed(42)
    (x_train,
     y_train), (x_test,
                _) = testing_utils.get_test_data(train_samples=TRAIN_SAMPLES,
                                                 test_samples=TEST_SAMPLES,
                                                 input_shape=(INPUT_DIM, ),
                                                 num_classes=NUM_CLASSES)

    clf.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS)

    score = clf.score(x_train, y_train, batch_size=BATCH_SIZE)
    assert np.isscalar(score) and np.isfinite(score)

    preds = clf.predict(x_test, batch_size=BATCH_SIZE)
    assert preds.shape == (TEST_SAMPLES, )
    for prediction in np.unique(preds):
        assert prediction in range(NUM_CLASSES)

    proba = clf.predict_proba(x_test, batch_size=BATCH_SIZE)
    assert proba.shape == (TEST_SAMPLES, NUM_CLASSES)
    assert np.allclose(np.sum(proba, axis=1), np.ones(TEST_SAMPLES))
Пример #33
0
  def test_temporal_classification_declarative(self):
    with self.test_session():
      np.random.seed(1336)
      (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
          train_samples=200,
          test_samples=100,
          input_shape=(4, 8),
          num_classes=2)
      y_train = keras.utils.to_categorical(y_train)
      y_test = keras.utils.to_categorical(y_test)

      model = keras.models.Sequential()
      model.add(keras.layers.LSTM(3, return_sequences=True,
                                  input_shape=x_train.shape[1:]))
      model.add(keras.layers.GRU(y_train.shape[-1], activation='softmax'))
      model.compile(loss='categorical_crossentropy',
                    optimizer='adam',
                    metrics=['accuracy'])
      history = model.fit(x_train, y_train, epochs=10, batch_size=16,
                          validation_data=(x_test, y_test),
                          verbose=2)
      self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #34
0
  def test_using_tf_layers_in_keras_sequential_model(self):
    with self.test_session():
      np.random.seed(1337)
      (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
          train_samples=200,
          test_samples=100,
          input_shape=(10,),
          num_classes=2)

      model = keras.models.Sequential()
      model.add(tf_core_layers.Dense(32, activation=nn.relu, input_shape=(10,)))
      model.add(tf_core_layers.Dense(2, activation=nn.softmax))
      model.summary()

      y_train = keras.utils.to_categorical(y_train)
      y_test = keras.utils.to_categorical(y_test)
      model.compile(loss='categorical_crossentropy',
                    optimizer='adam',
                    metrics=['accuracy'])
      history = model.fit(x_train, y_train, epochs=10, batch_size=16,
                          validation_data=(x_test, y_test),
                          verbose=0)
      self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #35
0
  def test_TensorBoard_with_ReduceLROnPlateau(self):
    with self.test_session():
      temp_dir = self.get_temp_dir()
      self.addCleanup(shutil.rmtree, temp_dir)

      (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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)

      model = keras.models.Sequential()
      model.add(
          keras.layers.Dense(
              NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
      model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
      model.compile(
          loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])

      cbks = [
          keras.callbacks.ReduceLROnPlateau(
              monitor='val_loss', factor=0.5, patience=4, verbose=1),
          keras.callbacks.TensorBoard(log_dir=temp_dir)
      ]

      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=2,
          verbose=0)

      assert os.path.exists(temp_dir)
Пример #36
0
    def test_vector_classification_shared_model(self):
        # Test that functional models that feature internal updates
        # and internal losses can be shared.
        with self.test_session():
            np.random.seed(1337)
            (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
                train_samples=200,
                test_samples=100,
                input_shape=(10, ),
                num_classes=2)
            y_train = keras.utils.to_categorical(y_train)
            y_test = keras.utils.to_categorical(y_test)

            inputs = keras.layers.Input(x_train.shape[1:])
            x = keras.layers.Dense(
                16,
                activation='relu',
                kernel_regularizer=keras.regularizers.l2(1e-5),
                bias_regularizer=keras.regularizers.l2(1e-5),
                input_shape=x_train.shape[1:])(inputs)
            x = keras.layers.BatchNormalization()(x)
            base_model = keras.models.Model(inputs, x)

            x = keras.layers.Input(x_train.shape[1:])
            y = base_model(x)
            y = keras.layers.Dense(y_train.shape[-1], activation='softmax')(y)
            model = keras.models.Model(x, y)
            model.compile(loss='categorical_crossentropy',
                          optimizer='rmsprop',
                          metrics=['accuracy'])
            history = model.fit(x_train,
                                y_train,
                                epochs=10,
                                batch_size=16,
                                validation_data=(x_test, y_test),
                                verbose=2)
            self.assertGreater(history.history['val_acc'][-1], 0.85)
Пример #37
0
  def test_vector_classification_functional(self):
    with self.test_session():
      np.random.seed(1337)
      (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
          train_samples=200,
          test_samples=100,
          input_shape=(8,),
          num_classes=2)
      y_train = keras.utils.to_categorical(y_train)
      y_test = keras.utils.to_categorical(y_test)

      inputs = keras.layers.Input(shape=x_train.shape[1:])
      x = keras.layers.Dense(8, activation='relu')(inputs)
      x = keras.layers.Dropout(0.1)(x)
      outputs = keras.layers.Dense(y_train.shape[-1], activation='softmax')(x)

      model = keras.models.Model(inputs, outputs)
      model.compile(loss='categorical_crossentropy',
                    optimizer='rmsprop',
                    metrics=['accuracy'])
      history = model.fit(x_train, y_train, epochs=10, batch_size=16,
                          validation_data=(x_test, y_test),
                          verbose=2)
      self.assertTrue(history.history['val_acc'][-1] > 0.85)
Пример #38
0
  def test_temporal_sample_weights(self):
    num_classes = 5
    batch_size = 5
    epochs = 5
    weighted_class = 3
    train_samples = 1000
    test_samples = 1000
    input_dim = 5
    timesteps = 3

    with self.test_session():
      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'))

      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()
      # convert class vectors to binary class matrices
      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.) for i in range(num_classes)])
      class_weight[weighted_class] = 2.

      sample_weight = np.ones((y_train.shape[0]))
      sample_weight[int_y_train == weighted_class] = 2.

      temporal_x_train = np.reshape(x_train, (len(x_train), 1,
                                              x_train.shape[1]))
      temporal_x_train = np.repeat(temporal_x_train, timesteps, axis=1)
      temporal_x_test = np.reshape(x_test, (len(x_test), 1, x_test.shape[1]))
      temporal_x_test = np.repeat(temporal_x_test, timesteps, axis=1)

      temporal_y_train = np.reshape(y_train, (len(y_train), 1,
                                              y_train.shape[1]))
      temporal_y_train = np.repeat(temporal_y_train, timesteps, axis=1)
      temporal_y_test = np.reshape(y_test, (len(y_test), 1, y_test.shape[1]))
      temporal_y_test = np.repeat(temporal_y_test, timesteps, axis=1)

      temporal_sample_weight = np.reshape(sample_weight, (len(sample_weight),
                                                          1))
      temporal_sample_weight = np.repeat(
          temporal_sample_weight, timesteps, axis=1)

      model.compile(
          loss='binary_crossentropy',
          optimizer='rmsprop',
          sample_weight_mode='temporal')

      model.fit(
          temporal_x_train,
          temporal_y_train,
          batch_size=batch_size,
          epochs=epochs // 3,
          verbose=0,
          sample_weight=temporal_sample_weight)
      model.fit(
          temporal_x_train,
          temporal_y_train,
          batch_size=batch_size,
          epochs=epochs // 3,
          verbose=0,
          sample_weight=temporal_sample_weight,
          validation_split=0.1)

      model.train_on_batch(
          temporal_x_train[:batch_size],
          temporal_y_train[:batch_size],
          sample_weight=temporal_sample_weight[:batch_size])
      model.test_on_batch(
          temporal_x_train[:batch_size],
          temporal_y_train[:batch_size],
          sample_weight=temporal_sample_weight[:batch_size])
      ref_score = model.evaluate(temporal_x_test, temporal_y_test, verbose=0)
      score = model.evaluate(
          temporal_x_test[test_ids], temporal_y_test[test_ids], verbose=0)
      self.assertLess(score, ref_score)
Пример #39
0
    def test_temporal_sample_weights(self):
        num_classes = 5
        batch_size = 5
        epochs = 5
        weighted_class = 3
        train_samples = 1000
        test_samples = 1000
        input_dim = 5
        timesteps = 3

        with self.test_session():
            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'))

            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()
            # convert class vectors to binary class matrices
            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.) for i in range(num_classes)])
            class_weight[weighted_class] = 2.

            sample_weight = np.ones((y_train.shape[0]))
            sample_weight[int_y_train == weighted_class] = 2.

            temporal_x_train = np.reshape(x_train,
                                          (len(x_train), 1, x_train.shape[1]))
            temporal_x_train = np.repeat(temporal_x_train, timesteps, axis=1)
            temporal_x_test = np.reshape(x_test,
                                         (len(x_test), 1, x_test.shape[1]))
            temporal_x_test = np.repeat(temporal_x_test, timesteps, axis=1)

            temporal_y_train = np.reshape(y_train,
                                          (len(y_train), 1, y_train.shape[1]))
            temporal_y_train = np.repeat(temporal_y_train, timesteps, axis=1)
            temporal_y_test = np.reshape(y_test,
                                         (len(y_test), 1, y_test.shape[1]))
            temporal_y_test = np.repeat(temporal_y_test, timesteps, axis=1)

            temporal_sample_weight = np.reshape(sample_weight,
                                                (len(sample_weight), 1))
            temporal_sample_weight = np.repeat(temporal_sample_weight,
                                               timesteps,
                                               axis=1)

            model.compile(loss='binary_crossentropy',
                          optimizer='rmsprop',
                          sample_weight_mode='temporal')

            model.fit(temporal_x_train,
                      temporal_y_train,
                      batch_size=batch_size,
                      epochs=epochs // 3,
                      verbose=0,
                      sample_weight=temporal_sample_weight)
            model.fit(temporal_x_train,
                      temporal_y_train,
                      batch_size=batch_size,
                      epochs=epochs // 3,
                      verbose=0,
                      sample_weight=temporal_sample_weight,
                      validation_split=0.1)

            model.train_on_batch(
                temporal_x_train[:batch_size],
                temporal_y_train[:batch_size],
                sample_weight=temporal_sample_weight[:batch_size])
            model.test_on_batch(
                temporal_x_train[:batch_size],
                temporal_y_train[:batch_size],
                sample_weight=temporal_sample_weight[:batch_size])
            ref_score = model.evaluate(temporal_x_test,
                                       temporal_y_test,
                                       verbose=0)
            score = model.evaluate(temporal_x_test[test_ids],
                                   temporal_y_test[test_ids],
                                   verbose=0)
            self.assertLess(score, ref_score)
Пример #40
0
  def test_class_weights(self):
    num_classes = 5
    batch_size = 5
    epochs = 5
    weighted_class = 3
    train_samples = 1000
    test_samples = 1000
    input_dim = 5

    with self.test_session():
      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='rmsprop')

      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()
      # convert class vectors to binary class matrices
      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.) for i in range(num_classes)])
      class_weight[weighted_class] = 2.

      sample_weight = np.ones((y_train.shape[0]))
      sample_weight[int_y_train == weighted_class] = 2.

      model.fit(
          x_train,
          y_train,
          batch_size=batch_size,
          epochs=epochs // 3,
          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=epochs // 2,
          verbose=0,
          class_weight=class_weight)
      model.fit(
          x_train,
          y_train,
          batch_size=batch_size,
          epochs=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)
Пример #41
0
    def test_class_weights(self):
        num_classes = 5
        batch_size = 5
        epochs = 5
        weighted_class = 3
        train_samples = 1000
        test_samples = 1000
        input_dim = 5

        with self.test_session():
            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='rmsprop')

            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()
            # convert class vectors to binary class matrices
            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.) for i in range(num_classes)])
            class_weight[weighted_class] = 2.

            sample_weight = np.ones((y_train.shape[0]))
            sample_weight[int_y_train == weighted_class] = 2.

            model.fit(x_train,
                      y_train,
                      batch_size=batch_size,
                      epochs=epochs // 3,
                      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=epochs // 2,
                      verbose=0,
                      class_weight=class_weight)
            model.fit(x_train,
                      y_train,
                      batch_size=batch_size,
                      epochs=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)
Пример #42
0
  def test_TensorBoard_multi_input_output(self):
    np.random.seed(1337)
    tmpdir = self.get_temp_dir()
    self.addCleanup(shutil.rmtree, tmpdir)

    with self.test_session():
      filepath = os.path.join(tmpdir, 'logs')

      (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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)

      def data_generator(train):
        if train:
          max_batch_index = len(x_train) // BATCH_SIZE
        else:
          max_batch_index = len(x_test) // BATCH_SIZE
        i = 0
        while 1:
          if train:
            # simulate multi-input/output models
            yield ([x_train[i * BATCH_SIZE: (i + 1) * BATCH_SIZE]] * 2,
                   [y_train[i * BATCH_SIZE: (i + 1) * BATCH_SIZE]] * 2)
          else:
            yield ([x_test[i * BATCH_SIZE: (i + 1) * BATCH_SIZE]] * 2,
                   [y_test[i * BATCH_SIZE: (i + 1) * BATCH_SIZE]] * 2)
          i += 1
          i %= max_batch_index

      inp1 = keras.Input((INPUT_DIM,))
      inp2 = keras.Input((INPUT_DIM,))
      inp = keras.layers.add([inp1, inp2])
      hidden = keras.layers.Dense(2, activation='relu')(inp)
      hidden = keras.layers.Dropout(0.1)(hidden)
      output1 = keras.layers.Dense(NUM_CLASSES, activation='softmax')(hidden)
      output2 = keras.layers.Dense(NUM_CLASSES, activation='softmax')(hidden)
      model = keras.models.Model([inp1, inp2], [output1, output2])
      model.compile(loss='categorical_crossentropy',
                    optimizer='sgd',
                    metrics=['accuracy'])

      # we must generate new callbacks for each test, as they aren't stateless
      def callbacks_factory(histogram_freq):
        return [keras.callbacks.TensorBoard(log_dir=filepath,
                                            histogram_freq=histogram_freq,
                                            write_images=True, write_grads=True,
                                            embeddings_freq=1,
                                            embeddings_layer_names=['dense_1'],
                                            batch_size=5)]

      # fit without validation data
      model.fit([x_train] * 2, [y_train] * 2, batch_size=BATCH_SIZE,
                callbacks=callbacks_factory(histogram_freq=0), epochs=3)

      # fit with validation data and accuracy
      model.fit([x_train] * 2, [y_train] * 2, batch_size=BATCH_SIZE,
                validation_data=([x_test] * 2, [y_test] * 2),
                callbacks=callbacks_factory(histogram_freq=1), epochs=2)

      # fit generator without validation data
      model.fit_generator(data_generator(True), len(x_train), epochs=2,
                          callbacks=callbacks_factory(histogram_freq=0))

      # fit generator with validation data and accuracy
      model.fit_generator(data_generator(True), len(x_train), epochs=2,
                          validation_data=([x_test] * 2, [y_test] * 2),
                          callbacks=callbacks_factory(histogram_freq=1))
      assert os.path.isdir(filepath)
Пример #43
0
    def test_class_weight_invalid_use_case(self):
        num_classes = 5
        train_samples = 1000
        test_samples = 1000
        input_dim = 5
        timesteps = 3

        with self.test_session():
            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='rmsprop')

            (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)
            # convert class vectors to binary class matrices
            y_train = keras.utils.to_categorical(y_train, num_classes)
            class_weight = dict([(i, 1.) 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='rmsprop',
                              sample_weight_mode=[])

            # Build multi-output model
            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='rmsprop', loss='mse')
            x_np = np.random.random((10, 3))
            y_np = np.random.random((10, 4))
            w_np = np.random.random((10, ))
            # This will work
            model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': w_np})
            # These will not
            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})
Пример #44
0
  def test_CSVLogger(self):
    with self.test_session():
      np.random.seed(1337)
      temp_dir = self.get_temp_dir()
      self.addCleanup(shutil.rmtree, temp_dir)
      filepath = os.path.join(temp_dir, 'log.tsv')

      sep = '\t'
      (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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)

      def make_model():
        np.random.seed(1337)
        model = keras.models.Sequential()
        model.add(
            keras.layers.Dense(
                NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
        model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))

        model.compile(
            loss='categorical_crossentropy',
            optimizer=keras.optimizers.SGD(lr=0.1),
            metrics=['accuracy'])
        return model

      # case 1, create new file with defined separator
      model = make_model()
      cbks = [keras.callbacks.CSVLogger(filepath, separator=sep)]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)

      assert os.path.exists(filepath)
      with open(filepath) as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read())
      assert dialect.delimiter == sep
      del model
      del cbks

      # case 2, append data to existing file, skip header
      model = make_model()
      cbks = [keras.callbacks.CSVLogger(filepath, separator=sep, append=True)]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)

      # case 3, reuse of CSVLogger object
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)

      with open(filepath) as csvfile:
        output = ' '.join(csvfile.readlines())
        assert len(re.findall('epoch', output)) == 1

      os.remove(filepath)
Пример #45
0
  def test_TensorBoard(self):
    np.random.seed(1337)

    temp_dir = self.get_temp_dir()
    self.addCleanup(shutil.rmtree, temp_dir)

    (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)
    y_test = keras.utils.to_categorical(y_test)
    y_train = keras.utils.to_categorical(y_train)

    def data_generator(train):
      if train:
        max_batch_index = len(x_train) // BATCH_SIZE
      else:
        max_batch_index = len(x_test) // BATCH_SIZE
      i = 0
      while 1:
        if train:
          yield (x_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE],
                 y_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE])
        else:
          yield (x_test[i * BATCH_SIZE:(i + 1) * BATCH_SIZE],
                 y_test[i * BATCH_SIZE:(i + 1) * BATCH_SIZE])
        i += 1
        i %= max_batch_index

    # case: Sequential
    with self.test_session():
      model = keras.models.Sequential()
      model.add(
          keras.layers.Dense(
              NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
      model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
      model.compile(
          loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

      tsb = keras.callbacks.TensorBoard(
          log_dir=temp_dir, histogram_freq=1, write_images=True,
          write_grads=True, embeddings_freq=1,
          embeddings_layer_names=['dense_1'], batch_size=5)
      cbks = [tsb]

      # fit with validation data
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=3,
          verbose=0)

      # fit with validation data and accuracy
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=2,
          verbose=0)

      # fit generator with validation data
      model.fit_generator(
          data_generator(True),
          len(x_train),
          epochs=2,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          verbose=0)

      # fit generator without validation data
      model.fit_generator(
          data_generator(True),
          len(x_train),
          epochs=2,
          callbacks=cbks,
          verbose=0)

      # fit generator with validation data and accuracy
      model.fit_generator(
          data_generator(True),
          len(x_train),
          epochs=2,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          verbose=0)

      # fit generator without validation data and accuracy
      model.fit_generator(
          data_generator(True), len(x_train), epochs=2, callbacks=cbks)
      assert os.path.exists(temp_dir)
Пример #46
0
  def test_class_weight_invalid_use_case(self):
    num_classes = 5
    train_samples = 1000
    test_samples = 1000
    input_dim = 5
    timesteps = 3

    with self.test_session():
      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='rmsprop')

      (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)
      # convert class vectors to binary class matrices
      y_train = keras.utils.to_categorical(y_train, num_classes)
      class_weight = dict([(i, 1.) 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='rmsprop',
            sample_weight_mode=[])

      # Build multi-output model
      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='rmsprop', loss='mse')
      x_np = np.random.random((10, 3))
      y_np = np.random.random((10, 4))
      w_np = np.random.random((10,))
      # This will work
      model.fit(x_np, [y_np, y_np], epochs=1,
                sample_weight={'1': w_np})
      # These will not
      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})
Пример #47
0
    def test_TensorBoard_multi_input_output(self):
        np.random.seed(1337)
        tmpdir = self.get_temp_dir()
        self.addCleanup(shutil.rmtree, tmpdir)

        with self.test_session():
            filepath = os.path.join(tmpdir, 'logs')

            (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)
            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)

            def data_generator(train):
                if train:
                    max_batch_index = len(x_train) // BATCH_SIZE
                else:
                    max_batch_index = len(x_test) // BATCH_SIZE
                i = 0
                while 1:
                    if train:
                        # simulate multi-input/output models
                        yield ([x_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]] *
                               2,
                               [y_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]] *
                               2)
                    else:
                        yield ([x_test[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]] *
                               2,
                               [y_test[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]] *
                               2)
                    i += 1
                    i %= max_batch_index

            inp1 = keras.Input((INPUT_DIM, ))
            inp2 = keras.Input((INPUT_DIM, ))
            inp = keras.layers.add([inp1, inp2])
            hidden = keras.layers.Dense(2, activation='relu')(inp)
            hidden = keras.layers.Dropout(0.1)(hidden)
            output1 = keras.layers.Dense(NUM_CLASSES,
                                         activation='softmax')(hidden)
            output2 = keras.layers.Dense(NUM_CLASSES,
                                         activation='softmax')(hidden)
            model = keras.models.Model([inp1, inp2], [output1, output2])
            model.compile(loss='categorical_crossentropy',
                          optimizer='sgd',
                          metrics=['accuracy'])

            # we must generate new callbacks for each test, as they aren't stateless
            def callbacks_factory(histogram_freq):
                return [
                    keras.callbacks.TensorBoard(
                        log_dir=filepath,
                        histogram_freq=histogram_freq,
                        write_images=True,
                        write_grads=True,
                        embeddings_freq=1,
                        embeddings_layer_names=['dense_1'],
                        batch_size=5)
                ]

            # fit without validation data
            model.fit([x_train] * 2, [y_train] * 2,
                      batch_size=BATCH_SIZE,
                      callbacks=callbacks_factory(histogram_freq=0),
                      epochs=3)

            # fit with validation data and accuracy
            model.fit([x_train] * 2, [y_train] * 2,
                      batch_size=BATCH_SIZE,
                      validation_data=([x_test] * 2, [y_test] * 2),
                      callbacks=callbacks_factory(histogram_freq=1),
                      epochs=2)

            # fit generator without validation data
            model.fit_generator(data_generator(True),
                                len(x_train),
                                epochs=2,
                                callbacks=callbacks_factory(histogram_freq=0))

            # fit generator with validation data and accuracy
            model.fit_generator(data_generator(True),
                                len(x_train),
                                epochs=2,
                                validation_data=([x_test] * 2, [y_test] * 2),
                                callbacks=callbacks_factory(histogram_freq=1))
            assert os.path.isdir(filepath)
Пример #48
0
    def test_TensorBoard(self):
        np.random.seed(1337)

        temp_dir = self.get_temp_dir()
        self.addCleanup(shutil.rmtree, temp_dir)

        (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)
        y_test = keras.utils.to_categorical(y_test)
        y_train = keras.utils.to_categorical(y_train)

        def data_generator(train):
            if train:
                max_batch_index = len(x_train) // BATCH_SIZE
            else:
                max_batch_index = len(x_test) // BATCH_SIZE
            i = 0
            while 1:
                if train:
                    yield (x_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE],
                           y_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE])
                else:
                    yield (x_test[i * BATCH_SIZE:(i + 1) * BATCH_SIZE],
                           y_test[i * BATCH_SIZE:(i + 1) * BATCH_SIZE])
                i += 1
                i %= max_batch_index

        # case: Sequential
        with self.test_session():
            model = keras.models.Sequential()
            model.add(
                keras.layers.Dense(NUM_HIDDEN,
                                   input_dim=INPUT_DIM,
                                   activation='relu'))
            model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
            model.compile(loss='categorical_crossentropy',
                          optimizer='sgd',
                          metrics=['accuracy'])

            tsb = keras.callbacks.TensorBoard(
                log_dir=temp_dir,
                histogram_freq=1,
                write_images=True,
                write_grads=True,
                embeddings_freq=1,
                embeddings_layer_names=['dense_1'],
                batch_size=5)
            cbks = [tsb]

            # fit with validation data
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=3,
                      verbose=0)

            # fit with validation data and accuracy
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=2,
                      verbose=0)

            # fit generator with validation data
            model.fit_generator(data_generator(True),
                                len(x_train),
                                epochs=2,
                                validation_data=(x_test, y_test),
                                callbacks=cbks,
                                verbose=0)

            # fit generator without validation data
            model.fit_generator(data_generator(True),
                                len(x_train),
                                epochs=2,
                                callbacks=cbks,
                                verbose=0)

            # fit generator with validation data and accuracy
            model.fit_generator(data_generator(True),
                                len(x_train),
                                epochs=2,
                                validation_data=(x_test, y_test),
                                callbacks=cbks,
                                verbose=0)

            # fit generator without validation data and accuracy
            model.fit_generator(data_generator(True),
                                len(x_train),
                                epochs=2,
                                callbacks=cbks)
            assert os.path.exists(temp_dir)
Пример #49
0
    def test_ModelCheckpoint(self):
        if h5py is None:
            return  # Skip test if models cannot be saved.

        with self.test_session():
            np.random.seed(1337)

            temp_dir = self.get_temp_dir()
            self.addCleanup(shutil.rmtree, temp_dir)

            filepath = os.path.join(temp_dir, 'checkpoint.h5')
            (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)
            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)
            # case 1
            monitor = 'val_loss'
            save_best_only = False
            mode = 'auto'

            model = keras.models.Sequential()
            model.add(
                keras.layers.Dense(NUM_HIDDEN,
                                   input_dim=INPUT_DIM,
                                   activation='relu'))
            model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
            model.compile(loss='categorical_crossentropy',
                          optimizer='rmsprop',
                          metrics=['accuracy'])

            cbks = [
                keras.callbacks.ModelCheckpoint(filepath,
                                                monitor=monitor,
                                                save_best_only=save_best_only,
                                                mode=mode)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)
            assert os.path.exists(filepath)
            os.remove(filepath)

            # case 2
            mode = 'min'
            cbks = [
                keras.callbacks.ModelCheckpoint(filepath,
                                                monitor=monitor,
                                                save_best_only=save_best_only,
                                                mode=mode)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)
            assert os.path.exists(filepath)
            os.remove(filepath)

            # case 3
            mode = 'max'
            monitor = 'val_acc'
            cbks = [
                keras.callbacks.ModelCheckpoint(filepath,
                                                monitor=monitor,
                                                save_best_only=save_best_only,
                                                mode=mode)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)
            assert os.path.exists(filepath)
            os.remove(filepath)

            # case 4
            save_best_only = True
            cbks = [
                keras.callbacks.ModelCheckpoint(filepath,
                                                monitor=monitor,
                                                save_best_only=save_best_only,
                                                mode=mode)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)
            assert os.path.exists(filepath)
            os.remove(filepath)

            # Case: metric not available.
            cbks = [
                keras.callbacks.ModelCheckpoint(filepath,
                                                monitor='unknown',
                                                save_best_only=True)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)
            # File won't be written.
            assert not os.path.exists(filepath)

            # case 5
            save_best_only = False
            period = 2
            mode = 'auto'

            filepath = os.path.join(temp_dir, 'checkpoint.{epoch:02d}.h5')
            cbks = [
                keras.callbacks.ModelCheckpoint(filepath,
                                                monitor=monitor,
                                                save_best_only=save_best_only,
                                                mode=mode,
                                                period=period)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=4,
                      verbose=1)
            assert os.path.exists(filepath.format(epoch=1))
            assert os.path.exists(filepath.format(epoch=3))
            os.remove(filepath.format(epoch=1))
            os.remove(filepath.format(epoch=3))
            assert not os.path.exists(filepath.format(epoch=0))
            assert not os.path.exists(filepath.format(epoch=2))

            # Invalid use: this will raise a warning but not an Exception.
            keras.callbacks.ModelCheckpoint(filepath,
                                            monitor=monitor,
                                            save_best_only=save_best_only,
                                            mode='unknown')
Пример #50
0
  def test_ModelCheckpoint(self):
    if h5py is None:
      return  # Skip test if models cannot be saved.

    with self.test_session():
      np.random.seed(1337)

      temp_dir = self.get_temp_dir()
      self.addCleanup(shutil.rmtree, temp_dir)

      filepath = os.path.join(temp_dir, 'checkpoint.h5')
      (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)
      y_test = keras.utils.to_categorical(y_test)
      y_train = keras.utils.to_categorical(y_train)
      # case 1
      monitor = 'val_loss'
      save_best_only = False
      mode = 'auto'

      model = keras.models.Sequential()
      model.add(
          keras.layers.Dense(
              NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
      model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
      model.compile(
          loss='categorical_crossentropy',
          optimizer='rmsprop',
          metrics=['accuracy'])

      cbks = [
          keras.callbacks.ModelCheckpoint(
              filepath,
              monitor=monitor,
              save_best_only=save_best_only,
              mode=mode)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)
      assert os.path.exists(filepath)
      os.remove(filepath)

      # case 2
      mode = 'min'
      cbks = [
          keras.callbacks.ModelCheckpoint(
              filepath,
              monitor=monitor,
              save_best_only=save_best_only,
              mode=mode)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)
      assert os.path.exists(filepath)
      os.remove(filepath)

      # case 3
      mode = 'max'
      monitor = 'val_acc'
      cbks = [
          keras.callbacks.ModelCheckpoint(
              filepath,
              monitor=monitor,
              save_best_only=save_best_only,
              mode=mode)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)
      assert os.path.exists(filepath)
      os.remove(filepath)

      # case 4
      save_best_only = True
      cbks = [
          keras.callbacks.ModelCheckpoint(
              filepath,
              monitor=monitor,
              save_best_only=save_best_only,
              mode=mode)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)
      assert os.path.exists(filepath)
      os.remove(filepath)

      # Case: metric not available.
      cbks = [
          keras.callbacks.ModelCheckpoint(
              filepath,
              monitor='unknown',
              save_best_only=True)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=1,
          verbose=0)
      # File won't be written.
      assert not os.path.exists(filepath)

      # case 5
      save_best_only = False
      period = 2
      mode = 'auto'

      filepath = os.path.join(temp_dir, 'checkpoint.{epoch:02d}.h5')
      cbks = [
          keras.callbacks.ModelCheckpoint(
              filepath,
              monitor=monitor,
              save_best_only=save_best_only,
              mode=mode,
              period=period)
      ]
      model.fit(
          x_train,
          y_train,
          batch_size=BATCH_SIZE,
          validation_data=(x_test, y_test),
          callbacks=cbks,
          epochs=4,
          verbose=1)
      assert os.path.exists(filepath.format(epoch=1))
      assert os.path.exists(filepath.format(epoch=3))
      os.remove(filepath.format(epoch=1))
      os.remove(filepath.format(epoch=3))
      assert not os.path.exists(filepath.format(epoch=0))
      assert not os.path.exists(filepath.format(epoch=2))

      # Invalid use: this will raise a warning but not an Exception.
      keras.callbacks.ModelCheckpoint(
          filepath,
          monitor=monitor,
          save_best_only=save_best_only,
          mode='unknown')
Пример #51
0
    def test_CSVLogger(self):
        with self.test_session():
            np.random.seed(1337)
            temp_dir = self.get_temp_dir()
            self.addCleanup(shutil.rmtree, temp_dir)
            filepath = os.path.join(temp_dir, 'log.tsv')

            sep = '\t'
            (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)
            y_test = keras.utils.to_categorical(y_test)
            y_train = keras.utils.to_categorical(y_train)

            def make_model():
                np.random.seed(1337)
                model = keras.models.Sequential()
                model.add(
                    keras.layers.Dense(NUM_HIDDEN,
                                       input_dim=INPUT_DIM,
                                       activation='relu'))
                model.add(keras.layers.Dense(NUM_CLASSES,
                                             activation='softmax'))

                model.compile(loss='categorical_crossentropy',
                              optimizer=keras.optimizers.SGD(lr=0.1),
                              metrics=['accuracy'])
                return model

            # case 1, create new file with defined separator
            model = make_model()
            cbks = [keras.callbacks.CSVLogger(filepath, separator=sep)]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)

            assert os.path.exists(filepath)
            with open(filepath) as csvfile:
                dialect = csv.Sniffer().sniff(csvfile.read())
            assert dialect.delimiter == sep
            del model
            del cbks

            # case 2, append data to existing file, skip header
            model = make_model()
            cbks = [
                keras.callbacks.CSVLogger(filepath, separator=sep, append=True)
            ]
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)

            # case 3, reuse of CSVLogger object
            model.fit(x_train,
                      y_train,
                      batch_size=BATCH_SIZE,
                      validation_data=(x_test, y_test),
                      callbacks=cbks,
                      epochs=1,
                      verbose=0)

            with open(filepath) as csvfile:
                output = ' '.join(csvfile.readlines())
                assert len(re.findall('epoch', output)) == 1

            os.remove(filepath)