Spaces:
Runtime error
Runtime error
File size: 7,374 Bytes
ec8f374 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
"""
Metrics Module
Provides various evaluation metrics for model performance assessment.
"""
import numpy as np
from typing import List, Dict, Optional, Union
import math
class Metrics:
"""
Comprehensive metrics calculator for model evaluation.
Supports:
- BLEU score
- ROUGE-L
- Perplexity
- Custom financial domain metrics
"""
def __init__(self):
"""Initialize metrics calculator."""
self.results = {}
def calculate_bleu(
self,
references: List[str],
hypotheses: List[str],
max_n: int = 4
) -> float:
"""
Calculate BLEU score.
Args:
references: Reference texts
hypotheses: Generated texts
max_n: Maximum n-gram size
Returns:
BLEU score (0-100)
"""
try:
from nltk.translate.bleu_score import corpus_bleu, SmoothingFunction
# Tokenize
ref_tokens = [[ref.split()] for ref in references]
hyp_tokens = [hyp.split() for hyp in hypotheses]
# Calculate with smoothing
smoothing = SmoothingFunction()
score = corpus_bleu(
ref_tokens,
hyp_tokens,
smoothing_function=smoothing.method1
)
return score * 100.0
except ImportError:
# Fallback: simple word overlap
return self._simple_bleu(references, hypotheses)
def _simple_bleu(self, references: List[str], hypotheses: List[str]) -> float:
"""Simple BLEU approximation without NLTK."""
total_overlap = 0
total_length = 0
for ref, hyp in zip(references, hypotheses):
ref_words = set(ref.lower().split())
hyp_words = set(hyp.lower().split())
overlap = len(ref_words & hyp_words)
total_overlap += overlap
total_length += max(len(ref_words), len(hyp_words))
if total_length == 0:
return 0.0
return (total_overlap / total_length) * 100.0
def calculate_rouge_l(
self,
references: List[str],
hypotheses: List[str]
) -> Dict[str, float]:
"""
Calculate ROUGE-L score.
Args:
references: Reference texts
hypotheses: Generated texts
Returns:
Dict with precision, recall, f1
"""
total_precision = 0
total_recall = 0
total_f1 = 0
for ref, hyp in zip(references, hypotheses):
ref_words = ref.split()
hyp_words = hyp.split()
# Find longest common subsequence
lcs_length = self._lcs_length(ref_words, hyp_words)
# Calculate metrics
precision = lcs_length / len(hyp_words) if len(hyp_words) > 0 else 0
recall = lcs_length / len(ref_words) if len(ref_words) > 0 else 0
f1 = (2 * precision * recall / (precision + recall)) if (precision + recall) > 0 else 0
total_precision += precision
total_recall += recall
total_f1 += f1
n = len(references)
return {
'precision': (total_precision / n) * 100.0 if n > 0 else 0.0,
'recall': (total_recall / n) * 100.0 if n > 0 else 0.0,
'f1': (total_f1 / n) * 100.0 if n > 0 else 0.0
}
def _lcs_length(self, seq1: List[str], seq2: List[str]) -> int:
"""Calculate longest common subsequence length."""
m, n = len(seq1), len(seq2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if seq1[i-1] == seq2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[m][n]
def calculate_perplexity(
self,
log_probs: List[float]
) -> float:
"""
Calculate perplexity from log probabilities.
Args:
log_probs: List of log probabilities
Returns:
Perplexity score
"""
if not log_probs:
return float('inf')
avg_log_prob = sum(log_probs) / len(log_probs)
perplexity = math.exp(-avg_log_prob)
return perplexity
def calculate_accuracy(
self,
predictions: List[str],
references: List[str]
) -> float:
"""
Calculate exact match accuracy.
Args:
predictions: Predicted answers
references: Reference answers
Returns:
Accuracy percentage
"""
if not predictions or not references:
return 0.0
matches = sum(
pred.strip().lower() == ref.strip().lower()
for pred, ref in zip(predictions, references)
)
return (matches / len(predictions)) * 100.0
def calculate_all_metrics(
self,
predictions: List[str],
references: List[str],
log_probs: Optional[List[float]] = None
) -> Dict[str, float]:
"""
Calculate all available metrics.
Args:
predictions: Model predictions
references: Reference answers
log_probs: Optional log probabilities for perplexity
Returns:
Dict of all metrics
"""
metrics = {}
# BLEU
try:
metrics['bleu'] = self.calculate_bleu(references, predictions)
except Exception as e:
print(f"BLEU calculation error: {e}")
metrics['bleu'] = 0.0
# ROUGE-L
try:
rouge = self.calculate_rouge_l(references, predictions)
metrics['rouge_l_precision'] = rouge['precision']
metrics['rouge_l_recall'] = rouge['recall']
metrics['rouge_l_f1'] = rouge['f1']
except Exception as e:
print(f"ROUGE calculation error: {e}")
metrics['rouge_l_f1'] = 0.0
# Accuracy
try:
metrics['accuracy'] = self.calculate_accuracy(predictions, references)
except Exception as e:
print(f"Accuracy calculation error: {e}")
metrics['accuracy'] = 0.0
# Perplexity
if log_probs:
try:
metrics['perplexity'] = self.calculate_perplexity(log_probs)
except Exception as e:
print(f"Perplexity calculation error: {e}")
metrics['perplexity'] = float('inf')
# Average response length
metrics['avg_response_length'] = sum(len(p.split()) for p in predictions) / len(predictions)
return metrics
def calculate_perplexity(log_probs: List[float]) -> float:
"""
Standalone function to calculate perplexity.
Args:
log_probs: List of log probabilities
Returns:
Perplexity score
"""
metrics = Metrics()
return metrics.calculate_perplexity(log_probs)
def calculate_bleu(references: List[str], hypotheses: List[str]) -> float:
"""
Standalone function to calculate BLEU score.
Args:
references: Reference texts
hypotheses: Generated texts
Returns:
BLEU score (0-100)
"""
metrics = Metrics()
return metrics.calculate_bleu(references, hypotheses)
|