def setUp(self): events.clear() ParamManager.init_param_col() # Load a pre-trained model load_experiment = LoadSerialized(filename=f"test/data/tiny_jaen.model", overwrite=[ { "path": "train", "val": None }, { "path": "status", "val": None }, ]) EXP_DIR = '.' EXP = "decode" uninitialized_experiment = YamlPreloader.preload_obj(load_experiment, exp_dir=EXP_DIR, exp_name=EXP) loaded_experiment = initialize_if_needed(uninitialized_experiment) ParamManager.populate() # Pull out the parts we need from the experiment self.model = loaded_experiment.model src_vocab = self.model.src_reader.vocab trg_vocab = self.model.trg_reader.vocab event_trigger.set_train(False) self.src_data = list( self.model.src_reader.read_sents("test/data/head.ja")) self.trg_data = list( self.model.trg_reader.read_sents("test/data/head.en"))
def run_training(self, save_fct: Callable) -> None: """ Main training loop (overwrites TrainingRegimen.run_training()) """ if self.run_for_epochs is None or self.run_for_epochs > 0: for src, trg in self.next_minibatch(): if self.dev_zero: self.checkpoint_and_save(save_fct) self.dev_zero = False with utils.ReportOnException({ "src": src, "trg": trg, "graph": utils.print_cg_conditional }): dy.renew_cg(immediate_compute=settings.IMMEDIATE_COMPUTE, check_validity=settings.CHECK_VALIDITY) with self.train_loss_tracker.time_tracker: event_trigger.set_train(True) loss_builder = self.training_step(src, trg) loss = loss_builder.compute() self.backward(loss, self.dynet_profiling) self.update(self.trainer) self.train_loss_tracker.report( trg, loss_builder.get_factored_loss_val( comb_method=self.loss_comb_method)) if self.checkpoint_needed(): self.checkpoint_and_save(save_fct) if self.should_stop_training(): break
def test_py_lstm_encoder_len(self): layer_dim = 512 model = DefaultTranslator( src_reader=self.src_reader, trg_reader=self.trg_reader, src_embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), encoder=PyramidalLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, layers=3), attender=MlpAttender(input_dim=layer_dim, state_dim=layer_dim, hidden_dim=layer_dim), decoder=AutoRegressiveDecoder( input_dim=layer_dim, embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), rnn=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, decoder_input_dim=layer_dim, yaml_path="model.decoder.rnn"), transform=NonLinear(input_dim=layer_dim * 2, output_dim=layer_dim), scorer=Softmax(input_dim=layer_dim, vocab_size=100), bridge=CopyBridge(dec_dim=layer_dim, dec_layers=1)), ) event_trigger.set_train(True) for sent_i in range(10): dy.renew_cg() src = self.src_data[sent_i].create_padded_sent( 4 - (self.src_data[sent_i].sent_len() % 4)) event_trigger.start_sent(src) embeddings = model.src_embedder.embed_sent(src) encodings = model.encoder.transduce(embeddings) self.assertEqual(int(math.ceil(len(embeddings) / float(4))), len(encodings))
def perform_inference(self, generator: 'models.GeneratorModel', src_file: str = None, trg_file: str = None) \ -> None: """ Perform inference. Args: generator: the model to be used src_file: path of input src file to be translated trg_file: path of file where trg translatons will be written """ src_file = src_file or self.src_file trg_file = trg_file or self.trg_file utils.make_parent_dir(trg_file) logger.info(f'Performing inference on {src_file}') ref_corpus, src_corpus = self._read_corpus(generator, src_file, mode=self.mode, ref_file=self.ref_file) event_trigger.set_train(False) ref_scores = None if self.mode == 'score': ref_scores = self._compute_losses(generator, ref_corpus, src_corpus, self.max_num_sents) self._write_rescored_output(ref_scores, self.ref_file, trg_file) if self.mode == 'forceddebug': ref_scores = self._compute_losses(generator, ref_corpus, src_corpus, self.max_num_sents) if self.mode != 'score': self._generate_output(generator=generator, forced_ref_corpus=ref_corpus, assert_scores=ref_scores, src_corpus=src_corpus, trg_file=trg_file, batcher=self.batcher, max_src_len=self.max_src_len)
def test_policy(self): event_trigger.set_train(True) self.model.policy_learning = PolicyGradient(input_dim=3 * self.layer_dim) mle_loss = MLELoss() loss = mle_loss.calc_loss(self.model, self.src[0], self.trg[0]) event_trigger.calc_additional_loss(self.trg[0], self.model, loss)
def eval(self) -> 'metrics.EvalScore': """ Perform evaluation task. Returns: Evaluated score """ event_trigger.set_train(False) if self.src_data is None: self.src_data, self.ref_data, self.src_batches, self.ref_batches = \ input_readers.read_parallel_corpus(src_reader=self.model.src_reader, trg_reader=self.model.trg_reader, src_file=self.src_file, trg_file=self.ref_file, batcher=self.batcher, max_num_sents=self.max_num_sents, max_src_len=self.max_src_len, max_trg_len=self.max_trg_len) loss_val = losses.FactoredLossVal() ref_words_cnt = 0 for src, trg in zip(self.src_batches, self.ref_batches): with utils.ReportOnException({"src": src, "trg": trg, "graph": utils.print_cg_conditional}): dy.renew_cg(immediate_compute=settings.IMMEDIATE_COMPUTE, check_validity=settings.CHECK_VALIDITY) loss = self.loss_calculator.calc_loss(self.model, src, trg) ref_words_cnt += sum([trg_i.len_unpadded() for trg_i in trg]) loss_val += loss.get_factored_loss_val(comb_method=self.loss_comb_method) loss_stats = {k: v/ref_words_cnt for k, v in loss_val.items()} # return metrics.LossScore(sum(loss_stats.values()), loss_stats=loss_stats, num_ref_words=ref_words_cnt, desc=self.desc)
def run_training(self, save_fct: Callable) -> None: task_generators = OrderedDict() for task in self.tasks: task_generators[task] = task.next_minibatch() dev_zero = {i: self.dev_zero for i in range(len(self.tasks))} if self.tasks[0].run_for_epochs > 0: while True: tt.reset_graph() cur_task_i = np.random.choice(range(len(self.tasks)), p=self.task_weights) cur_task = self.tasks[cur_task_i] task_gen = task_generators[cur_task] if dev_zero[cur_task_i]: self.checkpoint_and_save(cur_task, cur_task_i, save_fct, dev_zero) with cur_task.train_loss_tracker.time_tracker: for _ in range(self.update_every_within): src, trg = next(task_gen) event_trigger.set_train(True) loss_builder = cur_task.training_step(src, trg) self.backward(loss=loss_builder.compute( comb_method=self.loss_comb_method)) self.update(trainer=self.trainer) cur_task.train_loss_tracker.report( trg, loss_builder.get_factored_loss_val()) self.checkpoint_and_save(cur_task, cur_task_i, save_fct, dev_zero) if self.tasks[0].should_stop_training(): break
def run_training(self, save_fct: Callable) -> None: dev_zero = {i: self.dev_zero for i in range(len(self.tasks))} for cur_task_id in range(len(self.tasks)): self.train = None cur_task = self.tasks[cur_task_id] task_gen = cur_task.next_minibatch() if cur_task.run_for_epochs > 0: while True: tt.reset_graph() src, trg = next(task_gen) if dev_zero[cur_task_id]: self.checkpoint_and_save(cur_task, cur_task_id, save_fct, dev_zero) with cur_task.train_loss_tracker.time_tracker: event_trigger.set_train(True) loss_builder = cur_task.training_step(src, trg) task_loss = loss_builder.compute( comb_method=self.loss_comb_method) self.backward(task_loss) self.update(self.trainer) cur_task.train_loss_tracker.report( trg, loss_builder.get_factored_loss_val()) self.checkpoint_and_save(cur_task, cur_task_id, save_fct, dev_zero) if cur_task.should_stop_training(): break
def setUp(self): layer_dim = 512 events.clear() ParamManager.init_param_col() src_vocab = Vocab(vocab_file="examples/data/head.ja.vocab") trg_vocab = Vocab(vocab_file="examples/data/head.en.vocab") self.model = DefaultTranslator( src_reader=PlainTextReader(vocab=src_vocab), trg_reader=PlainTextReader(vocab=trg_vocab), src_embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), encoder=BiLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim), attender=MlpAttender(input_dim=layer_dim, state_dim=layer_dim, hidden_dim=layer_dim), decoder=AutoRegressiveDecoder( input_dim=layer_dim, embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), rnn=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, decoder_input_dim=layer_dim, yaml_path="model.decoder.rnn"), transform=NonLinear(input_dim=layer_dim * 2, output_dim=layer_dim), scorer=Softmax(input_dim=layer_dim, vocab_size=100), bridge=CopyBridge(dec_dim=layer_dim, dec_layers=1)), ) event_trigger.set_train(False) self.src_data = list( self.model.src_reader.read_sents("examples/data/head.ja"))
def test_policy_train_test(self): event_trigger.set_train(True) self.calc_loss_single_batch() self.assertEqual(self.model.encoder.policy_learning.sampling_action, PolicyGradient.SamplingAction.POLICY_CLP) event_trigger.set_train(False) self.calc_loss_single_batch() self.assertEqual(self.model.encoder.policy_learning.sampling_action, PolicyGradient.SamplingAction.POLICY_AMAX)
def test_transducer_composer(self): enc = self.segmenting_encoder enc.segment_composer = SeqTransducerComposer( seq_transducer=BiLSTMSeqTransducer(input_dim=self.layer_dim, hidden_dim=self.layer_dim)) event_trigger.set_train(True) enc.transduce(self.inp_emb(0))
def test_loss_model1(self): layer_dim = 512 model = DefaultTranslator( src_reader=self.src_reader, trg_reader=self.trg_reader, src_embedder=LookupEmbedder(emb_dim=layer_dim, vocab_size=100), encoder=BiLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim), attender=MlpAttender(input_dim=layer_dim, state_dim=layer_dim, hidden_dim=layer_dim), decoder=AutoRegressiveDecoder( input_dim=layer_dim, embedder=LookupEmbedder(emb_dim=layer_dim, vocab_size=100), rnn=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, decoder_input_dim=layer_dim, yaml_path="model.decoder.rnn"), transform=NonLinear(input_dim=layer_dim * 2, output_dim=layer_dim), scorer=Softmax(input_dim=layer_dim, vocab_size=100), bridge=CopyBridge(dec_dim=layer_dim, dec_layers=1)), ) event_trigger.set_train(False) self.assert_single_loss_equals_batch_loss(model)
def test_composite(self): event_trigger.set_train(True) composite_loss = loss_calculators.CompositeLoss([loss_calculators.MLELoss(), loss_calculators.PolicyMLELoss()]) composite_loss.calc_loss(self.model, self.src[0], self.trg[0]) event_trigger.set_train(False) self.model.generate(batchers.mark_as_batch([self.src_data[0]]), GreedySearch())
def _advance_epoch(self): """ Shifts internal state to the next epoch, including data (re-)loading, batch re-packing and shuffling. """ if self.reload_command is not None: if self.training_state.epoch_num == 0: self._augmentation_handle = None self._augment_data_initial() else: self._augment_data_next_epoch() if self.training_state.epoch_num==0 or self.sample_train_sents or \ self.model.src_reader.needs_reload() or self.model.trg_reader.needs_reload(): event_trigger.set_train(True) self.src_data, self.trg_data, self.src_batches, self.trg_batches = \ input_readers.read_parallel_corpus(src_reader=self.model.src_reader, trg_reader=self.model.trg_reader, src_file=self.src_file, trg_file=self.trg_file, batcher=self.batcher, sample_sents=self.sample_train_sents, max_num_sents=self.max_num_train_sents, max_src_len=self.max_src_len, max_trg_len=self.max_trg_len) self.model.src_reader.train = self.model.trg_reader.train = False self.training_state.epoch_seed = random.randint(1, 2147483647) random.seed(self.training_state.epoch_seed) np.random.seed(self.training_state.epoch_seed) self.src_batches, self.trg_batches = \ self.batcher.pack(self.src_data, self.trg_data) self.training_state.epoch_num += 1 self.training_state.steps_into_epoch = 0 self.training_state.sents_into_epoch = 0 self.minibatch_order = list(range(0, self.cur_num_minibatches())) np.random.shuffle(self.minibatch_order) event_trigger.new_epoch(training_task=self, num_sents=self.cur_num_sentences())
def run_training(self, save_fct: Callable) -> None: """ Main training loop (overwrites TrainingRegimen.run_training()) """ dy.renew_cg(immediate_compute=settings.IMMEDIATE_COMPUTE, check_validity=settings.CHECK_VALIDITY) if self.run_for_epochs is None or self.run_for_epochs > 0: total_loss = losses.FactoredLossExpr() # Needed for report total_trg = [] for src, trg in self.next_minibatch(): if self.dev_zero: self.checkpoint_and_save(save_fct) self.dev_zero = False with utils.ReportOnException({ "src": src, "trg": trg, "graph": utils.print_cg_conditional }): with self.train_loss_tracker.time_tracker: event_trigger.set_train(True) total_trg.append(trg[0]) loss_builder = self.training_step(src, trg) total_loss.add_factored_loss_expr(loss_builder) # num_updates_skipped is incremented in update but # we need to call backward before update if self.num_updates_skipped == self.update_every - 1: self.backward(total_loss.compute(), self.dynet_profiling) self.update(self.trainer) if self.num_updates_skipped == 0: total_loss_val = total_loss.get_factored_loss_val( comb_method=self.loss_comb_method) reported_trg = batchers.ListBatch(total_trg) self.train_loss_tracker.report(reported_trg, total_loss_val) total_loss = losses.FactoredLossExpr() total_trg = [] dy.renew_cg( immediate_compute=settings.IMMEDIATE_COMPUTE, check_validity=settings.CHECK_VALIDITY) if self.checkpoint_needed(): # Do a last update before checkpoint # Force forward-backward for the last batch even if it's smaller than update_every self.num_updates_skipped = self.update_every - 1 self.backward(total_loss.compute(), self.dynet_profiling) self.update(self.trainer) total_loss_val = total_loss.get_factored_loss_val( comb_method=self.loss_comb_method) reported_trg = batchers.ListBatch(total_trg) self.train_loss_tracker.report(reported_trg, total_loss_val) total_loss = losses.FactoredLossExpr() total_trg = [] self.checkpoint_and_save(save_fct) if self.should_stop_training(): break
def setUp(self): # Seeding numpy.random.seed(2) random.seed(2) layer_dim = 64 xnmt.events.clear() ParamManager.init_param_col() self.segment_encoder_bilstm = BiLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim) self.segment_composer = SumComposer() self.src_reader = CharFromWordTextReader(vocab=Vocab(vocab_file="examples/data/head.ja.charvocab")) self.trg_reader = PlainTextReader(vocab=Vocab(vocab_file="examples/data/head.en.vocab")) self.loss_calculator = FeedbackLoss(child_loss=MLELoss(), repeat=5) baseline = Linear(input_dim=layer_dim, output_dim=1) policy_network = Linear(input_dim=layer_dim, output_dim=2) self.poisson_prior = PoissonPrior(mu=3.3) self.eps_greedy = EpsilonGreedy(eps_prob=0.0, prior=self.poisson_prior) self.conf_penalty = ConfidencePenalty() self.policy_gradient = PolicyGradient(input_dim=layer_dim, output_dim=2, baseline=baseline, policy_network=policy_network, z_normalization=True, conf_penalty=self.conf_penalty) self.length_prior = PoissonLengthPrior(lmbd=3.3, weight=1) self.segmenting_encoder = SegmentingSeqTransducer( embed_encoder = self.segment_encoder_bilstm, segment_composer = self.segment_composer, final_transducer = BiLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim), policy_learning = self.policy_gradient, eps_greedy = self.eps_greedy, length_prior = self.length_prior, ) self.model = DefaultTranslator( src_reader=self.src_reader, trg_reader=self.trg_reader, src_embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), encoder=self.segmenting_encoder, attender=MlpAttender(input_dim=layer_dim, state_dim=layer_dim, hidden_dim=layer_dim), trg_embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), decoder=AutoRegressiveDecoder(input_dim=layer_dim, rnn=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, decoder_input_dim=layer_dim, yaml_path="decoder"), transform=AuxNonLinear(input_dim=layer_dim, output_dim=layer_dim, aux_input_dim=layer_dim), scorer=Softmax(vocab_size=100, input_dim=layer_dim), trg_embed_dim=layer_dim, bridge=CopyBridge(dec_dim=layer_dim, dec_layers=1)), ) event_trigger.set_train(True) self.layer_dim = layer_dim self.src_data = list(self.model.src_reader.read_sents("examples/data/head.ja")) self.trg_data = list(self.model.trg_reader.read_sents("examples/data/head.en")) my_batcher = batchers.TrgBatcher(batch_size=3) self.src, self.trg = my_batcher.pack(self.src_data, self.trg_data) dy.renew_cg(immediate_compute=True, check_validity=True)
def test_train_mle_only(self): self.model.policy_network = None event_trigger.set_train(True) mle_loss = loss_calculators.MLELoss() mle_loss.calc_loss(self.model, self.src[0], self.trg[0]) event_trigger.set_train(False) self.model.generate(batchers.mark_as_batch([self.src_data[0]]), GreedySearch())
def test_no_policy_train_test(self): self.model.encoder.policy_learning = None event_trigger.set_train(True) self.calc_loss_single_batch() self.assertEqual(self.model.encoder.segmenting_action, SegmentingSeqTransducer.SegmentingAction.PURE_SAMPLE) event_trigger.set_train(False) self.calc_loss_single_batch() self.assertEqual(self.model.encoder.segmenting_action, SegmentingSeqTransducer.SegmentingAction.PURE_SAMPLE)
def assert_in_out_len_equal(self, model): dy.renew_cg() event_trigger.set_train(True) src = self.src_data[0] event_trigger.start_sent(src) embeddings = model.src_embedder.embed_sent(src) encodings = model.encoder.transduce(embeddings) self.assertEqual(len(embeddings), len(encodings))
def setUp(self): # Seeding numpy.random.seed(2) random.seed(2) layer_dim = 32 xnmt.events.clear() ParamManager.init_param_col() edge_vocab = Vocab(vocab_file="examples/data/parse/head.en.edge_vocab") node_vocab = Vocab(vocab_file="examples/data/parse/head.en.node_vocab") value_vocab = Vocab(vocab_file="examples/data/head.en.vocab") self.src_reader = input_readers.PlainTextReader(vocab=value_vocab) self.trg_reader = input_readers.CoNLLToRNNGActionsReader( surface_vocab=value_vocab, nt_vocab=node_vocab, edg_vocab=edge_vocab) self.layer_dim = layer_dim self.src_data = list( self.src_reader.read_sents("examples/data/head.en")) self.trg_data = list( self.trg_reader.read_sents("examples/data/parse/head.en.conll")) self.loss_calculator = MLELoss() self.head_composer = composer.DyerHeadComposer( fwd_combinator=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim), bwd_combinator=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim), transform=AuxNonLinear(input_dim=layer_dim, aux_input_dim=layer_dim, output_dim=layer_dim)) self.model = DefaultTranslator( src_reader=self.src_reader, trg_reader=self.trg_reader, src_embedder=LookupEmbedder(emb_dim=layer_dim, vocab_size=len(value_vocab)), encoder=IdentitySeqTransducer(), attender=MlpAttender(input_dim=layer_dim, state_dim=layer_dim, hidden_dim=layer_dim), decoder=RNNGDecoder( input_dim=layer_dim, rnn=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, decoder_input_dim=layer_dim), transform=AuxNonLinear(input_dim=layer_dim, output_dim=layer_dim, aux_input_dim=layer_dim), bridge=NoBridge(dec_dim=layer_dim, dec_layers=1), graph_reader=self.trg_reader, head_composer=self.head_composer)) event_trigger.set_train(True) my_batcher = batchers.TrgBatcher(batch_size=1) self.src, self.trg = my_batcher.pack(self.src_data, self.trg_data) dy.renew_cg(immediate_compute=True, check_validity=True)
def test_transducer_composer(self): composer = SeqTransducerComposer(seq_transducer=BiLSTMSeqTransducer(input_dim=self.layer_dim, hidden_dim=self.layer_dim)) embedder = CharCompositionEmbedder(emb_dim=self.layer_dim, composer=composer, char_vocab=self.src_char_vocab) event_trigger.set_train(True) event_trigger.start_sent(self.src[1]) embedder.embed_sent(self.src[1])
def test_train_nll(self): event_trigger.set_train(True) mle_loss = loss_calculators.MLELoss() mle_loss.calc_loss(self.model, self.src[0], self.trg[0]) pol_loss = loss_calculators.PolicyMLELoss() pol_loss._perform_calc_loss(self.model, self.src[0], self.trg[0]) event_trigger.set_train(False) self.model.generate(batchers.mark_as_batch([self.src_data[0]]), GreedySearch())
def test_dyer_composer(self): composer = DyerHeadComposer(fwd_combinator=UniLSTMSeqTransducer(input_dim=self.layer_dim, hidden_dim=self.layer_dim), bwd_combinator=UniLSTMSeqTransducer(input_dim=self.layer_dim, hidden_dim=self.layer_dim), transform=AuxNonLinear(input_dim=self.layer_dim, output_dim=self.layer_dim, aux_input_dim=self.layer_dim)) embedder = CharCompositionEmbedder(emb_dim=self.layer_dim, composer=composer, char_vocab=self.src_char_vocab) event_trigger.set_train(True) event_trigger.start_sent(self.src[1]) embedder.embed_sent(self.src[1])
def test_convolution_composer(self): enc = self.segmenting_encoder enc.segment_composer = ConvolutionComposer(ngram_size=1, embed_dim=self.layer_dim, hidden_dim=self.layer_dim) event_trigger.set_train(True) enc.transduce(self.inp_emb(0)) enc.segment_composer = ConvolutionComposer(ngram_size=3, embed_dim=self.layer_dim, hidden_dim=self.layer_dim) event_trigger.set_train(True) enc.transduce(self.inp_emb(0))
def eval(self) -> Sequence[metrics.EvalScore]: event_trigger.set_train(False) if issubclass(self.model.__class__, reports.Reportable): self.model.report_corpus_info({"ref_file": self.ref_file}) if self.perform_inference: self.inference.perform_inference(generator=self.model, src_file=self.src_file, trg_file=self.hyp_file) # Evaluate eval_scores = xnmt_evaluate.xnmt_evaluate(hyp_file=self.hyp_file, ref_file=self.ref_file, desc=self.desc, evaluators=self.eval_metrics) return eval_scores
def setUp(self): # Seeding numpy.random.seed(2) random.seed(2) layer_dim = 4 xnmt.events.clear() ParamManager.init_param_col() self.segment_composer = SumComposer() self.src_reader = CharFromWordTextReader(vocab=Vocab( vocab_file="examples/data/head.ja.charvocab")) self.trg_reader = PlainTextReader(vocab=Vocab( vocab_file="examples/data/head.en.vocab")) self.loss_calculator = FeedbackLoss(child_loss=MLELoss(), repeat=5) self.segmenting_encoder = SegmentingSeqTransducer( segment_composer=self.segment_composer, final_transducer=BiLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim), ) self.model = DefaultTranslator( src_reader=self.src_reader, trg_reader=self.trg_reader, src_embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), encoder=self.segmenting_encoder, attender=MlpAttender(input_dim=layer_dim, state_dim=layer_dim, hidden_dim=layer_dim), decoder=AutoRegressiveDecoder( input_dim=layer_dim, rnn=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, decoder_input_dim=layer_dim, yaml_path="decoder"), transform=AuxNonLinear(input_dim=layer_dim, output_dim=layer_dim, aux_input_dim=layer_dim), scorer=Softmax(vocab_size=100, input_dim=layer_dim), embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=100), bridge=CopyBridge(dec_dim=layer_dim, dec_layers=1)), ) event_trigger.set_train(True) self.layer_dim = layer_dim self.src_data = list( self.model.src_reader.read_sents("examples/data/head.ja")) self.trg_data = list( self.model.trg_reader.read_sents("examples/data/head.en")) my_batcher = batchers.TrgBatcher(batch_size=3) self.src, self.trg = my_batcher.pack(self.src_data, self.trg_data) dy.renew_cg(immediate_compute=True, check_validity=True)
def trigger_train_event(self, value: bool) -> None: """ Trigger set_train event, but only if that would lead to a change of the value of set_train. Args: value: True or False """ if self.train is None: self.train = value event_trigger.set_train(value) else: if value != self.train: self.train = value event_trigger.set_train(value)
def eval(self): event_trigger.set_train(False) self.report_corpus_info({"ref_file": self.ref_file}) self.inference.perform_inference(generator=self.model, src_file=self.src_file, trg_file=self.hyp_file) # Evaluate eval_scores = xnmt.xnmt_evaluate.xnmt_evaluate( hyp_file=self.hyp_file, ref_file=self.ref_file, desc=self.desc, evaluators=self.eval_metrics) return eval_scores
def setUp(self): # Seeding numpy.random.seed(2) random.seed(2) layer_dim = 32 xnmt.events.clear() ParamManager.init_param_col() self.src_reader = PlainTextReader(vocab=Vocab( vocab_file="test/data/head.ja.vocab")) self.trg_reader = PlainTextReader(vocab=Vocab( vocab_file="test/data/head.en.vocab")) self.layer_dim = layer_dim self.src_data = list(self.src_reader.read_sents("test/data/head.ja")) self.trg_data = list(self.trg_reader.read_sents("test/data/head.en")) self.input_vocab_size = len(self.src_reader.vocab.i2w) self.output_vocab_size = len(self.trg_reader.vocab.i2w) self.loss_calculator = MLELoss() self.model = SimultaneousTranslator( src_reader=self.src_reader, trg_reader=self.trg_reader, src_embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=self.input_vocab_size), encoder=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim), attender=MlpAttender(input_dim=layer_dim, state_dim=layer_dim, hidden_dim=layer_dim), decoder=AutoRegressiveDecoder( input_dim=layer_dim, rnn=UniLSTMSeqTransducer(input_dim=layer_dim, hidden_dim=layer_dim, decoder_input_dim=layer_dim, yaml_path="decoder"), transform=AuxNonLinear(input_dim=layer_dim, output_dim=layer_dim, aux_input_dim=layer_dim), scorer=Softmax(vocab_size=self.output_vocab_size, input_dim=layer_dim), embedder=SimpleWordEmbedder(emb_dim=layer_dim, vocab_size=self.output_vocab_size), bridge=NoBridge(dec_dim=layer_dim, dec_layers=1)), ) event_trigger.set_train(True) my_batcher = batchers.TrgBatcher(batch_size=3) self.src, self.trg = my_batcher.pack(self.src_data, self.trg_data) dy.renew_cg(immediate_compute=True, check_validity=True)
def test_composite_composer(self): composer = DyerHeadComposer(fwd_combinator=UniLSTMSeqTransducer(input_dim=self.layer_dim, hidden_dim=self.layer_dim), bwd_combinator=UniLSTMSeqTransducer(input_dim=self.layer_dim, hidden_dim=self.layer_dim), transform=AuxNonLinear(input_dim=self.layer_dim, output_dim=self.layer_dim, aux_input_dim=self.layer_dim)) embedder_1 = CharCompositionEmbedder(emb_dim=self.layer_dim, composer=composer, char_vocab=self.src_char_vocab) embedder_2 = LookupEmbedder(emb_dim=self.layer_dim, vocab_size=100) embedder = CompositeEmbedder(embedders=[embedder_1, embedder_2]) event_trigger.set_train(True) event_trigger.start_sent(self.src[1]) embedder.embed_sent(self.src[1]) embedder.embed(self.src[1][0].words[0])