示例#1
0
def validate(args, device_id, pt, step):
    device = "cpu" if args.visible_gpus == '-1' else "cuda"
    if (pt != ''):
        test_from = pt
    else:
        test_from = args.test_from
    logger.info('Loading checkpoint from %s' % test_from)
    checkpoint = torch.load(test_from, map_location=lambda storage, loc: storage)


    opt = vars(checkpoint['opt'])

    for k in opt.keys():
        if (k in model_flags):
            setattr(args, k, opt[k])
    print(args)

    spm = sentencepiece.SentencePieceProcessor()
    spm.Load(args.vocab_path)
    word_padding_idx = spm.PieceToId('<PAD>')
    symbols = {'BOS': spm.PieceToId('<S>'), 'EOS': spm.PieceToId('</S>'), 'PAD': word_padding_idx,
               'EOT': spm.PieceToId('<T>'), 'EOP': spm.PieceToId('<P>'), 'EOQ': spm.PieceToId('<Q>')}

    vocab_size = len(spm)
    model = Summarizer(args, word_padding_idx, vocab_size, device, checkpoint)
    model.eval()

    valid_iter = data_loader.AbstractiveDataloader(args, load_dataset(args, 'valid', shuffle=False), symbols,
                                                   args.batch_size, device, shuffle=False, is_test=False)

    trainer = build_trainer(args, device_id, model, symbols, vocab_size, None)
    stats = trainer.validate(valid_iter)
    trainer._report_step(0, step, valid_stats=stats)
    return stats.ppl()
示例#2
0
def test(args, pt, step):
    device = "cpu" if args.visible_gpus == '-1' else "cuda"

    if (pt != ''):
        test_from = pt
    else:
        test_from = args.test_from
    logger.info('Loading checkpoint from %s' % test_from)
    checkpoint = torch.load(test_from, map_location=lambda storage, loc: storage)
    opt = vars(checkpoint['opt'])

    for k in opt.keys():
        if (k in model_flags):
            setattr(args, k, opt[k])
    print(args)

    spm = sentencepiece.SentencePieceProcessor()
    spm.Load(args.vocab_path)
    word_padding_idx = spm.PieceToId('<PAD>')
    symbols = {'BOS': spm.PieceToId('<S>'), 'EOS': spm.PieceToId('</S>'), 'PAD': word_padding_idx,
               'EOT': spm.PieceToId('<T>'), 'EOP': spm.PieceToId('<P>'), 'EOQ': spm.PieceToId('<Q>')}

    vocab_size = len(spm)
    vocab = spm
    model = Summarizer(args, word_padding_idx, vocab_size, device, checkpoint)
    model.eval()

    test_iter = data_loader.AbstractiveDataloader(args, load_dataset(args, args.dataset, shuffle=False), symbols,
                                                  args.valid_batch_size, device, shuffle=False, is_test=True)
    predictor = build_predictor(args, vocab, symbols, model, logger=logger)
    predictor.translate(test_iter, step)
示例#3
0
def train(args, device_id):
    init_logger(args.log_file)
    logger.info(str(args))

    device = "cpu" if args.visible_gpus == '-1' else "cuda"
    logger.info('Device ID %d' % device_id)
    logger.info('Device %s' % device)
    torch.manual_seed(args.seed)
    random.seed(args.seed)
    torch.backends.cudnn.deterministic = True

    if device_id >= 0:
        torch.cuda.set_device(device_id)
        torch.cuda.manual_seed(args.seed)

    if args.train_from != '':
        logger.info('Loading checkpoint from %s' % args.train_from)
        checkpoint = torch.load(args.train_from,
                                map_location=lambda storage, loc: storage)
        opt = vars(checkpoint['opt'])
        for k in opt.keys():
            if (k in model_flags):
                setattr(args, k, opt[k])
        ckpt_step = args.train_from.strip('.pt').split('_')[-1]
    else:
        checkpoint = None

    spm = sentencepiece.SentencePieceProcessor()
    spm.Load(args.vocab_path)
    word_padding_idx = spm.PieceToId('<PAD>')
    symbols = {
        'BOS': spm.PieceToId('<S>'),
        'EOS': spm.PieceToId('</S>'),
        'PAD': word_padding_idx,
        'EOT': spm.PieceToId('<T>'),
        'EOP': spm.PieceToId('<P>'),
        'EOQ': spm.PieceToId('<Q>')
    }
    print(symbols)
    vocab_size = len(spm)

    def train_iter_fct():
        return data_loader.AbstractiveDataloader(args,
                                                 load_dataset(args,
                                                              'train',
                                                              shuffle=True),
                                                 symbols,
                                                 args.batch_size,
                                                 device,
                                                 shuffle=True,
                                                 is_test=False)

    model = Summarizer(args, word_padding_idx, vocab_size, device, checkpoint)
    optim = model_builder.build_optim(args, model, checkpoint)
    if args.train_from != '':
        optim._step = int(ckpt_step)
    logger.info(model)
    trainer = build_trainer(args, device_id, model, symbols, vocab_size, optim)

    trainer.train(train_iter_fct, args.train_steps)
def test(args, pt, step):
    device = "cpu" if args.visible_gpus == '-1' else "cuda"

    if (pt != ''):
        test_from = pt
    else:
        test_from = args.test_from
    logger.info('Loading checkpoint from %s' % test_from)
    checkpoint = torch.load(test_from,
                            map_location=lambda storage, loc: storage)
    opt = vars(checkpoint['opt'])

    for k in opt.keys():
        if (k in model_flags):
            setattr(args, k, opt[k])
    print(args)

    spm = sentencepiece.SentencePieceProcessor()
    spm.Load(args.vocab_path)
    word_padding_idx = spm.PieceToId('<PAD>')
    symbols = {
        'BOS': spm.PieceToId('<S>'),
        'EOS': spm.PieceToId('</S>'),
        'PAD': word_padding_idx,
        'EOT': spm.PieceToId('<T>'),
        'EOP': spm.PieceToId('<P>'),
        'EOQ': spm.PieceToId('<Q>')
    }

    vocab_size = len(spm)
    vocab = spm
    model = Summarizer(args, word_padding_idx, vocab_size, device, checkpoint)
    model.eval()

    data = [
        "President-elect Joe Biden criticized the Trump administration Tuesday for the pace of distributing COVID-19 vaccines and predicted that “things will get worse before they get better” when it comes to the pandemic. Biden encouraged Americans to “steel our spines” for challenges to come and predicted that “things are going to get worse before they get better.” Earlier this month, Trump administration officials said they planned to have 20 million doses of the vaccine distributed by the end of the year. At the current pace, Biden said, “it’s gonna take years, not months, to vaccinate the American people.",
        "The Second Stimulus Answers to Your Questions About the Stimulus Bill Updated Dec 30, 2020 The economic relief package will issue payments of $600 and distribute a federal unemployment benefit of $300 for at least 10 weeks. To receive assistance, households will have to meet several conditions: Household income (for 2020) cannot exceed more than 80 percent of the area median income; at least one household member must be at risk of homelessness or housing instability; and individuals must qualify for unemployment benefits or have experienced financial hardship — directly or indirectly — because of the pandemic. Robert Van Sant’s unemployment benefits of $484 a week don’t cover his monthly expenses of $2,200 in rent, utilities, internet access, food and other necessities.",
        "The majority leader, who effectively ruled out consideration of the House version stimulus check bill, has sought to couple the stimulus boost with unrelated provisions aimed at placating Trump’s demands to address legal protections for tech companies and his unsubstantiated claims of voter fraud in the 2020 election. McConnell said the House bill on the stimulus checks “has no realistic path to quickly pass the Senate,” even as Trump continues to harangue McConnell and GOP leaders over their refusal to go along with his demands. Sen. John Cornyn (R-Texas) predicted the Senate would follow the House and override Trump’s veto of the defense bill, calling it a matter of “how long people want to extend this out.",
        "The Second Stimulus Answers to Your Questions About the Stimulus Bill Updated Dec 30, 2020 The economic relief package will issue payments of $600 and distribute a federal unemployment benefit of $300 for at least 10 weeks. To receive assistance, households will have to meet several conditions: Household income (for 2020) cannot exceed more than 80 percent of the area median income; at least one household member must be at risk of homelessness or housing instability; and individuals must qualify for unemployment benefits or have experienced financial hardship — directly or indirectly — because of the pandemic. Robert Van Sant’s unemployment benefits of $484 a week don’t cover his monthly expenses of $2,200 in rent, utilities, internet access, food and other necessities.",
        "House Armed Services Chair Adam Smith (D-Wash.) told reporters last week that lawmakers’ “only option” if an override fails will be to attempt to pass the same defense agreement after President-elect Joe Biden takes office. Trump has also pushed for lawmakers to use the defense bill to repeal legal protections for social media companies, known as Section 230, but was rebuffed as Republicans and Democrats alike said it fell outside of the Armed Services Committee’s jurisdiction. Both the House and Senate passed a compromise defense bill last week with more than enough votes to overcome a veto, including strong support from Republicans.",
        "Top officials, including Speaker Nancy Pelosi and Mnuchin, are privately discussing contingency plans such as a stopgap spending bill if Trump does formally veto the measure by Monday, when funding is set to lapse. But it’s not clear how long that stopgap measure would last — or whether Trump would sign it, if it makes none of the changes he’s demanded, such as cuts to foreign aid, according to people familiar with the discussions. “Today, on Christmas Eve morning, House Republicans cruelly deprived the American people of the $2,000 that the President agreed to support,” Pelosi said in a statement.",
        "McConnell has also expressed concern that the vote could hurt GOP senators facing tough general election fights by alienating moderate voters. According to multiple people familiar with the discussion, the Senate GOP leader also asked Hawley several times to walk through how his objection would play out. The Missouri senator has focused his objections on Pennsylvania, arguing that it and other states failed to adhere to their own election laws. A Toomey spokesperson confirmed the account, saying: “Sen. Toomey made his views on Senator Hawley’s planned objection clear. Hawley instead sent an email to Senate Republicans after the call wrapped.",
        "President Trump spurned Democrats and Republicans alike on Wednesday as he left Washington defiantly for the holidays, vetoing a major defense bill, imperiling a COVID-19 relief package and setting the stage for a possible government shutdown after Christmas. Mr. Trump vetoed the $740 billion National Defense Authorization Act as promised, citing among his objections that lawmakers didn’t honor his late demand to repeal legal liability protections for big social media companies such as Twitter, Google and Facebook. Senate Armed Services Committee Chairman James Inhofe, Oklahoma Republican, said the president’s valid concerns about Big Tech companies shouldn’t derail the annual defense bill that sets national security priorities."
    ]

    dataset_list = []
    mds = []

    for article in data:
        encoded = spm.Encode(article)
        mds.append(encoded)

    dataset_list = [{"src": mds, "tgt": [], "tgt_str": []}]
    # df = pd.DataFrame(dataset_list)
    # dataset = MultiNewsDataset(df)
    # dataloader = DataLoader(dataset, batch_size=1)

    # load_dataset(args, args.dataset, shuffle=False)
    # test_iter = data_loader.AbstractiveDataloader(args, load_dataset_live(dataset_list), symbols,
    #                                               args.valid_batch_size, device, shuffle=False, is_test=True)

    test_iter = data_loader.AbstractiveDataloader(args,
                                                  load_dataset(args,
                                                               args.dataset,
                                                               shuffle=False),
                                                  symbols,
                                                  args.valid_batch_size,
                                                  device,
                                                  shuffle=False,
                                                  is_test=True)
    predictor = build_predictor(args, vocab, symbols, model, logger=logger)
    predictor.translate(test_iter, step, spm)