File size: 3,677 Bytes
74b7543 | 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 | import os
import json
import re
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from collections import defaultdict
def parse_metrics():
"""解析 archives 目录下的 metrics.json 文件,返回 DataFrame"""
data_list = []
for root, dirs, files in os.walk("archives"):
for file in files:
if file.endswith("_metrics.json"):
path = os.path.join(root, file)
parent_match = re.search(r'E(\d+)_R(\d+)_k(\d+)_D(\d+)_([a-z0-9_.]+)', path)
seed_match = re.search(r'_S(\d+)', path)
step_match = re.search(r'checkpoint-(\d+)', path)
if not (parent_match and seed_match and step_match):
continue
e, r, k, d, dist = parent_match.groups()
seed, step = seed_match.group(1), int(step_match.group(1))
is_uniform_test = "eval_test_uniform" in file
# 对于 powerlaw 训练,只取 uniform test 结果
# 对于 uniform 训练,取 eval_test 结果(本身就是 uniform)
if "powerlaw" in dist and not is_uniform_test:
continue
if "uniform" in dist and is_uniform_test:
continue # 避免重复
try:
with open(path, 'r') as f:
data = json.load(f)
if "exact_match" in data:
val = data["exact_match"]
data_list.append({
"E": e, "R": r, "k": k, "D": d,
"distribution": "uniform" if "uniform" in dist else "powerlaw",
"step": step,
"seed": seed,
"accuracy": val
})
except Exception as err:
print(f"Error reading {path}: {err}")
return pd.DataFrame(data_list)
def plot_all(df):
os.makedirs("plots", exist_ok=True)
sns.set_theme(style="whitegrid")
palette = {"uniform": "tab:blue", "powerlaw": "tab:orange"}
# 按配置分组
configs = df.groupby(["E", "R", "k", "D"])
for (e, r, k, d), group_df in configs:
# 限制 k=3 的 steps
if k == '3':
group_df = group_df[group_df["step"] <= 50000]
if group_df.empty:
continue
fig, ax = plt.subplots(figsize=(7, 4.5))
sns.lineplot(
ax=ax,
data=group_df,
x="step",
y="accuracy",
hue="distribution",
marker="o",
markersize=7,
palette=palette,
errorbar="sd",
linewidth=2.5
)
ax.set_title(f"Multi-hop (E{e} R{r} k{k} D{d})", fontsize=16, fontweight='bold')
ax.set_xlabel("Training Steps", fontsize=14)
ax.set_ylabel("Accuracy", fontsize=14)
ax.set_ylim(0, 1.05)
ax.tick_params(axis='both', which='major', labelsize=12)
ax.legend(title="Distribution", title_fontsize=12, fontsize=11)
plt.tight_layout()
save_path = f"plots/compact_E{e}_R{r}_k{k}_D{d}.pdf"
plt.savefig(save_path, dpi=300)
print(f"Saved plot to {save_path}")
plt.close()
if __name__ == "__main__":
df = parse_metrics()
if df.empty:
print("No metrics found.")
else:
print(f"Loaded {len(df)} data points.")
plot_all(df)
|