def run(self, *args, **kwargs):
    kwargs['need_attn'] = False
    if self.opts.warm_up:
        self.warm_up(args=args, kwargs=kwargs)
    if (self.resume is not None):
        for epoch in range(self.start_epoch):
            self.lr_scheduler.step(epoch)
    eval_stats_dict = dict()
    for epoch in range(self.start_epoch, self.opts.epochs):
        epoch_lr = self.lr_scheduler.step(epoch)
        self.optimizer = update_optimizer(optimizer=self.optimizer, lr_value=epoch_lr)
        (train_acc, train_loss) = self.training(epoch=epoch, lr=epoch_lr, args=args, kwargs=kwargs)
        (val_acc, val_loss) = self.validation(epoch=epoch, lr=epoch_lr, args=args, kwargs=kwargs)
        eval_stats_dict[epoch] = val_acc
        gc.collect()
        is_best = (val_acc >= self.best_acc)
        self.best_acc = max(val_acc, self.best_acc)
        model_state = (self.mi_model.module.state_dict() if isinstance(self.mi_model, torch.nn.DataParallel) else self.mi_model.state_dict())
        optimizer_state = self.optimizer.state_dict()
        save_checkpoint(epoch=epoch, model_state=model_state, optimizer_state=optimizer_state, best_perf=self.best_acc, save_dir=self.opts.savedir, is_best=is_best, keep_best_k_models=self.opts.keep_best_k_models)
        self.logger.add_scalar('LR', round(epoch_lr, 6), epoch)
        self.logger.add_scalar('TrainingLoss', train_loss, epoch)
        self.logger.add_scalar('TrainingAcc', train_acc, epoch)
        self.logger.add_scalar('ValidationLoss', val_loss, epoch)
        self.logger.add_scalar('ValidationAcc', val_acc, epoch)
    eval_stats_dict_sort = {k: v for (k, v) in sorted(eval_stats_dict.items(), key=(lambda item: item[1]), reverse=True)}
    eval_stats_fname = '{}/val_stats_bag_{}_word_{}_{}_{}'.format(self.opts.savedir, self.opts.bag_size, self.opts.word_size, self.opts.attn_fn, self.opts.attn_type)
    writer = DictWriter(file_name=eval_stats_fname, format='json')
    if (not os.path.isfile(eval_stats_fname)):
        writer.write(data_dict=eval_stats_dict_sort)
    else:
        with open(eval_stats_fname, 'r') as json_file:
            eval_stats_dict_old = json.load(json_file)
        eval_stats_dict_old.update(eval_stats_dict_sort)
        eval_stats_dict_updated = {k: v for (k, v) in sorted(eval_stats_dict_old.items(), key=(lambda item: item[1]), reverse=True)}
        writer.write(data_dict=eval_stats_dict_updated)
    self.logger.close()
def run(self, *args, **kwargs):
    kwargs['need_attn'] = False
    if self.opts.warm_up:
        self.warm_up(args=args, kwargs=kwargs)
    if (self.resume is not None):
        for epoch in range(self.start_epoch):
            self.lr_scheduler.step(epoch)
    eval_stats_dict = dict()
    for epoch in range(self.start_epoch, self.opts.epochs):
        epoch_lr = self.lr_scheduler.step(epoch)
        self.optimizer = update_optimizer(optimizer=self.optimizer, lr_value=epoch_lr)
        (train_acc, train_loss) = self.training(epoch=epoch, lr=epoch_lr, args=args, kwargs=kwargs)
        (val_acc, val_loss) = self.validation(epoch=epoch, lr=epoch_lr, args=args, kwargs=kwargs)
        eval_stats_dict[epoch] = val_acc
        gc.collect()
        is_best = (val_acc >= self.best_acc)
        self.best_acc = max(val_acc, self.best_acc)
        model_state = (self.mi_model.module.state_dict() if isinstance(self.mi_model, torch.nn.DataParallel) else self.mi_model.state_dict())
        optimizer_state = self.optimizer.state_dict()
        save_checkpoint(epoch=epoch, model_state=model_state, optimizer_state=optimizer_state, best_perf=self.best_acc, save_dir=self.opts.savedir, is_best=is_best, keep_best_k_models=self.opts.keep_best_k_models)
        self.logger.add_scalar('LR', round(epoch_lr, 6), epoch)
        self.logger.add_scalar('TrainingLoss', train_loss, epoch)
        self.logger.add_scalar('TrainingAcc', train_acc, epoch)
        self.logger.add_scalar('ValidationLoss', val_loss, epoch)
        self.logger.add_scalar('ValidationAcc', val_acc, epoch)
    eval_stats_dict_sort = {k: v for (k, v) in sorted(eval_stats_dict.items(), key=(lambda item: item[1]), reverse=True)}
    eval_stats_fname = '{}/val_stats_bag_{}_word_{}_{}_{}'.format(self.opts.savedir, self.opts.bag_size, self.opts.word_size, self.opts.attn_fn, self.opts.attn_type)
    writer = DictWriter(file_name=eval_stats_fname, format='json')
    if (not os.path.isfile(eval_stats_fname)):
        writer.write(data_dict=eval_stats_dict_sort)
    else:
        with open(eval_stats_fname, 'r') as json_file:
            eval_stats_dict_old = json.load(json_file)
        eval_stats_dict_old.update(eval_stats_dict_sort)
        eval_stats_dict_updated = {k: v for (k, v) in sorted(eval_stats_dict_old.items(), key=(lambda item: item[1]), reverse=True)}
        writer.write(data_dict=eval_stats_dict_updated)
    self.logger.close()