Ejemplo n.º 1
0
def train(path):
    # test saving pipeline
    save_pipe(path)

    # Trainer
    trainer = Trainer(model=model,
                      train_data=train_data,
                      dev_data=dev_data,
                      loss=ParserLoss(),
                      metrics=ParserMetric(),
                      metric_key='UAS',
                      **train_args.data,
                      optimizer=fastNLP.Adam(**optim_args.data),
                      save_path=path)

    # model.word_embedding = torch.nn.Embedding.from_pretrained(embed, freeze=False)
    model.word_embedding.padding_idx = word_v.padding_idx
    model.word_embedding.weight.data[word_v.padding_idx].fill_(0)
    model.pos_embedding.padding_idx = pos_v.padding_idx
    model.pos_embedding.weight.data[pos_v.padding_idx].fill_(0)

    # try:
    #     ModelLoader.load_pytorch(model, "./save/saved_model.pkl")
    #     print('model parameter loaded!')
    # except Exception as _:
    #     print("No saved model. Continue.")
    #     pass

    # Start training
    trainer.train()
    print("Training finished!")

    # save pipeline
    save_pipe(path)
    print('pipe saved')
Ejemplo n.º 2
0
 def test_train2(self):
     model = BiaffineParser(init_embed=(VOCAB_SIZE, 10),
                            pos_vocab_size=VOCAB_SIZE, pos_emb_dim=10,
                            rnn_hidden_size=16,
                            arc_mlp_size=10,
                            label_mlp_size=10,
                            num_label=NUM_CLS, encoder='transformer')
     ds = prepare_parser_data()
     RUNNER.run_model(model, ds, loss=ParserLoss(), metrics=ParserMetric())
Ejemplo n.º 3
0
def train(path):
    # test saving pipeline
    save_pipe(path)
    embed = EmbedLoader.fast_load_embedding(model_args['word_emb_dim'],
                                            emb_file_name, word_v)
    embed = torch.tensor(embed, dtype=torch.float32)

    # embed = EmbedLoader.fast_load_embedding(emb_dim=model_args['word_emb_dim'], emb_file=emb_file_name, vocab=word_v)
    # embed = torch.tensor(embed, dtype=torch.float32)
    # model.word_embedding = torch.nn.Embedding.from_pretrained(embed, freeze=True)
    model.word_embedding.padding_idx = word_v.padding_idx
    model.word_embedding.weight.data[word_v.padding_idx].fill_(0)
    model.pos_embedding.padding_idx = pos_v.padding_idx
    model.pos_embedding.weight.data[pos_v.padding_idx].fill_(0)

    class MyCallback(Callback):
        def on_step_end(self, optimizer):
            step = self.trainer.step
            # learning rate decay
            if step > 0 and step % 1000 == 0:
                for pg in optimizer.param_groups:
                    pg['lr'] *= 0.93
                print('decay lr to {}'.format(
                    [pg['lr'] for pg in optimizer.param_groups]))

            if step == 3000:
                # start training embedding
                print('start training embedding at {}'.format(step))
                model = self.trainer.model
                for m in model.modules():
                    if isinstance(m, torch.nn.Embedding):
                        m.weight.requires_grad = True

    # Trainer
    trainer = Trainer(model=model,
                      train_data=train_data,
                      dev_data=dev_data,
                      loss=ParserLoss(),
                      metrics=ParserMetric(),
                      metric_key='UAS',
                      **train_args.data,
                      optimizer=fastNLP.Adam(**optim_args.data),
                      save_path=path,
                      callbacks=[MyCallback()])

    # Start training
    try:
        trainer.train()
        print("Training finished!")
    finally:
        # save pipeline
        save_pipe(path)
        print('pipe saved')
Ejemplo n.º 4
0
 def test_train(self):
     ds, v1, v2, v3 = init_data()
     model = BiaffineParser(word_vocab_size=len(v1),
                            word_emb_dim=30,
                            pos_vocab_size=len(v2),
                            pos_emb_dim=30,
                            num_label=len(v3))
     trainer = fastNLP.Trainer(model=model,
                               train_data=ds,
                               dev_data=ds,
                               loss=ParserLoss(),
                               metrics=ParserMetric(),
                               metric_key='UAS',
                               n_epochs=10,
                               use_cuda=False,
                               use_tqdm=False)
     trainer.train(load_best_model=False)