print(
            'valid:  f1: %.5f, precision: %.5f, recall: %.5f, best f1: %.5f\n'
            % (f1, precision, recall, self.best_val_f1))
        f1, precision, recall = evaluate(test_data, self.model)
        print('test:  f1: %.5f, precision: %.5f, recall: %.5f\n' %
              (f1, precision, recall))


if __name__ == '__main__':
    train_generator = data_generator(train_data, batch_size)

    predecessor_model_name = 'predecessor_best.weights'
    predecessor_evaluator = Evaluator(predecessor_model_name)

    predecessor_model.fit(train_generator.generator(),
                          steps_per_epoch=len(train_generator),
                          epochs=epochs,
                          callbacks=[predecessor_evaluator])

    theseus_model_name = 'theseus_best.weights'
    theseus_evaluator = Evaluator(theseus_model_name)
    theseus_model.fit_generator(train_generator.generator(),
                                steps_per_epoch=len(train_generator),
                                epochs=epochs * 2,
                                callbacks=[theseus_evaluator])

    theseus_model.load_weights(theseus_model_name)

    successor_model_name = 'successor_best.weights'
    successor_evaluator = Evaluator(successor_model_name)
    successor_model.fit_generator(train_generator.generator(),
                                  steps_per_epoch=len(train_generator),
Exemplo n.º 2
0
            % (filename, filename + '.pred.json')).read().strip())
    return metrics


class Evaluator(keras.callbacks.Callback):
    """评估和保存模型
    """
    def __init__(self):
        self.best_val_f1 = 0.

    def on_epoch_end(self, epoch, logs=None):
        metrics = evaluate(
            '/home/mingming.xu/datasets/NLP/qa/dureader_robust-data/dev.json')
        if float(metrics['F1']) >= float(self.best_val_f1):
            self.best_val_f1 = metrics['F1']
            model.save_weights('best_model.weights')
        metrics['BEST F1'] = self.best_val_f1
        print(metrics)


if __name__ == '__main__':
    train_generator = data_generator(train_data, batch_size)

    evaluator = Evaluator()
    model.fit(train_generator.generator(),
              steps_per_epoch=len(train_generator),
              epochs=5,
              callbacks=[evaluator])
else:
    model.load_weights('best_model.weights')