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 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)