def train(self, train_path, save_path=DEFAULT_CONFIG['save_path'], dev_path=None, vectors_path=None, log_dir=None, **kwargs):
    writer = SummaryWriter(log_dir=log_dir)
    train_dataset = lm_tool.get_dataset(train_path)
    if dev_path:
        dev_dataset = lm_tool.get_dataset(dev_path)
        word_vocab = lm_tool.get_vocab(train_dataset, dev_dataset)
    else:
        word_vocab = lm_tool.get_vocab(train_dataset)
    self._word_vocab = word_vocab
    config = LMConfig(word_vocab, save_path=save_path, vector_path=vectors_path, **kwargs)
    train_iter = lm_tool.get_iterator(train_dataset, batch_size=config.batch_size, bptt_len=config.bptt_len)
    rnnlm = RNNLM(config)
    self._model = rnnlm
    optim = torch.optim.Adam(rnnlm.parameters(), lr=config.lr)
    for epoch in range(config.epoch):
        rnnlm.train()
        acc_loss = 0
        for item in tqdm(train_iter):
            optim.zero_grad()
            logits = rnnlm(item.text)
            item_loss = F.cross_entropy(logits, item.target.view((- 1)))
            acc_loss += item_loss.item()
            item_loss.backward()
            optim.step()
        logger.info('epoch: {}, acc_loss: {}'.format(epoch, acc_loss))
        writer.add_scalar('lm_train/acc_loss', acc_loss, epoch)
        if dev_path:
            dev_score = self._validate(dev_dataset)
            logger.info('dev score:{}'.format(dev_score))
            writer.add_scalar('lm_train/dev_score', dev_score, epoch)
        writer.flush()
        adjust_learning_rate(optim, (config.lr / (1 + ((epoch + 1) * config.lr_decay))))
    writer.close()
    config.save()
    rnnlm.save()
def train(self, train_path, save_path=DEFAULT_CONFIG['save_path'], dev_path=None, vectors_path=None, log_dir=None, **kwargs):
    writer = SummaryWriter(log_dir=log_dir)
    train_dataset = lm_tool.get_dataset(train_path)
    if dev_path:
        dev_dataset = lm_tool.get_dataset(dev_path)
        word_vocab = lm_tool.get_vocab(train_dataset, dev_dataset)
    else:
        word_vocab = lm_tool.get_vocab(train_dataset)
    self._word_vocab = word_vocab
    config = LMConfig(word_vocab, save_path=save_path, vector_path=vectors_path, **kwargs)
    train_iter = lm_tool.get_iterator(train_dataset, batch_size=config.batch_size, bptt_len=config.bptt_len)
    rnnlm = RNNLM(config)
    self._model = rnnlm
    optim = torch.optim.Adam(rnnlm.parameters(), lr=config.lr)
    for epoch in range(config.epoch):
        rnnlm.train()
        acc_loss = 0
        for item in tqdm(train_iter):
            optim.zero_grad()
            logits = rnnlm(item.text)
            item_loss = F.cross_entropy(logits, item.target.view((- 1)))
            acc_loss += item_loss.item()
            item_loss.backward()
            optim.step()
        logger.info('epoch: {}, acc_loss: {}'.format(epoch, acc_loss))
        writer.add_scalar('lm_train/acc_loss', acc_loss, epoch)
        if dev_path:
            dev_score = self._validate(dev_dataset)
            logger.info('dev score:{}'.format(dev_score))
            writer.add_scalar('lm_train/dev_score', dev_score, epoch)
        writer.flush()
        adjust_learning_rate(optim, (config.lr / (1 + ((epoch + 1) * config.lr_decay))))
    writer.close()
    config.save()
    rnnlm.save()