Esempio n. 1
0
    def __init__(self, dset, conf, save=False):

        # Set batches
        train, val, test = dset.build_batches("relevant")

        # Build model, optimizer, loss and scheduler
        model = self.build_model(conf["model"], dset.vocab, dset.char_vocab)
        opt = Optimizer(model.parameters(), conf["optim"])
        loss = BCE()
        lr_sch = LR_scheduler(opt.opt, conf["optim"])

        # To track early stopping
        self.best = {"val": {"f1": 0}, "test": {}}
        step, stop = 0, 0

        # For max epochs
        for ep in range(conf["train"]["max_epochs"]):
            print("\n\tEpoch %d" % ep)
            for batch in train:
                # set the in training mode.
                model.train()
                # advance step
                step += 1
                # forward pass
                x, y, mask = self.fw_pass(model, batch)
                # measure error
                fw_loss = loss(x, y)
                # backward pass
                opt.train_op(fw_loss)

                # validation
                if step % conf["train"]["val_steps"] == 0:
                    # Set the in testing mode
                    model.eval()
                    # Eval on val set
                    val_metrics = utils.bin_fw_eval(model, self.fw_pass, val,
                                                    step)
                    if val_metrics["f1"] > self.best["val"]["f1"]:
                        # reset Early stop
                        stop = 0
                        # Eval on test set
                        test_metrics = utils.bin_fw_eval(
                            model, self.fw_pass, test, step)
                        self.best = {"val": val_metrics, "test": test_metrics}
                        if save:
                            model.save(step, conf, self.best, opt, lr_sch,
                                       "bin")
                    else:
                        if stop == conf["train"]["patience"]:
                            return
                        stop += 1
                # maybe update lr
                lr_sch.step()
    def __init__(self, dset, conf, save=False):
        # Set batches
        train, val, test = dset.build_batches("tok_tags")

        # Build model
        model = self.build_model(conf["model"], dset.vocab, dset.char_vocab,
                                 dset.tag_vocab)
        opt = Optimizer(model.parameters(), conf["optim"])
        if conf["model"]["use_crf"]:
            loss = CustomLoss(model.crf)
        else:
            loss = WeightedCEL(dset.tag_vocab)
        lr_sch = LR_scheduler(opt.opt, conf["optim"])

        # To track early stopping
        self.best = {"val": {"f1": 0}, "test": {}}
        step, stop = 0, 0

        # Tags to ignore in metrics
        ign_tok = [
            dset.tag_vocab["<p>"], dset.tag_vocab["<s>"],
            dset.tag_vocab["</s>"]
        ]

        for ep in range(conf["train"]["max_epochs"]):
            print("\n\tEpoch %d" % ep)
            for batch in train:
                # set the in training mode.
                model.train()
                # advance step
                step += 1
                # forward pass
                x, y, mask = self.fw_pass(model, batch)
                # measure error
                fw_loss = loss(x, y, mask)
                # backward pass
                opt.train_op(fw_loss)

                # validation
                if step % conf["train"]["val_steps"] == 0:
                    # Set the in testing mode
                    model.eval()
                    # Eval on val set
                    val_metrics = utils.ner_fw_eval(model, self.fw_pass, val,
                                                    step, ign_tok)
                    if val_metrics["f1"] > self.best["val"]["f1"]:
                        # reset Early stop
                        stop = 0
                        # Eval on test set
                        test_metrics = utils.ner_fw_eval(
                            model, self.fw_pass, test, step, ign_tok)
                        self.best = {"val": val_metrics, "test": test_metrics}
                        if save:
                            model.save(step, conf, self.best, opt, lr_sch,
                                       "ner")
                    else:
                        if stop == conf["train"]["patience"]:
                            return
                        stop += 1
                # maybe update lr
                lr_sch.step()