Kevin Fink commited on
Commit
d52c194
·
1 Parent(s): 12c1a6f
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -31,27 +31,32 @@ def fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size
31
  try:
32
  torch.cuda.empty_cache()
33
  torch.nn.CrossEntropyLoss()
34
- metric = evaluate.load("rouge", cache_dir='/data/cache')
35
  def compute_metrics(eval_preds):
36
  preds, labels = eval_preds
37
  if isinstance(preds, tuple):
38
  preds = preds[0]
 
39
  # Replace -100s used for padding as we can't decode them
40
- # preds = np.where(preds != -100, preds, tokenizer.pad_token_id)
41
- #preds = np.array(preds)
 
 
42
  decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
43
- if isinstance(labels, tuple):
44
- labels = labels[0]
45
- #labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
46
- #labels = np.array(labels)
47
  decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
48
-
49
- result = metric.compute(predictions=decoded_preds, references=decoded_labels, use_stemmer=True)
 
50
  result = {k: round(v * 100, 4) for k, v in result.items()}
 
 
 
 
 
 
51
  prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds]
52
  result["gen_len"] = np.mean(prediction_lens)
53
- accuracy = accuracy_score(decoded_labels, decoded_preds)
54
- result["eval_accuracy"] = round(accuracy * 100, 4)
55
  return result
56
 
57
  login(api_key.strip())
@@ -226,8 +231,8 @@ def fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size
226
  trainer = Trainer(
227
  model=model,
228
  args=training_args,
229
- train_dataset=dataset['train'],
230
- eval_dataset=dataset['validation'],
231
  compute_metrics=compute_metrics,
232
  #data_collator=data_collator,
233
  processing_class=tokenizer,
 
31
  try:
32
  torch.cuda.empty_cache()
33
  torch.nn.CrossEntropyLoss()
34
+ rouge_metric = evaluate.load("rouge", cache_dir='/data/cache')
35
  def compute_metrics(eval_preds):
36
  preds, labels = eval_preds
37
  if isinstance(preds, tuple):
38
  preds = preds[0]
39
+
40
  # Replace -100s used for padding as we can't decode them
41
+ preds = np.where(preds != -100, preds, tokenizer.pad_token_id)
42
+ labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
43
+
44
+ # Decode predictions and labels
45
  decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
 
 
 
 
46
  decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
47
+
48
+ # Compute ROUGE metrics
49
+ result = rouge_metric.compute(predictions=decoded_preds, references=decoded_labels)
50
  result = {k: round(v * 100, 4) for k, v in result.items()}
51
+
52
+ # Calculate accuracy
53
+ accuracy = accuracy_score(decoded_labels, decoded_preds)
54
+ result["accuracy"] = round(accuracy * 100, 4)
55
+
56
+ # Calculate average generation length
57
  prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds]
58
  result["gen_len"] = np.mean(prediction_lens)
59
+
 
60
  return result
61
 
62
  login(api_key.strip())
 
231
  trainer = Trainer(
232
  model=model,
233
  args=training_args,
234
+ train_dataset=train_set['train'],
235
+ eval_dataset=train_set['validation'],
236
  compute_metrics=compute_metrics,
237
  #data_collator=data_collator,
238
  processing_class=tokenizer,