부제 : ChatGPT 를 이용해 10분만에 이해하는 COMET
Crosslingual Optimized Metric for Evaluation of Translation
COMET은 크게 세 가지 버전으로 나뉨.
COMET (reference-based)
→ source, MT, reference 삼자를 입력으로 사용해 품질을 예측.
COMET-QE (Quality Estimation)
→ source와 MT만 보고 reference 없이 예측.
COMETKiwi (word-level QE)
→ 각 단어 단위의 품질 추정까지 지원.
COMET은 OpenKiwi와 유사하게, regression task로 훈련되며 평가 기준은 인간 품질 평가 (e.g., DA scores, MQM 등)와 정합되는지를 본다.
COMET은 Transformer 기반 multilingual encoder (주로 XLM-RoBERTa)을 backbone으로 활용해 다음과 같은 입력을 인코딩한다:
입력 형태
모델은 다음과 같이 세 문장을 <SRC>
, <MT>
, <REF>
라는 prefix와 함께 연결해서 인코더에 넣는다.
<S> source_sentence </S> <T> mt_sentence </T> <R> ref_sentence </R>
이제 인코더는 각 문장의 contextual embedding을 학습하게 되며, 최종적으로 다음과 같이 sentence-level representation을 얻는다:
여기서 은 주로 [CLS]
token 또는 mean pooling을 의미하고, 는 encoder output.
이후 sentence embedding 간의 관계를 입력으로 삼는 MLP 회귀 모델을 구성해 DA score (e.g., 0~1)을 예측한다.
주로 입력 feature로는 다음이 사용된다.
여기서 는 element-wise multiplication, 는 concatenation을 의미해.
이 feature를 MLP에 넣고, 출력 를 예측값으로 사용:
Loss는 일반적으로 MSE:
지표 | 값의 범위 | 의미 |
---|---|---|
BLEU | 0.0 ~ 1.0 (또는 0~100) | 정답과 n-gram이 얼마나 겹치는가 |
COMET | 이론상 제한 없음 (보통 -1 ~ +1 사이) | 의미 기반 품질 예측, 인간 평가 점수에 가까움 |
실험상 COMET 점수는 보통 -1 ~ +1 사이에 분포함.
하지만! 이건 모델이 훈련된 범위에 따라 다를 수 있는데, 예를 들어:
"ref": "The dog is sleeping on the couch."
"mt_1": "The dog sleeps on the sofa." → COMET score: **0.91**
"mt_2": "The sofa is sleeping on the dog." → COMET score: **-0.3**
"mt_3": "asdf qwerty uiop." → COMET score: **-0.9**
HuggingFace에서 COMET을 바로 사용 가능!
pip install unbabel-comet
또는 최신 Hugging Face 지원 버전으로 패키지 설치
pip install git+https://github.com/Unbabel/COMET@master
from comet import download_model, load_from_checkpoint
# 최신 COMET 모델 다운로드 (WMT22에서 훈련된 DA 기준)
model_path = download_model("Unbabel/wmt22-comet-da")
model = load_from_checkpoint(model_path)
# 평가할 문장들 (한국어 원문, 기계 번역 결과, 참고 번역)
data = [
{
"src": "나는 오늘 아침에 커피를 마셨다.", # 한국어 원문 (Source)
"mt": "I drank coffee this morning.", # 기계 번역 결과 (MT)
"ref": "I had coffee this morning." # 참고 번역 (정답(Reference))
},
{
"src": "그녀는 책을 읽는 것을 좋아한다.",
"mt": "She likes reading books.",
"ref": "She enjoys reading books."
},
{
"src": "날씨가 좋기 때문에 우리는 소풍을 갔다.",
"mt": "Because the weather was nice, we went on a picnic.",
"ref": "We went for a picnic because the weather was nice."
}
]
# 품질 평가 수행
results = model.predict(data, batch_size=8, gpus=0) # GPU 사용 시 gpus=1
# 결과 출력
for i, result in enumerate(results["scores"]):
print(f"Example {i+1} → COMET Score: {result:.4f}")
출력
Fetching 5 files: 100%|██████████| 5/5 [00:00<00:00, 100342.20it/s]
Lightning automatically upgraded your loaded checkpoint from v1.8.3.post1 to v2.5.1.post0. To apply the upgrade to your files permanently, run `python -m pytorch_lightning.utilities.upgrade_checkpoint ../.cache/huggingface/hub/models--Unbabel--wmt22-comet-da/snapshots/2760a223ac957f30acfb18c8aa649b01cf1d75f2/checkpoints/model.ckpt`
Encoder model frozen.
Using default `ModelCheckpoint`. Consider installing `litmodels` package to enable `LitModelCheckpoint` for automatic upload to the Lightning model registry.
GPU available: True (mps), used: True
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
Predicting DataLoader 0: 100%|██████████| 1/1 [00:04<00:00, 4.45s/it]
Example 1 → COMET Score: 0.9587
Example 2 → COMET Score: 0.9689
Example 3 → COMET Score: 0.9509
from comet import download_model, load_from_checkpoint
# reference-free 버전인 COMET-QE 모델 다운로드
model_path = download_model("Unbabel/wmt20-comet-qe-da") # 또는 최신 QE 모델
model = load_from_checkpoint(model_path)
# 평가할 데이터: source와 MT만 제공 (reference 없음!)
data = [
{
"src": "나는 오늘 아침에 커피를 마셨다.",
"mt": "I drank coffee this morning."
},
{
"src": "그녀는 책을 읽는 것을 좋아한다.",
"mt": "She likes reading books."
},
{
"src": "날씨가 좋기 때문에 우리는 소풍을 갔다.",
"mt": "Because the weather was nice, we went on a picnic."
}
]
# 품질 예측 수행 (reference 없이!)
results = model.predict(data, batch_size=8, gpus=0)
# 결과 출력
for i, score in enumerate(results["scores"]):
print(f"Example {i+1} → COMET-QE Score: {score:.4f}")
출력
Fetching 5 files: 100%|██████████| 5/5 [02:32<00:00, 30.56s/it]
Lightning automatically upgraded your loaded checkpoint from v1.3.5 to v2.5.1.post0. To apply the upgrade to your files permanently, run `python -m pytorch_lightning.utilities.upgrade_checkpoint ../.cache/huggingface/hub/models--Unbabel--wmt20-comet-qe-da/snapshots/2e7ffc84fb67d99cf92506611766463bb9230cfb/checkpoints/model.ckpt`
Encoder model frozen.
/Users/judy/Downloads/myenv/lib/python3.12/site-packages/pytorch_lightning/core/saving.py:195: Found keys that are not in the model state dict but in the checkpoint: ['encoder.model.embeddings.position_ids']
Using default `ModelCheckpoint`. Consider installing `litmodels` package to enable `LitModelCheckpoint` for automatic upload to the Lightning model registry.
GPU available: True (mps), used: True
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
Predicting DataLoader 0: 100%|██████████| 1/1 [00:00<00:00, 1.01it/s]
Example 1 → COMET-QE Score: 0.5950
Example 2 → COMET-QE Score: 0.6835
Example 3 → COMET-QE Score: 0.2709
항목 | Reference-based COMET | COMET-QE |
---|---|---|
입력 | src , mt , ref | src , mt (❌ ref ) |
사용 모델 | "Unbabel/wmt22-comet-da" | "Unbabel/wmt20-comet-qe-da" |
태스크 종류 | 번역 평가 (DA 기반) | Quality Estimation (QE) |
특징 | 정답이 있을 때 가장 정확함 | 빠르게 reference 없이 평가 가능 |
목적 | 모델 이름 |
---|---|
일반 평가 (참조 포함) | Unbabel/wmt22-comet-da |
QE (참조 없이) | Unbabel/wmt20-comet-qe-da |
Word-level QE | Unbabel/COMET-KIWI |
참조 없이 번역 품질 분류 | Unbabel/wmt23-cometkiwi-da |
gpus
=1로 설정)outputs = model.predict(data, batch_size=4, gpus=1, return_all=True)
for output in outputs['raw']:
print(f"Score: {output['score']:.4f}, Confidence: {output['confidence']:.4f}")```