-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_evaluation.py
More file actions
151 lines (114 loc) · 5.26 KB
/
Copy pathrun_evaluation.py
File metadata and controls
151 lines (114 loc) · 5.26 KB
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
import os
import argparse
import json
from datetime import datetime
from typing import Dict, Any
from src.models import get_model, InferenceConfig
from src.datasets import get_dataset_processor, sample_dataset
from src.evaluator import SelfConsistencyEvaluator, save_results
from src.analysis import create_summary_report, plot_results
import logging
# 设置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('evaluation.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser(description="Self-Consistency Baseline Evaluation")
parser.add_argument("--models", nargs="+",
default=["ministral-7b", "gemma-2-2b", "qwen2.5-3b"],
help="Models to evaluate")
parser.add_argument("--datasets", nargs="+",
default=["mmlu-pro", "gsm8k"],
help="Datasets to evaluate on")
parser.add_argument("--num_samples", type=int, default=None,
help="Number of samples to evaluate (None for all)")
parser.add_argument("--output_dir", type=str, default="results",
help="Output directory for results")
parser.add_argument("--temperature", type=float, default=0.7,
help="Sampling temperature")
parser.add_argument("--top_p", type=float, default=0.9,
help="Top-p sampling parameter")
parser.add_argument("--num_consistency_samples", type=int, default=5,
help="Number of samples for self-consistency")
parser.add_argument("--max_new_tokens", type=int, default=512,
help="Maximum new tokens to generate")
args = parser.parse_args()
# 创建输出目录
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_dir = os.path.join(args.output_dir, f"evaluation_{timestamp}")
os.makedirs(output_dir, exist_ok=True)
# 保存配置
config = {
"models": args.models,
"datasets": args.datasets,
"num_samples": args.num_samples,
"inference_config": {
"num_samples": args.num_consistency_samples,
"temperature": args.temperature,
"top_p": args.top_p,
"max_new_tokens": args.max_new_tokens
},
"timestamp": timestamp
}
with open(os.path.join(output_dir, "config.json"), "w") as f:
json.dump(config, f, indent=2)
# 创建推理配置
inference_config = InferenceConfig(
num_samples=args.num_consistency_samples,
temperature=args.temperature,
top_p=args.top_p,
max_new_tokens=args.max_new_tokens
)
all_results = {}
# 评估每个模型在每个数据集上的表现
for model_name in args.models:
logger.info(f"Starting evaluation for model: {model_name}")
try:
# 加载模型
model = get_model(model_name)
evaluator = SelfConsistencyEvaluator(model, inference_config)
model_results = {}
for dataset_name in args.datasets:
logger.info(f"Evaluating {model_name} on {dataset_name}")
try:
# 加载数据集
processor = get_dataset_processor(dataset_name)
# 运行评估
results = evaluator.evaluate_dataset(processor, args.num_samples)
# 保存单个结果
result_file = os.path.join(output_dir, f"{model_name}_{dataset_name}_results.json")
save_results(results, result_file)
model_results[dataset_name] = results
logger.info(f"Completed {model_name} on {dataset_name}: "
f"Accuracy = {results.accuracy:.4f}, "
f"Avg Tokens = {results.avg_tokens_per_sample:.1f}")
except Exception as e:
logger.error(f"Error evaluating {model_name} on {dataset_name}: {e}")
continue
all_results[model_name] = model_results
except Exception as e:
logger.error(f"Error loading model {model_name}: {e}")
continue
# 生成汇总报告
if all_results:
logger.info("Generating summary report...")
try:
# 创建汇总报告
summary_file = os.path.join(output_dir, "summary_report.md")
create_summary_report(all_results, summary_file)
# 创建可视化图表
plot_file = os.path.join(output_dir, "results_visualization.png")
plot_results(all_results, plot_file)
logger.info(f"Evaluation completed. Results saved in {output_dir}")
except Exception as e:
logger.error(f"Error generating summary: {e}")
else:
logger.error("No successful evaluations completed")
if __name__ == "__main__":
main()