Esempio n. 1
0
def stat(opt):

    opt.experiment = os.path.join(root_dir, opt.experiment)
    opt.load_chkpt = os.path.join(opt.experiment, opt.save_model)
    opt.test_file = os.path.join(opt.data_root, opt.test_file)
    opt.log_path = os.path.join(opt.experiment, 'stat.info')
    opt.logger = make_logger(opt.log_path)

    # Model loading
    model = make_model(opt)
    chkpt = torch.load(opt.load_chkpt,
                       map_location=lambda storage, log: storage)
    model.load_state_dict(chkpt)
    if opt.cuda:
        model = model.cuda()

    # ====== *********************** ================
    model.eval()
    # ===============================================

    # training procedure
    trainers = []
    trainers.append(ActTrainer(model, None, None, opt.logger, cuda=opt.cuda))
    trainers.append(SlotTrainer(model, None, None, opt.logger, cuda=opt.cuda))
    trainers.append(ValueTrainer(model, None, None, opt.logger, cuda=opt.cuda))
    trainers.append(
        SLUTrainer(model, (None, None), None, opt.logger, cuda=opt.cuda))

    for trainer in trainers:
        metric = trainer.valid_on_epoch(0, opt.test_file, opt.memory)

    print('Stat results saved in {}'.format(opt.log_path))
Esempio n. 2
0
def train(opt):

    # basics definition
    opt.experiment = os.path.join(root_dir, opt.experiment)
    if not os.path.exists(opt.experiment):
        os.makedirs(opt.experiment)
    opt.save_model = os.path.join(opt.experiment, opt.save_model)
    opt.log_path = os.path.join(opt.experiment, 'log.train')
    opt.logger = make_logger(opt.log_path)

    bert_tokenizer, bert_model = make_bert()

    # dataIter definition
    class2idx = build_class_vocab(opt.data_root + 'class.all')
    opt.class_size = len(class2idx)
    train_iter = BertIter4STC(opt.data_root + 'train', bert_tokenizer,
                              class2idx, opt.batch_size, opt.cuda, True)
    valid_iter = BertIter4STC(opt.data_root + 'valid', bert_tokenizer,
                              class2idx, opt.batch_size, opt.cuda, False)

    # model definition
    model = make_model(opt, bert_model)

    # criterion definition
    criterion = nn.BCELoss(reduction='sum')
    if opt.cuda:
        criterion = criterion.cuda()

    # optimizer definition
    if opt.fix_bert:
        for (name, parameter) in model.bert.named_parameters():
            parameter.requires_grad = False

    if opt.optim == 'bert':
        params = list(
            filter(lambda x: x[1].requires_grad == True,
                   model.named_parameters()))
        print('Trainable parameter number: {}'.format(len(params)))
        print('Trainer: bert')
        no_decay = ['bias', 'gamma', 'beta']
        grouped_params = [{
            'params': [p for n, p in params if n not in no_decay],
            'weight_decay_rate': 0.01
        }, {
            'params': [p for n, p in params if n in no_decay],
            'weight_decay_rate': 0.0
        }]
        optimizer = BertAdam(grouped_params,
                             opt.lr,
                             warmup=0.1,
                             t_total=len(train_iter) * opt.epochs)
    else:
        optimizer = Optim(opt.optim, opt.lr, max_grad_norm=opt.max_norm)
        optimizer.set_parameters(model.named_parameters())
        print('Trainable parameter number: {}'.format(len(optimizer.params)))

    # training procedure
    trainer = BertTrainer4STC(model, criterion, optimizer, opt.logger)
    trainer.train(opt.epochs, train_iter, valid_iter, opt.save_model)
Esempio n. 3
0
def train(opt):

    opt.experiment = os.path.join(root_dir, opt.experiment)
    if not os.path.exists(opt.experiment):
        os.makedirs(opt.experiment)

    opt.save_model = os.path.join(opt.experiment, opt.save_model)
    if opt.load_model is not None:
        opt.load_model = os.path.join(opt.experiment, opt.load_model)
    opt.log_path = os.path.join(opt.experiment, 'log.train')
    opt.logger = make_logger(opt.log_path)

    # memory info
    print("encoder word2idx number: {}".format(opt.enc_word_vocab_size))
    print("decoder word2idx number: {}".format(opt.dec_word_vocab_size))

    # Model definition
    model = make_model(opt)

    if opt.load_word_emb:
        if opt.enlarge_word_vocab:
            enc_emb = opt.memory['word2idx_w_glove_emb']
        else:
            enc_emb = opt.memory['word2idx_emb']
        dec_emb = opt.memory['word2idx_emb']
        model.enc_word_emb.init_weight_from_pre_emb(enc_emb, opt.fix_word_emb)
        model.dec_word_emb.init_weight_from_pre_emb(dec_emb, opt.fix_word_emb)

    if opt.enc_word_vocab_size == opt.dec_word_vocab_size:
        model.dec_word_emb.embedding.weight.data = model.enc_word_emb.embedding.weight.data
        model.dec_word_emb.embedding.weight.requires_grad = model.enc_word_emb.embedding.weight.requires_grad

    if opt.load_model is not None:
        chkpt = torch.load(opt.load_model, map_location = lambda storage, log: storage)
        model.load_state_dict(chkpt)
    if opt.cuda:
        model = model.cuda()
    print(model)

    # optimizer details
    optimizer = Optim(opt.optim, opt.lr, max_grad_norm=opt.max_norm)
    optimizer.set_parameters(model.named_parameters())
    print("training parameters number: {}".format(len(optimizer.params)))

    nll_criterion = nn.NLLLoss(reduction='sum')
    if opt.cuda:
        nll_criterion = nll_criterion.cuda()

    # training procedure
    train_iter = DADataset(opt.data_root + opt.train_file, opt.memory, opt.cuda, True)
    valid_iter = DADataset(opt.data_root + opt.valid_file, opt.memory, opt.cuda, False)

    trainer = DATrainer(model, nll_criterion, optimizer, opt.logger, cuda=opt.cuda)
    trainer.train(opt.epochs, opt.batch_size, train_iter, valid_iter, opt.save_model)
Esempio n. 4
0
def train():

    # Initialization
    opt = parse_args()
    opt.experiment = os.path.join(root_dir, opt.experiment)
    if not os.path.exists(opt.experiment):
        os.makedirs(opt.experiment)
    opt.save_model = os.path.join(opt.experiment, opt.save_model)
    opt.log_path = os.path.join(opt.experiment, 'log.train')
    opt.logger = make_logger(opt.log_path)

    # Data iterator definition
    train_iter = DataLoader(opt.train_file,
                            opt.word2idx,
                            opt.batch_size,
                            opt.cuda,
                            epoch_shuffle=True)
    valid_iter = DataLoader(opt.valid_file,
                            opt.word2idx,
                            opt.batch_size,
                            opt.cuda,
                            epoch_shuffle=False)

    # Model definition
    model = make_model(len(opt.word2idx))
    if opt.gpuid >= 0:
        model = model.cuda()
    #print(model)
    tally_parameters(model)

    optimizer = Optim('Adam', 0.001, max_grad_norm=5)
    optimizer.set_parameters(model.named_parameters())

    criterion = nn.NLLLoss(reduction='sum')
    if opt.gpuid >= 0:
        criterion = criterion.cuda()

    trainer = Trainer(model, criterion, optimizer, opt.logger, opt.cuda)
    trainer.train(opt.epochs, train_iter, valid_iter, opt.save_model)
Esempio n. 5
0
def train(opt):

    # basics definition
    opt.experiment = os.path.join(root_dir, opt.experiment)
    if not os.path.exists(opt.experiment):
        os.makedirs(opt.experiment)
    opt.save_model = os.path.join(opt.experiment, opt.save_model)
    opt.log_path = os.path.join(opt.experiment, 'log.train')
    opt.logger = make_logger(opt.log_path)

    # dataIter definition
    train_iter = OneBestIter4STC(opt.data_root+'train', opt.word2idx, opt.class2idx, 
            opt.batch_size, opt.cuda, True)
    valid_iter = OneBestIter4STC(opt.data_root+'valid', opt.word2idx, opt.class2idx, 
            opt.batch_size, opt.cuda, False)

    # model definition
    model = make_model(opt)
    if opt.load_emb:
        emb = read_emb(opt.word2idx)
        model.emb.init_weight_from_pre_emb(emb, opt.fix_emb)
    print(model)

    # criterion definition
    criterion = nn.BCELoss(reduction='sum')
    if opt.cuda:
        criterion = criterion.cuda()

    # optimizer definition
    optimizer = Optim(opt.optim, opt.lr, max_grad_norm=opt.max_norm)
    optimizer.set_parameters(model.named_parameters())
    print('Trainable parameter number: {}'.format(len(optimizer.params)))

    # training procedure
    trainer = OneBestTrainer4STC(model, criterion, optimizer, opt.logger)
    trainer.train(opt.epochs, train_iter, valid_iter, opt.save_model)
Esempio n. 6
0
def train(opt):

    opt.experiment = os.path.join(root_dir, opt.experiment)
    if not os.path.exists(opt.experiment):
        os.makedirs(opt.experiment)

    opt.save_model = os.path.join(opt.experiment, opt.save_model)
    opt.log_path = os.path.join(opt.experiment, 'log.train')
    opt.logger = make_logger(opt.log_path)

    # memory info
    print("encoder word2idx number: {}".format(opt.enc_word_vocab_size))
    print("decoder word2idx number: {}".format(opt.dec_word_vocab_size))
    print("act2idx number: {}".format(opt.act_vocab_size))
    print("slot2idx number: {}".format(opt.slot_vocab_size))

    # Model definition
    model = make_model(opt)
    if opt.load_word_emb:
        emb = read_emb(opt.memory['enc2idx'])
        model.enc_word_emb.init_weight_from_pre_emb(emb, opt.fix_word_emb)
    if opt.load_class_emb:
        emb = opt.memory['act_emb']
        model.act_emb.init_weight_from_pre_emb(emb, opt.fix_class_emb)
        emb = opt.memory['slot_emb']
        model.slot_emb.init_weight_from_pre_emb(emb, opt.fix_class_emb)
    if opt.share_param:
        #model.value_decoder.outlin.weight.data = model.word_emb.embedding.weight.data
        #model.value_decoder.outlin.weight.requires_grad = model.word_emb.embedding.weight.requires_grad
        model.act_stc.lin.weight.data = model.act_emb.embedding.weight.data
        model.act_stc.lin.weight.requires_grad = model.act_emb.embedding.weight.requires_grad
        model.slot_stc.lin.weight.data = model.slot_emb.embedding.weight.data
        model.slot_stc.lin.weight.requires_grad = model.slot_emb.embedding.weight.requires_grad
    if opt.cuda:
        model = model.cuda()
    print(model)

    # optimizer details
    optimizer = Optim(opt.optim, opt.lr, max_grad_norm=opt.max_norm)
    optimizer.set_parameters(model.named_parameters())
    print("training parameters number: {}".format(len(optimizer.params)))

    # loss definition
    #stc_criterion = MaskedBCELoss(opt.cuda)
    stc_criterion = nn.BCELoss(reduction='sum')
    nll_criterion = nn.NLLLoss(reduction='sum')
    if opt.cuda:
        stc_criterion = stc_criterion.cuda()
        nll_criterion = nll_criterion.cuda()

    # training procedure
    if opt.task == 'slu':
        data_iter = SLUDataset(opt.data_root + 'train', opt.memory, opt.cuda,
                               True)
        trainer = SLUTrainer(model, (stc_criterion, nll_criterion),
                             optimizer,
                             opt.logger,
                             cuda=opt.cuda)
    elif opt.task == 'act':
        data_iter = ActDataset(opt.data_root + 'train', opt.memory, opt.cuda,
                               True)
        trainer = ActTrainer(model,
                             stc_criterion,
                             optimizer,
                             opt.logger,
                             cuda=opt.cuda)
    elif opt.task == 'slot':
        data_iter = SlotDataset(opt.data_root + 'train', opt.memory, opt.cuda,
                                True)
        trainer = SlotTrainer(model,
                              stc_criterion,
                              optimizer,
                              opt.logger,
                              cuda=opt.cuda)
    elif opt.task == 'value':
        data_iter = ValueDataset(opt.data_root + 'train', opt.memory, opt.cuda,
                                 True)
        trainer = ValueTrainer(model,
                               nll_criterion,
                               optimizer,
                               opt.logger,
                               cuda=opt.cuda)

    trainer.train(opt.epochs, opt.batch_size, opt.memory, data_iter,
                  opt.data_root + 'valid', opt.save_model)