def test_beam(self): src_voc = Vocabulary() trg_voc = Vocabulary() for tok in "</s> I am Philip You are a".split(): src_voc[tok] for tok in "</s> 私 は フィリップ です 1 2 3".split(): trg_voc[tok] model = EncDecNMT(Args("attn"), src_voc, trg_voc, optimizer=optimizers.SGD()) model_out = "/tmp/model-nmt.temp" X, Y = src_voc, trg_voc # Train with 1 example src = np.array([[X["I"], X["am"], X["Philip"]]], dtype=np.int32) trg = np.array([[Y["私"], Y["は"], Y["フィリップ"], Y["です"]]], dtype=np.int32) model.train(src, trg) # Save serializer = ModelSerializer(model_out) serializer.save(model) # Load model1 = EncDecNMT(InitArgs(model_out)) k = model.classify(src, beam=10)
def save_model(epoch): out_file = args.model_out if args.save_models: out_file += "-" + str(epoch) UF.trace("saving model to " + out_file + "...") serializer = ModelSerializer(out_file) serializer.save(model)
def test_NMT_2_read_write(self): for model in ["encdec", "attn"]: src_voc = Vocabulary() trg_voc = Vocabulary() for tok in "</s> I am Philip".split(): src_voc[tok] for tok in "</s> 私 は フィリップ です".split(): trg_voc[tok] model = EncDecNMT(Args(model), src_voc, trg_voc, optimizer=optimizers.SGD()) model_out = "/tmp/nmt/tmp" X, Y = src_voc, trg_voc # Train with 1 example src = np.array([[X["I"], X["am"], X["Philip"]]], dtype=np.int32) trg = np.array([[Y["私"], Y["は"], Y["フィリップ"], Y["です"]]], dtype=np.int32) model.train(src, trg) # Save serializer = ModelSerializer(model_out) serializer.save(model) # Load model1 = EncDecNMT(InitArgs(model_out)) # Check self.assertModelEqual(model._model, model1._model)
def __init__(self, args, X=None, Y=None, optimizer=None, use_gpu=-1, collect_output=False, activation=F.tanh): ## We only instantitate the descendant of this class assert hasattr(self, "_all_models"), "Shoudln't instantitate this class." ## Default configuration self._opt = optimizer self._xp, use_gpu = UF.setup_gpu(use_gpu) self._collect_output = collect_output self._gpu_id = use_gpu self._train_state = self._train_state = { "loss": 150, "epoch": 0 } ## Loading Classifier if args.init_model: serializer = ModelSerializer(args.init_model) serializer.load(self, self._all_models, xp=self._xp) self._model.report(sys.stderr, verbosity=1) else: args.input = len(X) args.output = len(Y) self._model = UF.select_model(args.model, self._all_models)(X, Y, args, xp=self._xp) ## Use GPU or not? if use_gpu >= 0: self._model = self._model.to_gpu(use_gpu) ## Setup Optimizer if optimizer is not None: self._opt.setup(self._model) if args.init_model: serializer.load_optimizer(self._opt)
def test_read_write(self): model = "/tmp/chainer-test/text/model.temp" X, Y = self.src_voc, self.trg_voc # Train with 1 example inp = np.array([[X["Philip"], X["I"]]], dtype=np.int32) out = np.array([[Y["フィリップ"], Y["私"]]], dtype=np.int32) self.model.train(inp, out) # Save serializer = ModelSerializer(model) serializer.save(self.model) # Load model1 = ParallelTextClassifier(InitArgs(model)) # Check self.assertModelEqual(self.model._model, model1._model)
def test_read_write(self): model = "/tmp/rnn.temp" serializer = ModelSerializer(model) serializer._init_dir() serializer._write_model(self.model) model1 = serializer._read_model(self.Model) self.assertModelEqual(self.model, model1)