加布里埃萊·薩爾蒂 • 格熱戈日·赫魯帕拉 • 馬爾維娜·尼西姆 • 阿麗安娜·比薩扎
pecore 兩步驟流程" width="300" style="max-width: 100%;"> pecore 範例" width="500" style="max-width: 100%;">
摘要:確定語言模型是否能夠以人類合理的方式使用上下文資訊對於確保其在現實世界環境中的安全採用非常重要。然而,上下文何時以及哪些部分影響模型生成的問題通常是單獨解決的,而當前的合理性評估實際上僅限於少數人為基準。為了解決這個問題,我們引入了上下文依賴的合理性評估( pecore ),這是一個端到端的可解釋性框架,旨在量化語言模型生成中的上下文使用。我們的方法利用模型內部結構來(i)對比識別生成文本中的上下文敏感目標標記,以及(ii)將它們連結到證明其預測合理的上下文線索。我們使用pecore來量化情境感知機器翻譯模型的合理性,將模型原理與人類註釋在幾種話語層面的現像中進行比較。最後,我們將我們的方法應用於未註釋的生成,以識別上下文介導的預測並突出顯示模型翻譯中(不)合理的上下文使用的實例。
該儲存庫包含與論文「量化神經機器翻譯中上下文依賴的合理性」相關的腳本和筆記本。如果您在工作中使用以下任何內容,我們懇請您引用我們的論文:
@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 "
}
提示
您可以從我們的 Hugging Face Spaces 線上演示中嘗試pecore 。
雖然此儲存庫實現了上述論文實驗評估中使用的功能,但我們透過 Inseq 可解釋性庫提供了pecore的新 CLI 實作。我們強烈建議研究人員採用該實現,因為它更加穩健和通用,支援 Huggingface 庫中的所有僅解碼器和編碼器-解碼器模型,以進行輸入和輸出上下文相關性檢測和歸因。有關更多詳細信息,請參閱 Inseq 自述文件中的inseq attribute-context
部分。
論文的所有工件,包括微調模型和訓練/評估資料集,都可以在pecore HuggingFace Collection 中找到。試玩版即將推出,敬請期待!
上下文感知 NMT 模型使用train_context_aware_mt_model.py
腳本進行訓練。該腳本是原始run_translation_no_trainer.py
的修改。此腳本添加以下欄位用於上下文模型訓練:
context_size
:用於訓練的上下文句子的數量。預設值為 0(句子級訓練)。
sample_context
:如果設定,則每個範例的上下文大小將從 0 到context_size
(含)之間的均勻分佈進行採樣。如果未傳遞且context_size
大於 0,則上下文大小始終等於context_size
。
context_word_dropout
:從上下文中刪除單字的機率在 0 到 1 之間。預設值為 0(無遺失)。
use_target_context
:如果設置,上下文也包含在訓練損失的翻譯文字中。在這種情況下,輸入src_ctx <brk> src
的輸出格式將變成tgt_ctx <brk> tgt
。否則輸出格式為tgt
(僅翻譯src
)。
以下是在上下文增強的 IWSLT17 資料集上微調 mBART 1-to-50 模型的範例,其中包含最多 4 個上下文句子和 10% 的上下文單字遺失:
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
以下是在 SCAT 訓練部分上繼續微調上下文感知 En->Fr OpusMT 模型的範例,最多有 4 個上下文句子和 10% 的上下文單字遺失:
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
pecore CLI 是一個命令列介面,用於在給定模型和資料集上運行pecore步驟。 CLI 在pecore /cli.py
腳本中實現,並且可以在使用 pip install -e 安裝軟體包時用作pecore -viz
pip install -e .
。目前的實作支援 Inseq 框架支援的所有編碼器-解碼器模型的上下文相關目標 (CTI) 識別和上下文線索插補 (CCI),包括帶有語言前綴標籤的模型(mBART-50、NLLB、M2M100)以及使用特殊上下文標籤訓練的模型(例如,在HF Hub 上的context-mt 組織中找到的模型集合)。 CLI 可用於在給定模型和範例上運行pecore步驟,如下所示:
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. "
上面的範例產生以下輸出,正確突出顯示上下文中代名詞“il”對名詞“cow”和“animal”的依賴性。
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.
當使用 CLI 運行常規模型時,如果使用者不強制輸出,則需要執行額外的步驟來指定模型產生中上下文中斷的位置。以下是使用 HF Hub 中的常規 mBART-50 型號的範例:
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. "
將提示用戶以下訊息:
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:
然後,使用者可以透過在適當的位置添加<brk>
來重寫輸出以標記上下文中斷:
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.
最終輸出將是:
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.
在這種情況下,我們看到模型選擇產生彎曲的撇號'
而不是預設在無上下文輸出中使用的直撇號'
以堅持輸出上下文樣式,並多次使用該字元(由pecore識別為上下文線索) 。
在這個例子中,我們使用第 5 層中頭 8 的注意力權重來歸因上下文依賴性。根據經驗發現,這個頭部與人類的直覺非常吻合。
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