(
self,
target_dir: str,
epoch: int,
optimizer: AdamW,
best_score: float,
training_stats: Dict[str, List]
)
| 263 | ) |
| 264 | |
| 265 | def _save_checkpoint( |
| 266 | self, |
| 267 | target_dir: str, |
| 268 | epoch: int, |
| 269 | optimizer: AdamW, |
| 270 | best_score: float, |
| 271 | training_stats: Dict[str, List] |
| 272 | ) -> None: |
| 273 | checkpoint = { |
| 274 | "epoch": epoch, |
| 275 | "model": self.model.state_dict(), |
| 276 | "optimizer": optimizer.state_dict(), |
| 277 | "best_score": best_score, |
| 278 | **training_stats |
| 279 | } |
| 280 | torch.save( |
| 281 | checkpoint, |
| 282 | os.path.join(target_dir, "best.pth.tar") |
| 283 | ) |
| 284 | logger.info("Model saved successfully") |
| 285 | |
| 286 | # 记录最佳模型信息到TensorBoard |
| 287 | self.writer.add_text('Best Model', f'Epoch: {epoch}, Accuracy: {best_score:.4f}', epoch) |
| 288 | |
| 289 | def _load_checkpoint( |
| 290 | self, |
no test coverage detected