def run_epoch(self, sess, saver, train, dev):
        prog = Progbar(target=int(len(train) / self.config.batch_size))
        losses, grad_norms = [], []
        for i, batch in enumerate(minibatches(train, self.config.batch_size)):
            loss, grad_norm, summ = self.train_on_batch(sess, *batch)
            losses.append(loss)
            grad_norms.append(grad_norm)
            prog.update(i + 1, [("train loss", loss)])

        print("\nEvaluating on dev set...")
        predictions = []
        references = []
        for batch in minibatches(dev, self.config.batch_size):
            inputs_batch, targets_batch = batch
            prediction = list(self.predict_on_batch(sess, inputs_batch))
            predictions += prediction
            references += list(targets_batch)

        predictions = [
            tokens_to_sentences(pred, self.config.idx2word)
            for pred in predictions
        ]
        references = [
            tokens_to_sentences(ref, self.config.idx2word)
            for ref in references
        ]

        f1, _, _ = rouge_n(predictions, references)
        print("- dev rouge f1: {}".format(f1))
        return losses, grad_norms, summ, predictions, f1
예제 #2
0
    def run_epoch(self, sess, saver, train, dev):
        prog = Progbar(target=int(len(train) / self.config.batch_size))
        train_preds, losses, accs, refs = [], [], [], []
        for i, batch in enumerate(minibatches(train, self.config.batch_size)):
            _, targets_batch = batch
            train_pred, loss, acc, loss_summ, acc_summ = self.train_on_batch(
                sess, *batch)
            train_pred = list(train_pred)
            losses.append(loss)
            accs.append(acc)
            train_preds += train_pred
            refs += list(targets_batch)
            prog.update(i + 1, [("train loss", loss), ("train acc", acc)])

        train_preds = [
            tokens_to_sentences(pred, self.config.idx2word)
            for pred in train_preds
        ]
        refs = [tokens_to_sentences(ref, self.config.idx2word) for ref in refs]

        train_f1, _, _ = rouge_n(train_preds, refs)
        print("- train rouge f1: {}".format(train_f1))

        print("\nEvaluating on dev set...")
        dev_preds, refs, dev_losses, dev_accs = [], [], [], []
        prog_dev = Progbar(target=int(len(dev) / self.config.batch_size))
        for i, batch in enumerate(minibatches(dev, self.config.batch_size)):
            _, targets_batch = batch
            dev_pred, dev_loss, dev_acc, dev_loss_summ, dev_acc_summ = self.predict_on_batch(
                sess, *batch)
            dev_pred = list(dev_pred)
            dev_losses.append(dev_loss)
            dev_accs.append(dev_acc)
            dev_preds += dev_pred
            refs += list(targets_batch)
            prog_dev.update(i + 1, [("dev loss", dev_loss),
                                    ("dev_acc", dev_acc)])

        dev_preds = [
            tokens_to_sentences(pred, self.config.idx2word)
            for pred in dev_preds
        ]
        refs = [tokens_to_sentences(ref, self.config.idx2word) for ref in refs]

        dev_f1, _, _ = rouge_n(dev_preds, refs)
        print("- dev rouge f1: {}".format(dev_f1))
        return losses, accs, dev_losses, dev_accs, loss_summ, acc_summ, dev_loss_summ, dev_acc_summ, dev_f1
예제 #3
0
        refs = []
        test_losses = []
        test_accs = []
        for batch in minibatches(test, model.config.batch_size):
            inputs_batch, targets_batch = batch
            pred, test_loss, test_acc, _, _ = model.predict_on_batch(
                sess, *batch)
            pred = list(pred)
            preds += pred
            refs += list(targets_batch)
            test_losses.append(test_loss)
            test_accs.append(test_acc)

        mean_test_loss = np.mean(np.asarray(test_losses))
        preds = [
            tokens_to_sentences(pred, model.config.idx2word) for pred in preds
        ]
        refs = [
            tokens_to_sentences(ref, model.config.idx2word) for ref in refs
        ]

        f1, _, _ = rouge_n(preds, refs)
        print("- test ROUGE: {}".format(f1))
        print("- test loss: {}".format(mean_test_loss))
        print("Writing predictions")
        fname = './data/predictions' + str(date.today()) + '.txt'
        with open(fname, 'w') as f:
            for pred, ref in zip(preds, refs):
                f.write(pred + '\t' + ref)
                f.write('\n')
        print("Done!")
            print(80 * "=")
            print("TESTING")
            print(80 * "=")
            print("Restoring the best model weights found on the dev set")
            saver.restore(sess, './data/weights/model.weights')
            print("Final evaluation on test set")
            predictions = []
            references = []
            for batch in minibatches(test, model.config.batch_size):
                inputs_batch, targets_batch = batch
                prediction = list(model.predict_on_batch(sess, inputs_batch))
                predictions += prediction
                references += list(targets_batch)

            predictions = [
                tokens_to_sentences(pred, model.config.idx2word)
                for pred in predictions
            ]
            references = [
                tokens_to_sentences(ref, model.config.idx2word)
                for ref in references
            ]

            f1, _, _ = rouge_n(predictions, references)
            print("- test ROUGE: {}".format(f1))
            print("Writing predictions")
            fname = 'predictions' + str(date.today()) + '.txt'
            with open(fname, 'w') as f:
                for pred, ref in zip(predictions, references):
                    f.write(pred + '\t' + ref)
                    f.write('\n')