Can you implement ROUGE in this code: [code]
- User
Can you implement ROUGE in this code:
code - Assistant
- User
- Assistant
Can you implement ROUGE in this code:
# Install Required Libraries
!pip install --upgrade transformers
!pip install datasets
!pip install evaluate
!pip install sacrebleu
!pip install bert_score
# Import Libraries
import evaluate
import torch
import numpy as np
from transformers import (
T5Tokenizer,
T5ForConditionalGeneration,
Seq2SeqTrainer,
Seq2SeqTrainingArguments,
DataCollatorForSeq2Seq,
)
from datasets import load_dataset, concatenate_datasets
from collections import defaultdict
from nltk import ngrams
import nltk
# Download NLTK data (if not already downloaded)
nltk.download('punkt')
# Load and Prepare the Dataset
dataset1 = load_dataset("danielv835/personal_finance_v0.2")
# Remove the 'rejected' column from dataset1
dataset1 = dataset1.remove_columns(['rejected'])
# Load the tokenizer
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
# Tokenization function without padding
def tokenize_function(examples):
model_inputs = tokenizer(
examples["context"],
max_length=256,
truncation=True,
padding=False
)
with tokenizer.as_target_tokenizer():
labels = tokenizer(
examples["chosen"],
max_length=256,
truncation=True,
padding=False
)
model_inputs["labels"] = labels["input_ids"]
return model_inputs
# Tokenize both datasets
tokenized_datasets1 = dataset1.map(tokenize_function, batched=True)
# Load the pre-trained FLAN-T5 model for conditional generation
tw_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
# Define the data collator
data_collator = DataCollatorForSeq2Seq(
tokenizer=tokenizer,
model=tw_model,
label_pad_token_id=-100, # Explicitly set label padding
padding="longest",
return_tensors="pt"
)
# Ensure the pad_token_id is set
tw_model.config.pad_token_id = tokenizer.pad_token_id
# Define training arguments for the Trainer API
training_args = Seq2SeqTrainingArguments(
output_dir="./trainer_wheel_model", # Output directory for saving model checkpoints
eval_strategy="epoch", # Evaluate at the end of every epoch
learning_rate=5e-5, # Set model learning rate
per_device_train_batch_size=8, # Adjust batch size for your GPU/CPU
per_device_eval_batch_size=4,
gradient_accumulation_steps=2, # Use gradient accumulation
save_steps=500, # Save model every 500 steps
save_total_limit=2, # Limit number of saved checkpoints
logging_dir='./logs', # Directory for storing logs
logging_steps=200, # Log every 200 steps
num_train_epochs=3, # Number of training epochs
fp16=False, # Disable mixed precision
predict_with_generate=True, # Generate predictions during evaluation
generation_max_length=256, # Maximum length of generated sequences
generation_num_beams=5, # Number of beams for beam search
)
# Define the refined F1 Score with n-gram overlaps and weighting
def f1_score_refined(preds, labels, max_n=3, weights=None):
"""
Calculate a weighted F1 score based on n-gram overlaps.
Args:
preds (list of str): Predicted texts.
labels (list of str): Ground truth texts.
max_n (int): Maximum n-gram to consider.
weights (dict): Weights for each n-gram level. E.g., {1:1.0, 2:1.5, 3:2.0}
Returns:
float: Weighted F1 score.
"""
if weights is None:
# Default weights: higher n-grams have higher weights
weights = {1: 1.0, 2: 1.5, 3: 2.0}
total_f1 = 0.0
for pred, label in zip(preds, labels):
pred = nltk.word_tokenize(pred.lower())
label = nltk.word_tokenize(label.lower())
f1 = 0.0
for n in range(1, max_n + 1):
pred_ngrams = set(ngrams(pred, n))
label_ngrams = set(ngrams(label, n))
common_ngrams = pred_ngrams.intersection(label_ngrams)
if not pred_ngrams or not label_ngrams:
precision = 0.0
recall = 0.0
else:
precision = len(common_ngrams) / len(pred_ngrams)
recall = len(common_ngrams) / len(label_ngrams)
if precision + recall == 0:
f1_n = 0.0
else:
f1_n = 2 * precision * recall / (precision + recall)
# Apply weighting for the n-gram level
f1 += weights.get(n, 1.0) * f1_n
# Normalize the f1 by the sum of weights to keep it in a reasonable range
total_weight = sum(weights.get(n, 1.0) for n in range(1, max_n + 1))
f1 /= total_weight
total_f1 += f1
return total_f1 / len(preds) if len(preds) > 0 else 0
# Load required metrics
metric_bert = evaluate.load("bertscore")
metric_meteor = evaluate.load("meteor")
# Define the compute_metrics function
def compute_metrics(eval_pred):
preds, labels = eval_pred
# If logits are returned as a tuple, extract the first element
if isinstance(preds, tuple):
preds = preds[0]
# Replace -100 in preds and the labels as we can't decode them
preds = np.where(preds != -100, preds, tokenizer.pad_token_id)
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
# Decode the predicted tokens into text
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
# Post-process predictions and labels
decoded_preds = [pred.strip() for pred in decoded_preds]
decoded_labels = [label.strip() for label in decoded_labels]
# Compute Refined F1 Score
refined_f1 = f1_score_refined(decoded_preds, decoded_labels)
# Compute METEOR
meteor_result = metric_meteor.compute(predictions=decoded_preds, references=decoded_labels)
meteor_score = meteor_result["meteor"]
# Compute BERTScore (using default 'bert-base-uncased' model)
bert_result = metric_bert.compute(predictions=decoded_preds, references=decoded_labels, lang="en")
bert_score_f1 = np.mean(bert_result["f1"])
# Combine the metrics
result = {
"refined_f1": refined_f1 * 100,
"meteor": meteor_score * 100, # Express METEOR as percentage
"bert_score_f1": bert_score_f1 * 100 # Express BERTScore F1 as percentage
}
return result
# Initialize the Trainer instance
trainer = Seq2SeqTrainer(
model=tw_model, # Model to be trained
args=training_args, # Training arguments
train_dataset=tokenized_datasets1["train"], # Training dataset
eval_dataset=tokenized_datasets1["test"], # Evaluation dataset
data_collator=data_collator, # Data collator for batching
tokenizer=tokenizer, # Tokenizer for encoding/decoding
compute_metrics=compute_metrics, # Function to compute evaluation metrics
)
# Empty the cache before starting the training process
torch.cuda.empty_cache()
# Start training the model
print("Starting training...")
trainer.train()
print("Training completed!")
# Clear GPU memory cache after training
torch.cuda.empty_cache()
# Evaluate the Fine-Tuned Model
eval_results = trainer.evaluate()
print(f"Final evaluation loss: {eval_results['eval_loss']}")
To implement the ROUGE metric in the provided code, you can use the evaluate library which has built-in support for ROUGE. Below, I'll show you how to integrate ROUGE into your existing code.
You need to add the ROUGE metric to the metrics you are using.
After decoding the predictions and labels, you'll compute the ROUGE scores in the compute_metrics function alongside the other metrics.
Here's how you can modify the code:
# Load required metrics
metric_bert = evaluate.load("bertscore")
metric_meteor = evaluate.load("meteor")
metric_rouge = evaluate.load("rouge") # Load ROUGE metric
# Define the compute_metrics function
def compute_metrics(eval_pred):
preds, labels = eval_pred
# If logits are returned as a tuple, extract the first element
if isinstance(preds, tuple):
preds = preds[0]
# Replace -100 in preds and the labels as we can't decode them
preds = np.where(preds != -100, preds, tokenizer.pad_token_id)
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
# Decode the predicted tokens into text
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
# Post-process predictions and labels
decoded_preds = [pred.strip() for pred in decoded_preds]
decoded_labels = [label.strip() for label in decoded_labels]
# Compute Refined F1 Score
refined_f1 = f1_score_refined(decoded_preds, decoded_labels)
# Compute METEOR
meteor_result = metric_meteor.compute(predictions=decoded_preds, references=decoded_labels)
meteor_score = meteor_result["meteor"]
# Compute BERTScore (using default 'bert-base-uncased' model)
bert_result = metric_bert.compute(predictions=decoded_preds, references=decoded_labels, lang="en")
bert_score_f1 = np.mean(bert_result["f1"])
# Compute ROUGE
rouge_result = metric_rouge.compute(predictions=decoded_preds, references=decoded_labels)
rouge_score = {
"rouge1": np.mean(rouge_result["rouge1"].fmeasure) * 100,
"rouge2": np.mean(rouge_result["rouge2"].fmeasure) * 100,
"rougeL": np.mean(rouge_result["rougeL"].fmeasure) * 100
}
# Combine the metrics
result = {
"refined_f1": refined_f1 * 100,
"meteor": meteor_score * 100, # Express METEOR as percentage
"bert_score_f1": bert_score_f1 * 100, # Express BERTScore F1 as percentage
"rouge1": rouge_score["rouge1"],
"rouge2": rouge_score["rouge2"],
"rougeL": rouge_score["rougeL"]
}
return result
Load ROUGE Metric: Add metric_rouge = evaluate.load("rouge") to load the ROUGE metric.
Compute ROUGE Scores:
rouge_result = metric_rouge.compute(predictions=decoded_preds, references=decoded_labels)
rouge_score = {
"rouge1": np.mean(rouge_result["rouge1"].fmeasure) * 100,
"rouge2": np.mean(rouge_result["rouge2"].fmeasure) * 100,
"rougeL": np.mean(rouge_result["rougeL"].fmeasure) * 100
}
Update Result Dictionary: Include the ROUGE scores in the final result dictionary to output them after training.
Now, when you run the training and evaluation process, you will receive the ROUGE scores as part of your output metrics.
AttributeError Traceback (most recent call last)
<ipython-input-6-c5f4dfb62460> in <cell line: 32>()
30 # Start training the model
31 print("Starting training...")
---> 32 trainer.train()
33 print("Training completed!")
7 frames
/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
2050 hf_hub_utils.enable_progress_bars()
2051 else:
-> 2052 return inner_training_loop(
2053 args=args,
2054 resume_from_checkpoint=resume_from_checkpoint,
/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in _inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
2485
2486 self.control = self.callback_handler.on_epoch_end(args, self.state, self.control)
-> 2487 self._maybe_log_save_evaluate(tr_loss, grad_norm, model, trial, epoch, ignore_keys_for_eval)
2488
2489 if DebugOption.TPU_METRICS_DEBUG in self.args.debug:
/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in _maybe_log_save_evaluate(self, tr_loss, grad_norm, model, trial, epoch, ignore_keys_for_eval)
2913 metrics = None
2914 if self.control.should_evaluate:
-> 2915 metrics = self._evaluate(trial, ignore_keys_for_eval)
2916
2917 if self.control.should_save:
/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in _evaluate(self, trial, ignore_keys_for_eval, skip_scheduler)
2870
2871 def _evaluate(self, trial, ignore_keys_for_eval, skip_scheduler=False):
-> 2872 metrics = self.evaluate(ignore_keys=ignore_keys_for_eval)
2873 self._report_to_hp_search(trial, self.state.global_step, metrics)
2874
/usr/local/lib/python3.10/dist-packages/transformers/trainer_seq2seq.py in evaluate(self, eval_dataset, ignore_keys, metric_key_prefix, **gen_kwargs)
178 self.gather_function = self.accelerator.gather
179 self._gen_kwargs = gen_kwargs
--> 180 return super().evaluate(eval_dataset, ignore_keys=ignore_keys, metric_key_prefix=metric_key_prefix)
181
182 def predict(
/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in evaluate(self, eval_dataset, ignore_keys, metric_key_prefix)
3866
3867 eval_loop = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
-> 3868 output = eval_loop(
3869 eval_dataloader,
3870 description="Evaluation",
/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in evaluation_loop(self, dataloader, description, prediction_loss_only, ignore_keys, metric_key_prefix)
4158 )
4159 else:
-> 4160 metrics = self.compute_metrics(EvalPrediction(predictions=all_preds, label_ids=all_labels))
4161 elif metrics is None:
4162 metrics = {}
<ipython-input-5-4f982335b7a5> in compute_metrics(eval_pred)
126 rouge_result = metric_rouge.compute(predictions=decoded_preds, references=decoded_labels)
127 rouge_score = {
--> 128 "rouge1": np.mean(rouge_result["rouge1"].fmeasure) * 100,
129 "rouge2": np.mean(rouge_result["rouge2"].fmeasure) * 100,
130 "rougeL": np.mean(rouge_result["rougeL"].fmeasure) * 100
AttributeError: 'numpy.float64' object has no attribute 'fmeasure'
The error you're encountering suggests that the output of the metric_rouge.compute() function is different from what was expected. In current versions of the evaluate library, the rouge metric's output might return raw numeric scores (e.g., float values) instead of structured objects containing attributes like fmeasure.
To address the issue, you may need to adjust how the ROUGE scores are accessed. Here's how to modify the computation to correctly extract the scores:
fmeasure, you should directly use the scores returned by the metric_rouge.compute().Here is the revised section of your compute_metrics function:
# Compute ROUGE
rouge_result = metric_rouge.compute(predictions=decoded_preds, references=decoded_labels)
rouge_score = {
"rouge1": rouge_result["rouge1"].score * 100, # Access score directly
"rouge2": rouge_result["rouge2"].score * 100, # Access score directly
"rougeL": rouge_result["rougeL"].score * 100 # Access score directly
}
compute_metrics FunctionCombining all of the changes, your complete compute_metrics function should now look like this:
# Define the compute_metrics function
def compute_metrics(eval_pred):
preds, labels = eval_pred
# If logits are returned as a tuple, extract the first element
if isinstance(preds, tuple):
preds = preds[0]
# Replace -100 in preds and the labels as we can't decode them
preds = np.where(preds != -100, preds, tokenizer.pad_token_id)
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
# Decode the predicted tokens into text
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
# Post-process predictions and labels
decoded_preds = [pred.strip() for pred in decoded_preds]
decoded_labels = [label.strip() for label in decoded_labels]
# Compute Refined F1 Score
refined_f1 = f1_score_refined(decoded_preds, decoded_labels)
# Compute METEOR
meteor_result = metric_meteor.compute(predictions=decoded_preds, references=decoded_labels)
meteor_score = meteor_result["meteor"]
# Compute BERTScore (using default 'bert-base-uncased' model)
bert_result = metric_bert.compute(predictions=decoded_preds, references=decoded_labels, lang="en")
bert_score_f1 = np.mean(bert_result["f1"])
# Compute ROUGE
rouge_result = metric_rouge.compute(predictions=decoded_preds, references=decoded_labels)
rouge_score = {
"rouge1": rouge_result["rouge1"].score * 100, # Accessing score directly
"rouge2": rouge_result["rouge2"].score * 100, # Accessing score directly
"rougeL": rouge_result["rougeL"].score * 100 # Accessing score directly
}
# Combine the metrics
result = {
"refined_f1": refined_f1 * 100,
"meteor": meteor_score * 100, # Express METEOR as percentage
"bert_score_f1": bert_score_f1 * 100, # Express BERTScore F1 as percentage
"rouge1": rouge_score["rouge1"],
"rouge2": rouge_score["rouge2"],
"rougeL": rouge_score["rougeL"]
}
return result
score property (as opposed to fmeasure) fixes the AttributeError.evaluate library to ensure compatibility with these changes.Not yet included in a published release.