def main(): args = get_arguments() print("initializing device ... ", end="", file=sys.stderr, flush=True) dev = D.Naive() if args.gpu < 0 else D.CUDA(args.gpu) Device.set_default(dev) print("done.", file=sys.stderr) mode = args.mode prefix = args.model if mode == "train": encdec = EncoderDecoder(args.dropout) encdec.init(args.src_vocab, args.trg_vocab, args.embed, args.hidden) optimizer = O.Adam() optimizer.set_weight_decay(1e-6) optimizer.set_gradient_clipping(5) train(encdec, optimizer, args, 1e10) elif mode == "resume": print("loading model/optimizer ... ", end="", file=sys.stderr, flush=True) encdec = EncoderDecoder(args.dropout) encdec.load(prefix + ".model") optimizer = O.Adam() optimizer.load(prefix + ".optimizer") valid_ppl = load_ppl(prefix + ".valid_ppl") print("done.", file=sys.stderr) train(encdec, optimizer, args, valid_ppl) else: print("loading model ... ", end="", file=sys.stderr, flush=True) encdec = EncoderDecoder(args.dropout) encdec.load(prefix + ".model") print("done.", file=sys.stderr) test(encdec, args)
def main(): parser = ArgumentParser() parser.add_argument("mode", help="(train|resume|test)") parser.add_argument("model_prefix", help="prefix of the model files.") args = parser.parse_args() mode = args.mode prefix = args.model_prefix print("mode:", mode, file=sys.stderr) print("prefix:", prefix, file=sys.stderr) if mode not in ("train", "resume", "test"): print("unknown mode:", mode, file=sys.stderr) return print("initializing device ... ", end="", file=sys.stderr) sys.stderr.flush() dev = D.CUDA(0) Device.set_default(dev) print("done.", file=sys.stderr) if mode == "train": encdec = AttentionalEncoderDecoder() encdec.init(SRC_VOCAB_SIZE, TRG_VOCAB_SIZE, NUM_EMBED_UNITS, NUM_HIDDEN_UNITS) optimizer = O.Adam() optimizer.set_weight_decay(1e-6) optimizer.set_gradient_clipping(5) train(encdec, optimizer, prefix, 1e10) elif mode == "resume": print("loading model/optimizer ... ", end="", file=sys.stderr) sys.stderr.flush() encdec = AttentionalEncoderDecoder() encdec.load(prefix + ".model") optimizer = O.Adam() optimizer.load(prefix + ".optimizer") valid_ppl = load_ppl(prefix + ".valid_ppl") print("done.", file=sys.stderr) train(encdec, optimizer, prefix, valid_ppl) else: print("loading model ... ", end="", file=sys.stderr) sys.stderr.flush() encdec = AttentionalEncoderDecoder() encdec.load(prefix + ".model") print("done.", file=sys.stderr) test(encdec)
def main(): parser = ArgumentParser() parser.add_argument("mode") parser.add_argument("model_prefix") args = parser.parse_args() mode = args.mode prefix = args.model_prefix print("mode:", mode, file=sys.stderr) print("prefix:", prefix, file=sys.stderr) if mode not in ("train", "resume", "test"): print("unknown mode:", mode, file=sys.stderr) return print("initializing device ... ", end="", file=sys.stderr) sys.stderr.flush() dev = D.Naive() # = D.CUDA(0) Device.set_default(dev) print("done.", file=sys.stderr) if mode == "train": encdec = EncoderDecoder("encdec", SRC_VOCAB_SIZE, TRG_VOCAB_SIZE, NUM_EMBED_UNITS, NUM_HIDDEN_UNITS, DROPOUT_RATE) trainer = T.Adam() trainer.set_weight_decay(1e-6) trainer.set_gradient_clipping(5) train(encdec, trainer, prefix, 1e10) elif mode == "resume": print("loading model/trainer ... ", end="", file=sys.stderr) sys.stderr.flush() encdec = EncoderDecoder.load("encdec", prefix + '.') trainer = T.Adam() trainer.load(prefix + ".trainer.config") valid_ppl = load_ppl(prefix + ".valid_ppl.config") print("done.", file=sys.stderr) train(encdec, trainer, prefix, valid_ppl) else: # mode == "test" print("loading model ... ", end="", file=sys.stderr) sys.stderr.flush() encdec = EncoderDecoder.load("encdec", prefix + '.') print("done.", file=sys.stderr) test(encdec)