示例#1
0
def main():
    args = parse_args()
    beam_width = args.beam_width

    d = builder.initiate(args.model_dir)
    m = d['model']
    src_vcb, trg_vcb = d['vocabularies']
    _, valid_data = d['datasets']

    source, source_variables = get_agiga_example(src_vcb, valid_data,
                                                 args.index)

    generator = BeamSearchGenerator(m, src_vcb, trg_vcb, beam_width=beam_width)

    length = np.array([args.length], dtype=np.int32)
    ret = generator.generate(source_variables, length, byte=True)
    output, score, state, beams = ret
    out = trg_vcb.revert(output, with_tags=False)
    print('{} & {} & {} \\\\'.format(m.name, args.length, ' '.join(out)))
    print(' ', len(out), len(' '.join(out)))

    print('- others')
    for b in beams:
        sent = ' '.join(trg_vcb.revert(b[1], with_tags=False))
        print('  %0.2f' % b[0], len(sent), sent, sep=' & ')
示例#2
0

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('model_dir')
    parser.add_argument("--gpu", type=int, default=-1)
    parser.add_argument("--reset", action='store_true')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()

    if args.reset:
        builder.reset(args.model_dir)
    d = builder.initiate(args.model_dir)
    model = d['model']

    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()
        model.to_gpu()

    trainer = Trainer(model, d['optimizer'], d['vocabularies'], args.model_dir)
    train_data, valid_data = d['datasets']

    b = d['builder']
    batch_size = int(b.config["Training"]["batch_size"])
    batches_for_sort = int(b.config["Training"]["batches_for_sort"])
    epochs = int(b.config["Training"]["epochs"])

    trainer.train(epochs, train_data, valid_data, batch_size, batches_for_sort)