So, currently, I am using the raptorkwok/cantonese-chinese-translation, which is a model that is pre-trained for cantonese to chinese translation (modified version of fnlp/bart-base-chinese
Now, the issue is with how the predicted values are generated for each evaluation, and it gets worse and worse.
Firstly, this is my code
from datasets import load_dataset
model_name = "raptorkwok/cantonese-chinese-translation"
dataset = load_dataset("classified")
dataset = dataset["train"].train_test_split(test_size=0.2)
print(dataset["test"][0])
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)
source_lang = "yue"
target_lang = "zh"
prefix = "translate canto to written: "
def preprocess_function(examples):
inputs = [example[source_lang] for example in examples["translation"]]
targets = [example[target_lang] for example in examples["translation"]]
model_inputs = tokenizer(inputs, text_target=targets, max_length=512, truncation=True)
return model_inputs
tokenized_dataset = dataset.map(preprocess_function, batched=True)
from transformers import DataCollatorForSeq2Seq
data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model_name)
import evaluate
metric = evaluate.load("sacrebleu")
import numpy as np
def postprocess_text(preds, labels):
preds = [pred.strip() for pred in preds]
labels = [[label.strip()] for label in labels]
return preds, labels
def compute_metrics(eval_preds):
preds, labels = eval_preds
if isinstance(preds, tuple):
preds = preds[0]
#preds = preds[0]
print(preds)
print(labels)
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
decoded_preds, decoded_labels = postprocess_text(decoded_preds, decoded_labels)
print("predicted: " + str(decoded_preds))
print("answer: " + str(decoded_labels))
result = metric.compute(predictions=decoded_preds, references=decoded_labels)
result = {"bleu": result["score"]}
print(result)
prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds]
result["gen_len"] = np.mean(prediction_lens)
result = {k: round(v, 4) for k, v in result.items()}
return result
from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
training_args = Seq2SeqTrainingArguments(
output_dir='./Results',
logging_steps=2,
evaluation_strategy="steps",
learning_rate=1e-3,
per_device_train_batch_size=4,
per_device_eval_batch_size=4,
weight_decay=0.01,
save_total_limit=3,
num_train_epochs=45,
warmup_steps=20,
save_steps=20,
eval_steps=4,
gradient_accumulation_steps=1,
push_to_hub=False,
report_to=["tensorboard"],
metric_for_best_model= "eval_bleu",
greater_is_better=True,
predict_with_generate=True
)
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
tokenizer=tokenizer,
data_collator=data_collator,
train_dataset=tokenized_dataset["train"],
eval_dataset=tokenized_dataset["test"],
compute_metrics=compute_metrics
)
trainer.train()
The issue with the prediction values is that the values will either be direct answers from the training dataset [case 1], or just completely blank [case 2].
Case 1 example:
predicted: [‘能 否 真 的 趕 及 任 期 完 成 所 有 工 作’, ‘能 否 真 的 趕 及 任 期 完 成 所 有 工 作’, ‘能 否 真 的 趕 及 任 期 完 成 所 有 工 作’, ‘能 否
真 的 趕 及 任 期 完 成 所 有 工 作’, ‘能 否 真 的 趕 及 任 期 完 成 所 有 工 作’, ‘能 否 真 的 趕 及 任 期 完 成 所 有 工 作’, ‘能 否 真 的 趕 及 任 期 完 成 所 有 工 作’, ‘能 否 真 的 趕 及 任 期 完 成 所 有 工 作’]
answer: [[‘回 溯 至 本 屆 第 一 份 施 政 報 告 , 那 時 是…’], [‘但 林 太 聽 了 行 政 會 議 成 員 之 中’], [‘以 及 希 望 達 到 甚 麼 效 果’], [‘在 創 科 方 面 , 她 提 出 八 大 發 展 方 向’], [‘我 們 跟 大 家 一 起 偵 測 一 下’], [‘從 一 份 發 展 至 下 一 份 也 有 很 多 不 同 議 題’], [‘即 是 除 了 因 為 競 選 結 束’], [‘「 一 起 同 行 , 擁 抱 希 望 , 分 享 快 樂 」’]]
Case 2 example:
predicted: [‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’]
answer: [[‘回 溯 至 本 屆 第 一 份 施 政 報 告 , 那 時 是…’], [‘但 林 太 聽 了 行 政 會 議 成 員 之 中’], [‘以 及 希 望 達 到 甚 麼 效 果’], [‘在 創 科 方 面 , 她 提 出 八 大 發 展 方 向’], [‘我 們 跟 大 家 一 起 偵 測 一 下’], [‘從 一 份 發 展 至 下 一 份 也 有 很 多 不 同 議 題’], [‘即 是 除 了 因 為 競 選 結 束’], [‘「 一 起 同 行 , 擁 抱 希 望 , 分 享 快 樂 」’]]
But one thing you might notice as well, is that the predicted value for each sentence has the same string regardless. This doesn’t happen at the start, but happens after a few evaluations, and will remain like this through the whole training process.
Can anyone find the source of the issue, I am not familiar with huggingface and I am struggling to find what’s wrong, thanks.