가브리엘레 사르티 • 그르제고르츠 흐루팔라 • 말비나 니심 • 아리아나 비사차
pecore 2단계 프로세스" 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 README의 inseq attribute-context
섹션을 참조하세요.
미세 조정된 모델 및 훈련/평가 데이터 세트를 포함한 논문의 모든 아티팩트는 pecore HuggingFace 컬렉션에서 사용할 수 있습니다. 곧 데모가 공개될 예정이니 계속 지켜봐 주시기 바랍니다!
상황 인식 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
만 번역됨).
다음은 최대 4개의 문맥 문장과 10% 문맥 단어 삭제를 사용하여 문맥 증강 IWSLT17 데이터세트에서 mBART 1~50 모델을 미세 조정하는 예입니다.
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
다음은 최대 4개의 문맥 문장과 10%의 문맥 단어 삭제를 사용하여 SCAT의 훈련 부분에서 상황 인식 En->Fr OpusMT 모델의 미세 조정을 계속하는 예입니다.
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 .
. 현재 구현에서는 언어 접두사 태그(mBART-50, NLLB, M2M100)가 있는 모델을 포함하여 Inseq 프레임워크에서 지원하는 모든 인코더-디코더 모델에 대해 상황별 대상(CTI) 식별 및 상황별 단서(CCI) 대체를 지원합니다. 특수 컨텍스트 태그로 훈련된 모델(예: 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. "
위의 예는 문맥에서 명사 "cow" 및 "animal"에 대한 대명사 "il"에 대한 종속성을 올바르게 강조하는 다음 출력을 생성합니다.
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