print(f"Skipping step {step}") continue print(idx, "translating", fname) model = os.path.join(mdl_dir, fname) output_pfx = f'bms{args.bms}.blk{args.blk}.bsz{args.bsz}' if args.weights: output_pfx += f'.wgt{args.weights}'.replace(' ', '') output_pfx += f'.{"small" if args.small else "full"}' output = os.path.join(gns_dir, f'{output_pfx}-step_{step}.txt') log_file = os.path.join(exp_dir, 'translate-log.txt') cmd_args = [ f'-model {model}', f'-src {src}', f'-tgt {tgt}', f'-output {output}', f'-beam_size {args.bms}', f'-block_ngram_repeat {args.blk}', f'-batch_size {args.bsz}', f'-gpu {args.gpu}', f'-log_file {log_file}' ] if args.weights: weights = ' '.join([str(w) for w in args.weights]) cmd_args.append(f'-rnn_weights {weights}') translate(f'--config translate.cfg {" ".join(cmd_args)}')
from onmt.bin.train import main as train if __name__ == '__main__': parser = ArgumentParser() # Simply add an argument for preprocess, train, translate mode = parser.add_mutually_exclusive_group() mode.add_argument("--preprocess", dest='preprocess', action='store_true', help="Activate to preprocess with OpenNMT") mode.add_argument("--train", dest='train', action='store_true', help="Activate to train with OpenNMT") mode.add_argument("--translate", dest='translate', action='store_true', help="Activate to translate with OpenNMT") mode, remaining_args = parser.parse_known_args() if mode.preprocess: preprocess(remaining_args) elif mode.train: train(remaining_args) elif mode.translate: args = translate(remaining_args) # TODO compute scores directly after the translation is done
avg = sys.argv[5] == "True" test = sys.argv[6] == "True" sys.argv = [ sys.argv[0], "--config", "config-seed-{}/translate_{}.cfg".format(seed, exp) ] logger.info("seed-{} exp-{} start-{} end-{}".format(seed, exp, start, end)) midstr = "_avg" if avg else "" gens = "test" if test else "valid" data = "test" if test else "validation" for i in range(start, end + 1): steps = i * 1000 parser = _get_parser() opt = parser.parse_args() opt.src = "data/{}_{}_data.txt".format( exp if "S4" in exp else exp[2:4], data) opt.output = "experiments/exp-seed-{}/exp-{}/gens/{}/predictions{}_{}.txt".format( seed, exp, gens, midstr, steps) opt.models = [ "experiments/exp-seed-{}/exp-{}/models/model{}_step_{}.pt".format( seed, exp, midstr, steps) ] opt.log_file = "experiments/exp-seed-{}/exp-{}/translation{}-log.txt".format( seed, exp, midstr, steps) tag = prepare_model(seed, exp, i, avg, i == end) if tag: translate(opt) clear_translate_model(seed, exp, i, avg) else: logger.info("translate error n={}".format(i))