Gabriele Sarti • Grzegorz Chrupała • Malvina Nissim • Arianna Bisazza
pecore zweistufiger Prozess" width="300" style="max-width: 100%;"> pecore-Beispiele" width="500" style="max-width: 100%;">
Zusammenfassung: Es ist wichtig festzustellen, ob Sprachmodelle Kontextinformationen auf eine für den Menschen plausible Weise nutzen können, um ihre sichere Übernahme in realen Umgebungen sicherzustellen. Allerdings werden die Fragen, wann und welche Teile des Kontexts Modellgenerationen beeinflussen, typischerweise separat behandelt, und aktuelle Plausibilitätsbewertungen beschränken sich praktisch auf eine Handvoll künstlicher Benchmarks. Um dieses Problem anzugehen, führen wir Plausibility Evaluation of Context Reliance ( pecore ) ein, ein End-to-End-Interpretierbarkeitsframework, das zur Quantifizierung der Kontextnutzung in Generationen von Sprachmodellen entwickelt wurde. Unser Ansatz nutzt Modellinterna, um (i) kontextsensitive Ziel-Tokens in generierten Texten kontrastiv zu identifizieren und (ii) sie mit kontextuellen Hinweisen zu verknüpfen, die ihre Vorhersage rechtfertigen. Wir verwenden pecore um die Plausibilität kontextbewusster maschineller Übersetzungsmodelle zu quantifizieren, indem wir Modellbegründungen mit menschlichen Anmerkungen über mehrere Phänomene auf Diskursebene hinweg vergleichen. Schließlich wenden wir unsere Methode auf nicht annotierte Generationen an, um kontextvermittelte Vorhersagen zu identifizieren und Fälle (un)plausibler Kontextverwendung in Modellübersetzungen hervorzuheben.
Dieses Repository enthält Skripte und Notizbücher zum Artikel „Quantifying the Plausibility of Context Reliance in Neural Machine Translation“. Wenn Sie einen der folgenden Inhalte für Ihre Arbeit verwenden, bitten wir Sie, unseren Beitrag zu zitieren:
@inproceedings { sarti-etal-2023-quantifying ,
title = " Quantifying the Plausibility of Context Reliance in Neural Machine Translation " ,
author = " Sarti, Gabriele and
Chrupa{l}a, Grzegorz and
Nissim, Malvina and
Bisazza, Arianna " ,
booktitle = " The Twelfth International Conference on Learning Representations (ICLR 2024) " ,
month = may,
year = " 2024 " ,
address = " Vienna, Austria " ,
publisher = " OpenReview " ,
url = " https://openreview.net/forum?id=XTHfNGI3zT "
}
Tipp
Sie können pecore in unserer Online-Demo zu Hugging Face Spaces ausprobieren.
Während dieses Repository die bei der experimentellen Auswertung des oben genannten Artikels verwendeten Funktionen implementiert, stellen wir über die Inseq-Interpretierbarkeitsbibliothek eine neue CLI-Implementierung von pecore bereit. Wir empfehlen Forschern dringend, diese Implementierung zu übernehmen, da sie robuster und verallgemeinerbarer ist und alle Nur-Decoder- und Encoder-Decoder-Modelle aus der Huggingface-Bibliothek für die Erkennung und Zuordnung von Kontextabhängigkeiten bei Eingabe und Ausgabe unterstützt. Weitere Einzelheiten finden Sie im Abschnitt inseq attribute-context
in der Inseq-README-Datei.
Alle Artefakte für die Arbeit, einschließlich fein abgestimmter Modelle und Trainings-/Evaluierungsdatensätze, sind in der pecore HuggingFace Collection verfügbar. Eine Demo wird bald verfügbar sein, bleiben Sie dran!
Kontextsensitive NMT-Modelle werden mit dem Skript train_context_aware_mt_model.py
trainiert. Das Skript ist eine Modifikation des Originals run_translation_no_trainer.py
. Das Skript fügt die folgenden Felder für das kontextbezogene Modelltraining hinzu:
context_size
: Die Anzahl der Kontextsätze, die für das Training verwendet werden sollen. Der Standardwert ist 0 (Training auf Satzebene).
sample_context
: Wenn festgelegt, wird die Größe des Kontexts für jedes Beispiel aus einer gleichmäßigen Verteilung zwischen 0 und context_size
(einschließlich) abgetastet. Wenn nicht übergeben und context_size
größer als 0 ist, ist die Kontextgröße immer gleich context_size
.
context_word_dropout
: Wahrscheinlichkeit zwischen 0 und 1, ein Wort aus dem Kontext zu entfernen. Der Standardwert ist 0 (kein Dropout).
use_target_context
: Wenn festgelegt, wird der Kontext auch in den übersetzten Text für den Trainingsverlust einbezogen. In diesem Fall wird das Ausgabeformat für eine Eingabe src_ctx <brk> src
zu tgt_ctx <brk> tgt
. Ansonsten ist das Ausgabeformat tgt
(nur src
wird übersetzt).
Hier ist ein Beispiel für die Feinabstimmung eines mBART 1-zu-50-Modells auf dem kontexterweiterten IWSLT17-Datensatz mit bis zu 4 Kontextsätzen und einem 10 %igen Kontextwortausfall:
accelerate launch scripts/train_context_aware_mt_model.py
--model_name_or_path facebook/mbart-large-50-one-to-many-mmt
--source_lang en_XX
--target_lang fr_XX
--dataset_name gsarti/iwslt2017_context
--dataset_config_name iwslt2017-en-fr
--output_dir outputs/models/iwslt17-mbart50-1toM-ctx4-cwd1-en-fr
--num_beams 5
--max_source_length 512
--max_target_length 128
--num_train_epochs 20
--gradient_accumulation_steps 4
--per_device_train_batch_size 8
--num_warmup_steps 500
--learning_rate 3e-4
--checkpointing_steps epoch
--with_tracking
--report_to tensorboard
--context_size 4
--sample_context
--context_word_dropout 0.1
Hier ist ein Beispiel für die Fortsetzung der Feinabstimmung eines kontextsensitiven En->Fr OpusMT-Modells im Trainingsteil von SCAT mit bis zu 4 Kontextsätzen und einem 10 %igen Kontextwortausfall:
accelerate launch scripts/train_context_aware_mt_model.py
--model_name_or_path context-mt/iwslt17-marian-big-ctx4-cwd1-en-fr
--dataset_name inseq/scat
--dataset_config_name sentences
--output_dir outputs/models/scat-marian-big-ctx4-cwd1-en-fr
--num_beams 5
--max_source_length 512
--max_target_length 128
--num_train_epochs 2
--gradient_accumulation_steps 2
--per_device_train_batch_size 8
--num_warmup_steps 0
--learning_rate 5e-5
--checkpointing_steps 1000
--logging_steps 200
--with_tracking
--report_to tensorboard
--context_size 4
--sample_context
--context_word_dropout 0.1
Die pecore CLI ist eine Befehlszeilenschnittstelle zum Ausführen der pecore -Schritte für ein bestimmtes Modell und einen bestimmten Datensatz. Die CLI ist im pecore /cli.py
implementiert und kann bei der Installation des Pakets mit pip install -e als pecore -viz
verwendet werden pip install -e .
. Die aktuelle Implementierung unterstützt die Identifizierung kontextsensitiver Ziele (CTI) und die Imputation kontextbezogener Hinweise (CCI) für alle vom Inseq-Framework unterstützten Encoder-Decoder-Modelle, einschließlich Modelle mit Sprachpräfix-Tags (mBART-50, NLLB, M2M100). und Modelle, die mit speziellen Kontext-Tags trainiert wurden (z. B. die Sammlung von Modellen, die in der context-mt-Organisation auf dem HF-Hub zu finden sind). Die CLI kann verwendet werden, um die pecore -Schritte für ein bestimmtes Modell und Beispiel wie folgt auszuführen:
pecore -viz
--model_name context-mt/scat-marian-small-ctx4-cwd1-en-fr
--attributions_aggregate_fns sum
--model_use_ctx_break
--impute_with_contextless_output
--force_context_aware_output_prefix
--input " Did I mention we stole a cow? A beautiful animal, truly. We brought it to the stable and kept it there for ages.<brk> Sadly, we could not foresee it would disappear. "
Das obige Beispiel erzeugt die folgende Ausgabe, die die Abhängigkeit des Pronomens „il“ von den Substantiven „cow“ und „animal“ im Kontext korrekt hervorhebt.
Context with contextual cues (std λ=1.00) followed by output sentence
with context-sensitive target spans (std λ=1.00):
Input context: Did I mention we stole a cow ? A beautiful animal, truly. We brought it to the stable and kept it there for ages.
Input current: Sadly, we could not foresee it would disappear.
Context-aware output: Malheureusement, nous n ' avons pas pu prévoir qu ' il disparaîtrait.
Using ' <brk> ' to separate context and current inputs.
# 1. (CTI |kl_divergence| > 0.14, CCI |saliency| > 0.71)
Contextless output: Malheureusement, nous n ' avons pas pu prévoir qu ' il disparaîtrait.
Current output: Malheureusement, nous n ' avons pas pu prévoir qu ' il(0.412) disparaîtrait.
Input context: Did I mention we stole a cow(1.524) ? A beautiful animal(1.472), truly. We brought it to the stable and kept it
there for ages.
Wenn Sie die CLI zum Ausführen eines regulären Modells verwenden, ist ein zusätzlicher Schritt erforderlich, um die Position des Kontextbruchs bei der Modellgenerierung anzugeben, wenn eine Ausgabe nicht vom Benutzer erzwungen wird. Hier ist ein Beispiel mit dem regulären mBART-50-Modell vom HF Hub:
pecore -viz
--model_name facebook/mbart-large-50-one-to-many-mmt
--input_lang eng --output_lang fra --model_type mbart50-1toM
--impute_with_contextless_output
--force_context_aware_output_prefix
--input " Did I mention we stole a cow? A beautiful animal, truly. We brought it to the stable and kept it there for ages.<brk> Sadly, we could not foresee it would disappear. "
Der Benutzer wird mit der folgenden Meldung aufgefordert:
The following output was generate by the model: J’ai mentionné que nous avons volé une vache, c’est vraiment un beau animal, que nous avons emmené à l’élevage et que nous l’avons gardée pendant des époques. Malheureusement, nous n’avons pas pu prévoir qu’elle disparaîtrait.
Rewrite it here by adding ' <brk> ' wherever appropriate to mark context break:
Der Benutzer kann die Ausgabe dann umschreiben, indem er an beliebiger Stelle <brk>
hinzufügt, um den Kontextumbruch zu markieren:
J’ai mentionné que nous avons volé une vache, c’est vraiment un beau animal, que nous avons emmené à l’élevage et que nous l’avons gardée pendant des époques. < brk > Malheureusement, nous n’avons pas pu prévoir qu’elle disparaîtrait.
Die endgültige Ausgabe wird sein:
Context with contextual cues (std λ=1.00) followed by output sentence
with context-sensitive target spans (std λ=1.00):
Input context: Did I mention we stole a cow ? A beautiful animal, truly. We brought it to the stable and kept it there for ages.
Input current: Sadly, we could not foresee it would disappear.
Output context: J’ai mentionné que nous avons volé une vache, c’est vraiment un beau animal, que nous avons emmené à l’élevage et que nous l’avons gardée pendant
des époques.
Context-aware output: J’ai mentionné que nous avons volé une vache, c’est vraiment un beau animal, que nous avons emmené à l’élevage et que nous l’avons gardée
pendant des époques. Malheureusement, nous n’avons pas pu prévoir qu’elle disparaîtrait.
Using language tags for model type ' mbart50-1toM ' (eng - > fra).
# 1. (CTI |kl_divergence| > 1.08, CCI |saliency| > 0.00)
Contextless output: Malheureusement, nous n ' avons pas pu prévoir sa disparition.
Current output: Malheureusement, nous n’(3.505)avons pas pu prévoir qu’elle disparaîtrait.
Input context: Did I mention we stole a cow? A beautiful animal, truly. We brought it to the stable(0.002) and kept it there for ages.
Output context: J’(0.004)ai mentionné que nous avons volé une vache, c’(0.002)est vraiment un beau animal, que nous avons emmené à l’(0.003)élevage et que nous
l’(0.007)avons gardée pendant des époques.
In diesem Fall sehen wir, dass sich das Modell dafür entscheidet, den gekrümmten Apostroph '
anstelle des geraden Apostrophs zu generieren '
der standardmäßig in der kontextlosen Ausgabe verwendet wird, um den Ausgabekontextstil beizubehalten und dieses Zeichen mehrmals zu verwenden (von pecore als kontextbezogene Hinweise identifiziert). .
In diesem Beispiel verwenden wir das Aufmerksamkeitsgewicht von Kopf 8 in Schicht 5, um Kontextabhängigkeit zuzuordnen. Es wurde empirisch festgestellt, dass dieser Kopf gut mit der menschlichen Intuition übereinstimmt.
pecore -viz
--model_name context-mt/scat-marian-small-ctx4-cwd1-en-fr
--attributions_aggregate_fns mean mean
--model_use_ctx_break
--impute_with_contextless_output
--force_context_aware_output_prefix
--input " Did I mention we stole a cow? A beautiful animal, truly. We brought it to the stable and kept it there for ages.<brk> Sadly, we could not foresee it would disappear. "
--attribution_method attention
--select_attributions_idx 7 4
python scripts/translate.py
--model_type mbart50-1toM
--model_id mbart50-1toM-scat
--model_name context-mt/scat-mbart50-1toM-ctx4-cwd1-en-fr
--context_size 4
--dataset scat
--context_word_dropout 1
python scripts/translate.py
--model_type marian-big
--model_id marian-big-scat-target
--model_name context-mt/scat-marian-big-target-ctx4-cwd0-en-fr
--context_size 4
--dataset disc_eval_mt
--context_word_dropout 0
--dataset_config anaphora
python scripts/translate.py
--model_type marian-big
--model_id marian-big-scat-target
--model_name context-mt/scat-marian-big-target-ctx4-cwd0-en-fr
--context_size 4
--dataset disc_eval_mt
--context_word_dropout 0
--dataset_config lexical-choice
python scripts/translate.py
--model_type marian-big
--model_id marian-big-scat
--model_name context-mt/scat-marian-big-ctx4-cwd1-en-fr
--context_size 4
--dataset disc_eval_mt
--context_word_dropout 1
--dataset_config anaphora
python scripts/translate.py
--model_type marian-big
--model_id marian-big-scat
--model_name context-mt/scat-marian-big-ctx4-cwd1-en-fr
--context_size 4
--dataset disc_eval_mt
--context_word_dropout 1
--dataset_config lexical-choice
python scripts/translate.py
--model_type mbart50-1toM
--model_id mbart50-1toM-scat
--model_name context-mt/scat-mbart50-1toM-ctx4-cwd1-en-fr
--context_size 0
--dataset disc_eval_mt
--context_word_dropout 0
--dataset_config lexical-choice
python scripts/evaluate_mt_outputs.py
--filepath outputs/translations/ctx/scat-marian-small-scat-target.txt
--model_id marian-small-scat-target
--dataset scat
--src_lang eng
--tgt_lang fra
--metrics bleu comet accuracy flip
--has_target_context
--max_idx 250
python scripts/evaluate_mt_outputs.py
--filepath outputs/translations/ctx/disc_eval_mt-anaphora-marian-small-scat-target.txt
--model_id marian-small-scat-target
--dataset disc_eval_mt
--src_lang eng
--tgt_lang fra
--metrics bleu comet accuracy flip
--has_target_context
--max_idx 250
python scripts/evaluate_mt_outputs.py
--filepath outputs/translations/ctx/scat-mbart50-1toM-scat.txt
--model_id mbart50-1toM-scat
--dataset scat
--src_lang eng
--tgt_lang fra
--metrics bleu comet accuracy
python scripts/evaluate_mt_outputs.py
--filepath outputs/translations/ctx/scat-mbart50-1toM-scat.txt
--model_id mbart50-1toM-scat
--dataset scat
--src_lang eng
--tgt_lang fra
--metrics comet accuracy
python scripts/generate_examples.py
--dataset scat
--model_name context-mt/scat-marian-small-target-ctx4-cwd0-en-fr
--src_lang eng
--tgt_lang fra
--model_id marian-small-scat-target
--model_type marian-small
--has_context
--has_contrast
--has_target_context
python scripts/generate_examples.py
--dataset scat
--model_name context-mt/scat-mbart50-1toM-target-ctx4-cwd0-en-fr
--src_lang eng
--tgt_lang fra
--model_id mbart50-1toM-scat-target
--model_type mbart50-1toM
--has_context
--has_target_context
--has_contrast
python scripts/generate_examples.py
--dataset disc_eval_mt
--dataset_config anaphora
--model_name context-mt/scat-marian-small-ctx4-cwd1-en-fr
--src_lang eng
--tgt_lang fra
--model_id marian-small-scat
--model_type marian-small
--has_context
--has_contrast
python scripts/generate_examples.py
--dataset scat
--model_name Helsinki-NLP/opus-mt-en-fr
--src_lang eng
--tgt_lang fra
--model_id marian-small
--model_type marian-small
--has_contrast
python scripts/tag_cti_metrics.py
--examples_path outputs/processed_examples/scat-marian-small-scat.tsv
--model_name context-mt/scat-marian-small-ctx4-cwd1-en-fr
--model_type marian-small
python scripts/tag_cti_metrics.py
--examples_path outputs/processed_examples/scat-marian-big-scat.tsv
--model_name context-mt/scat-marian-big-ctx4-cwd1-en-fr
--model_type marian-big
python scripts/tag_cti_metrics.py
--examples_path outputs/processed_examples/scat-mbart50-1toM-scat.tsv
--model_name context-mt/scat-mbart50-1toM-ctx4-cwd1-en-fr
--model_type mbart50-1toM
python scripts/tag_cti_metrics.py
--examples_path outputs/processed_examples/scat-marian-small-scat-target.tsv
--model_name context-mt/scat-marian-small-target-ctx4-cwd0-en-fr
--model_type marian-small
python scripts/tag_cti_metrics.py
--examples_path outputs/processed_examples/scat-marian-big-scat-target.tsv
--model_name context-mt/scat-marian-big-target-ctx4-cwd0-en-fr
--model_type marian-big
python scripts/tag_cti_metrics.py
--examples_path outputs/processed_examples/scat-mbart50-1toM-scat-target.tsv
--model_name context-mt/scat-mbart50-1toM-target-ctx4-cwd0-en-fr
--model_type mbart50-1toM
python scripts/tag_cci_metrics.py
--examples_path outputs/processed_examples/scat-marian-small-scat.tsv
--model_name context-mt/scat-marian-small-ctx4-cwd1-en-fr
--model_type marian-small
python scripts/tag_cci_metrics.py
--examples_path outputs/processed_examples/scat-mbart50-1toM-scat-target.tsv
--model_name context-mt/scat-mbart50-1toM-target-ctx4-cwd0-en-fr
--model_type mbart50-1toM
python scripts/evaluate_tagged_metrics.py
--scores_path outputs/scores/scat-marian-small-scat-cti.tsv
--eval_mode cti
--use_trained_model
python scripts/evaluate_tagged_metrics.py
--scores_path outputs/scores/scat-marian-small-scat-cti.tsv
--eval_mode cti
--average_example_scores
--metrics random pcxmi kl_divergence
--save_preds
python scripts/evaluate_tagged_metrics.py
--scores_path outputs/scores/scat-marian-small-scat-cci.tsv
--eval_mode cci
--example_target_column is_supporting_context
--average_example_scores
--metrics random saliency_contrast_prob_diff attention_default attention_best
python scripts/evaluate_tagged_metrics.py
--scores_path outputs/scores/scat-marian-small-scat-target-cti.tsv
--eval_mode cti
--average_example_scores
--metrics random pcxmi kl_divergence
--save_preds