MCPcopy Create free account
hub / github.com/AtlasAnalyticsLab/AdaFisher / run_epochs

Method run_epochs

Image_Classification/src/train.py:338–390  ·  view source on GitHub ↗
(self, trial: int, epochs: List[int])

Source from the content-addressed store, hash-verified

336 return s
337
338 def run_epochs(self, trial: int, epochs: List[int]) -> None:
339 for epoch in epochs:
340 start_time = time.time()
341 train_loss, train_acc1, train_acc5 = self.epoch_iteration(epoch)
342 mem_used = self.track_gpu_memory_usage() if MEM_TRACKING else None
343 test_loss, test_acc1, test_acc5 = self.validate()
344 end_time = time.time()
345 tot_time_epoch = end_time - start_time
346 results = {"train_loss": train_loss, "train_acc1": train_acc1, "train_acc5": train_acc5,
347 "test_loss": test_loss, "test_acc1": test_acc1, "test_acc5": test_acc5, "time": tot_time_epoch,
348 "mem_used": mem_used}
349 self.on_time_results(results, epoch)
350 if isinstance(self.scheduler, StepLR) or isinstance(self.scheduler, CosineAnnealingLR):
351 self.scheduler.step()
352 total_time = time.time()
353 scheduler_string = f" w/ {self.config['scheduler']}" if \
354 self.scheduler is not None else ''
355 print(
356 f"{self.config['optimizer']}{scheduler_string} " +
357 f"on {self.config['dataset']}: " +
358 f"T {trial + 1}/{self.config['n_trials']} | " +
359 f"E {epoch + 1}/{epochs[-1] + 1} Ended | " +
360 "E Time: {:.3f}s | ".format(end_time - start_time) +
361 "~Time Left: {:.3f}s | ".format(
362 (total_time - start_time) * (epochs[-1] - epoch)),
363 "Train Loss: {:.4f}% | Train Acc. {:.4f}% | ".format(
364 train_loss,
365 train_acc1) +
366 "Test Loss: {:.4f}% | Test Acc. {:.4f}%".format(
367 test_loss,
368 test_acc1))
369
370 if self.early_stop(train_loss):
371 print("Info: Early stop activated.")
372 break
373 if not self.dist:
374 data = {'epoch': epoch + 1,
375 'trial': trial,
376 'config': self.config,
377 'state_dict_network': self.network.state_dict(),
378 'state_dict_optimizer': self.optimizer.state_dict(),
379 'state_dict_scheduler': self.scheduler.state_dict()
380 if self.scheduler is not None else None,
381 'best_acc1': self.best_acc1,
382 'output_filename': Path(self.output_filename).name}
383 if epoch % self.save_freq == 0:
384 filename = f'trial_{trial}_epoch_{epoch}.pth.tar'
385 torch.save(data, str(self.checkpoint_path / filename))
386 if torch.greater(test_acc1, self.best_acc1):
387 self.best_acc1 = test_acc1
388 torch.save(
389 data, str(self.checkpoint_path / 'best.pth.tar'))
390 torch.save(data, str(self.checkpoint_path / 'last.pth.tar'))
391
392 def epoch_iteration(self, epoch: int):
393 losses = AverageMeter()

Callers 1

trainMethod · 0.95

Calls 6

epoch_iterationMethod · 0.95
validateMethod · 0.95
on_time_resultsMethod · 0.95
stepMethod · 0.45
state_dictMethod · 0.45

Tested by

no test coverage detected