unlearning CR
This commit is contained in:
34
Util.py
Normal file
34
Util.py
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
from pathlib import Path
|
||||
from architectures.Model import Model
|
||||
|
||||
def _log_to_csv(model:Model, mode, accuracy, report_dict, strategy):
|
||||
"""Handles directory structures, file setups, and distinct CSV column formatting."""
|
||||
arch_name = model.__class__.__name__.lower()
|
||||
save_dir = Path(f"reports/{strategy}")
|
||||
save_dir.mkdir(parents=True, exist_ok=True)
|
||||
csv_path = save_dir / f"{arch_name}-{mode}.csv"
|
||||
|
||||
file_exists = csv_path.exists()
|
||||
|
||||
headers = [
|
||||
"accuracy",
|
||||
"macro_precision", "macro_recall", "macro_f1",
|
||||
"weighted_precision", "weighted_recall", "weighted_f1"
|
||||
]
|
||||
row = [
|
||||
f"{accuracy / 100.0:.4f}",
|
||||
f"{report_dict['macro avg']['precision']:.4f}",
|
||||
f"{report_dict['macro avg']['recall']:.4f}",
|
||||
f"{report_dict['macro avg']['f1-score']:.4f}",
|
||||
f"{report_dict['weighted avg']['precision']:.4f}",
|
||||
f"{report_dict['weighted avg']['recall']:.4f}",
|
||||
f"{report_dict['weighted avg']['f1-score']:.4f}"
|
||||
]
|
||||
|
||||
with open(csv_path, "a") as f:
|
||||
if not file_exists:
|
||||
f.write(",".join(headers) + "\n")
|
||||
f.write(",".join(row) + "\n")
|
||||
|
||||
print(f">> Direct CSV metrics appended to {csv_path}")
|
||||
Reference in New Issue
Block a user