Skip to content

Commit

Permalink
Merge pull request #496 from neulab/revert-486-refactor-loss
Browse files Browse the repository at this point in the history
Revert "Started refactoring loss calculation [WIP]"
  • Loading branch information
philip30 committed Aug 2, 2018
2 parents f21ddca + 53b3a96 commit 9dd7f1c
Show file tree
Hide file tree
Showing 43 changed files with 444 additions and 578 deletions.
7 changes: 0 additions & 7 deletions test/config/assemble.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# note: run as ``xnmt --settings=unittest <config>.yaml``
exp1-pretrain1: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.ja.vocab}
Expand Down Expand Up @@ -33,7 +31,6 @@ exp1-pretrain1: !Experiment
exp2-pretrain2: !Experiment
exp_global: !ExpGlobal
model_file: 'test/tmp/{EXP}.mod'
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.ja.vocab}
Expand Down Expand Up @@ -63,8 +60,6 @@ exp2-pretrain2: !Experiment


exp3-assemble: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
Expand Down Expand Up @@ -97,8 +92,6 @@ exp3-assemble: !Experiment


exp4-assemble: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
Expand Down
2 changes: 0 additions & 2 deletions test/config/classifier.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# This demonstrates using a sequence classifier model that takes a sequence as input and outputs a single class label.
classifier: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !SequenceClassifier
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
Expand Down
5 changes: 2 additions & 3 deletions test/config/lm.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
lm: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !LanguageModel
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
src_embedder: !SimpleWordEmbedder {}
src_embedder: !SimpleWordEmbedder
emb_dim: 512
rnn: !UniLSTMSeqTransducer
layers: 1
scorer: !Softmax
Expand Down
8 changes: 3 additions & 5 deletions test/config/minrisk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ minrisk: !Experiment
vocab: !Vocab {vocab_file: examples/data/head.ja.vocab}
trg_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
search_strategy: !SamplingSearch
max_len: 10
sample_size: 10
train: !SimpleTrainingRegimen
batcher: !SrcBatcher
batch_size: 1
run_for_epochs: 2
src_file: examples/data/head.ja
trg_file: examples/data/head.en
Expand All @@ -24,9 +25,6 @@ minrisk: !Experiment
ref_file: examples/data/head.en
loss_calculator: !MinRiskLoss
alpha: 0.005
search_strategy: !SamplingSearch
max_len: 10
sample_size: 10
evaluate:
- !AccuracyEvalTask
eval_metrics: bleu
Expand Down
2 changes: 0 additions & 2 deletions test/config/preproc.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
standard-preproc: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
preproc: !PreprocRunner
overwrite: True
tasks:
Expand Down
2 changes: 0 additions & 2 deletions test/config/pretrained_embeddings.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# An example for how to use pretrained embeddings
pretrained_emb: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.ja.vocab}
Expand Down
6 changes: 2 additions & 4 deletions test/config/reinforce.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ reinforce: !Experiment
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.ja.vocab}
trg_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
search_strategy: !SamplingSearch {}
train: !SimpleTrainingRegimen
batcher: !SrcBatcher
batch_size: 1
run_for_epochs: 2
src_file: examples/data/head.ja
trg_file: examples/data/head.en
Expand All @@ -23,7 +22,6 @@ reinforce: !Experiment
ref_file: examples/data/head.en
loss_calculator: !ReinforceLoss
use_baseline: True
search_strategy: !SamplingSearch {}
evaluate:
- !AccuracyEvalTask
eval_metrics: bleu
Expand Down
2 changes: 0 additions & 2 deletions test/config/reload.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
reload: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !NpzReader
transpose: True
Expand Down
2 changes: 0 additions & 2 deletions test/config/reload_exception.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# note: run as ``xnmt --settings=unittest <config>.yaml``
reload: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !NpzReader
transpose: True
Expand Down
1 change: 0 additions & 1 deletion test/config/report.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
report: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
compute_report: True
model: !DefaultTranslator
src_reader: !PlainTextReader
Expand Down
1 change: 0 additions & 1 deletion test/config/seg_debug.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
report: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
compute_report: True
model: !DefaultTranslator
src_reader: !CharFromWordTextReader
Expand Down
5 changes: 1 addition & 4 deletions test/config/seg_report.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
report: !Experiment
exp_global: !ExpGlobal
compute_report: True
default_layer_dim: 64
model: !DefaultTranslator
src_reader: !CharFromWordTextReader
vocab: !Vocab {vocab_file: examples/data/head.ja.charvocab}
Expand All @@ -14,13 +13,11 @@ report: !Experiment
final_transducer: !BiLSTMSeqTransducer {}
policy_learning: !PolicyGradient
z_normalization: True
sample: 2
train: !SimpleTrainingRegimen
run_for_epochs: 1
src_file: examples/data/head.ja
trg_file: examples/data/head.en
loss_calculator: !FeedbackLoss
child_loss: !MLELoss {}
repeat: 2
evaluate:
- !AccuracyEvalTask
eval_metrics: bleu,wer
Expand Down
1 change: 0 additions & 1 deletion test/config/self_attentional_am.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# note: run as ``xnmt --settings=unittest <config>.yaml``
speech-self-att: !Experiment
exp_global: !ExpGlobal
default_layer_dim: 64
dropout: 0.2
preproc: !PreprocRunner
overwrite: False
Expand Down
2 changes: 0 additions & 2 deletions test/config/seq_labeler.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
seq-labeler: !Experiment
# model architecture
exp_global: !ExpGlobal
default_layer_dim: 64
model: !SeqLabeler
src_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
Expand Down
2 changes: 2 additions & 0 deletions test/config/standard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ standard: !Experiment
vocab: !Vocab {vocab_file: examples/data/head.ja.vocab}
trg_reader: !PlainTextReader
vocab: !Vocab {vocab_file: examples/data/head.en.vocab}
search_strategy: !BeamSearch
beam_size: 5
train: !SimpleTrainingRegimen
run_for_epochs: 2
src_file: examples/data/head.ja
Expand Down
14 changes: 8 additions & 6 deletions test/test_beam_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from xnmt.modelparts.embedders import SimpleWordEmbedder
from xnmt.input_readers import PlainTextReader
from xnmt.transducers.recurrent import UniLSTMSeqTransducer, BiLSTMSeqTransducer
from xnmt.loss_calculators import MLELoss
from xnmt.loss_calculators import AutoRegressiveMLELoss
from xnmt.param_collections import ParamManager
from xnmt.modelparts.transforms import NonLinear
from xnmt.modelparts.scorers import Softmax
from xnmt.models.translators import DefaultTranslator
from xnmt.search_strategies import BeamSearch, GreedySearch
from xnmt.param_collections import ParamManager
from xnmt.vocabs import Vocab

class TestForcedDecodingOutputs(unittest.TestCase):
Expand Down Expand Up @@ -88,8 +88,9 @@ def setUp(self):

def test_single(self):
dy.renew_cg()
train_loss = self.model.calc_nll(src=self.src_data[0],
trg=self.trg_data[0]).value()
train_loss = self.model.calc_loss(src=self.src_data[0],
trg=self.trg_data[0],
loss_calculator=AutoRegressiveMLELoss()).value()
dy.renew_cg()
outputs = self.model.generate(batchers.mark_as_batch([self.src_data[0]]), [0], BeamSearch(beam_size=1),
forced_trg_ids=batchers.mark_as_batch([self.trg_data[0]]))
Expand Down Expand Up @@ -127,8 +128,9 @@ def test_single(self):
outputs = self.model.generate(batchers.mark_as_batch([self.src_data[0]]), [0], BeamSearch(),
forced_trg_ids=batchers.mark_as_batch([self.trg_data[0]]))
dy.renew_cg()
train_loss = self.model.calc_nll(src=self.src_data[0],
trg=outputs[0]).value()
train_loss = self.model.calc_loss(src=self.src_data[0],
trg=outputs[0],
loss_calculator=AutoRegressiveMLELoss()).value()

self.assertAlmostEqual(-outputs[0].score, train_loss, places=4)

Expand Down
12 changes: 7 additions & 5 deletions test/test_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from xnmt.modelparts.decoders import AutoRegressiveDecoder
from xnmt.modelparts.embedders import SimpleWordEmbedder
from xnmt.input_readers import PlainTextReader
from xnmt.loss_calculators import MLELoss
from xnmt.loss_calculators import AutoRegressiveMLELoss
from xnmt.transducers.recurrent import UniLSTMSeqTransducer, BiLSTMSeqTransducer
from xnmt.param_collections import ParamManager
from xnmt.modelparts.transforms import NonLinear
Expand Down Expand Up @@ -90,8 +90,9 @@ def setUp(self):

def test_single(self):
dy.renew_cg()
train_loss = self.model.calc_nll(src=self.src_data[0],
trg=self.trg_data[0]).value()
train_loss = self.model.calc_loss(src=self.src_data[0],
trg=self.trg_data[0],
loss_calculator=AutoRegressiveMLELoss()).value()
dy.renew_cg()
outputs = self.model.generate(batchers.mark_as_batch([self.src_data[0]]), [0], GreedySearch(),
forced_trg_ids=batchers.mark_as_batch([self.trg_data[0]]))
Expand Down Expand Up @@ -132,8 +133,9 @@ def test_single(self):
output_score = outputs[0].score

dy.renew_cg()
train_loss = self.model.calc_nll(src=self.src_data[0],
trg=outputs[0]).value()
train_loss = self.model.calc_loss(src=self.src_data[0],
trg=outputs[0],
loss_calculator=AutoRegressiveMLELoss()).value()

self.assertAlmostEqual(-output_score, train_loss, places=5)

Expand Down
4 changes: 2 additions & 2 deletions test/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from xnmt.eval import metrics
from xnmt import events
from xnmt.utils import has_cython
from test.utils import has_cython
from xnmt.vocabs import Vocab

class TestBLEU(unittest.TestCase):
Expand All @@ -26,7 +26,7 @@ def test_bleu_1gram(self):
def test_bleu_4gram_fast(self):
bleu = metrics.FastBLEUEvaluator(ngram=4, smooth=1)
exp_bleu = math.exp(math.log((3.0/5.0) * (2.0/5.0) * (1.0/4.0) * (1.0/3.0))/4.0)
act_bleu = bleu.evaluate_one_sent(self.ref_id, self.hyp_id)
act_bleu = bleu.evaluate(self.ref_id, self.hyp_id)
self.assertEqual(act_bleu, exp_bleu)

class TestGLEU(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion test/test_hyperparams.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from xnmt import events

from xnmt.hyper_params import *


Expand Down Expand Up @@ -48,7 +49,7 @@ def test_defined_sequence(self):
# transformer=self.tail_transformer)
# self.src_reader = PlainTextReader()
# self.trg_reader = PlainTextReader()
# self.loss_calculator = MLELoss()
# self.loss_calculator = AutoRegressiveMLELoss()
# self.segmenting_encoder = SegmentingSeqTransducer(
# embed_encoder = self.segment_embed_encoder_bilstm,
# segment_composer = self.segment_composer,
Expand Down
2 changes: 1 addition & 1 deletion test/test_run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import os, shutil

from xnmt.utils import has_cython
from test.utils import has_cython
import xnmt.xnmt_run_experiments as run
import xnmt.events

Expand Down

0 comments on commit 9dd7f1c

Please sign in to comment.