Spaces:
Sleeping
Sleeping
Download src/utils/logger.py from quangdat05/AIChamcong: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/spaces/quangdat05/AIChamcong/resolve/main/src/utils/logger.py
- Command line
-
hf download hf://spaces/quangdat05/AIChamcong/src/utils/logger.py
-
curl -L -o logger.py https://huggingface.co/spaces/quangdat05/AIChamcong/resolve/main/src/utils/logger.py
1.32 kB
| import os | |
| import csv | |
| from typing import Dict, Any | |
| class CSVLogger: | |
| """ | |
| Ghi log quá trình huấn luyện ra file CSV. | |
| Dùng để vẽ biểu đồ Loss, Accuracy, Learning Rate trong báo cáo. | |
| """ | |
| def __init__(self, log_path: str) -> None: | |
| self.log_path = log_path | |
| os.makedirs(os.path.dirname(log_path), exist_ok=True) | |
| self.fieldnames = [ | |
| "epoch", | |
| "phase", | |
| "learning_rate", | |
| "train_loss", | |
| "train_accuracy", | |
| "val_loss", | |
| "val_accuracy", | |
| "embedding_norm", | |
| "gradient_norm", | |
| "train_batches", | |
| "val_batches", | |
| "epoch_time_sec", | |
| "device", | |
| "gpu_memory_mb", | |
| "checkpoint_saved" | |
| ] | |
| if not os.path.exists(self.log_path): | |
| with open(self.log_path, "w", newline="", encoding="utf-8") as f: | |
| writer = csv.DictWriter(f, fieldnames=self.fieldnames) | |
| writer.writeheader() | |
| def log(self, row: Dict[str, Any]) -> None: | |
| """Ghi một dòng kết quả vào file CSV.""" | |
| with open(self.log_path, "a", newline="", encoding="utf-8") as f: | |
| writer = csv.DictWriter(f, fieldnames=self.fieldnames) | |
| writer.writerow(row) |