epoch + 1, lr, train_res['loss'], train_res['accuracy'], test_res['loss'], test_res['accuracy'], time_ep, train_res['nll'], train_res['kl'], test_res['accuracy'], test_res['loss'] ] table = tabulate.tabulate([values], columns, tablefmt='simple', floatfmt='8.4f') if epoch % 40 == 0: table = table.split('\n') table = '\n'.join([table[1]] + table) else: table = table.split('\n')[2] print(table) print('Final test res:', test_res) test_preds, test_targets = utils.predictions(loaders['test'], model) pth = os.path.join(args.dir, args.fname + '.pt') print('Save to:', pth) torch.save(model.state_dict(), pth) print('Global time: ', time.time() - global_time) model.eval() print('\nEnsemble results:') for ns in [1, 10, 100]: acc, llh, log_prob, targets, preds = utils.ens_pred( model, loaders['test'], ns) print('ns %s: acc %.4f, llh %.4f' % (ns, acc, llh)) print('\n\n', args.dataset, args.model, args.beta, acc, llh, '\n')
for epoch in range(start_epoch, args.epochs): time_ep = time.time() lr = schedule(epoch) utils.adjust_learning_rate(optimizer, lr) model.train() train_res = utils.train_epoch(loaders['train'], model, criterion, optimizer) test_res = {'loss': None, 'accuracy': None} valid_res = {'loss': None, 'accuracy': None} tta_res = {'loss': None, 'accuracy': None} if (epoch + 1) % args.eval_freq == 0: with torch.no_grad(): model.eval() # Testing log_prob, targets = utils.predictions(loaders['test'], model) metrics_ts = metrics_kfold(log_prob, targets, n_splits=2, n_runs=5, temp_scale=True) test_res['loss'] = -metrics_ts['ll'] test_res['accuracy'] = metrics_ts['acc'] # Validation if args.valid_size > 0: log_prob, targets = utils.predictions(loaders['valid'], model) metrics_ts = metrics_kfold(log_prob, targets, n_splits=2, n_runs=5, temp_scale=True) valid_res['loss'] = -metrics_ts['ll'] valid_res['accuracy'] = metrics_ts['acc'] # Test-time augmentation log_probs = [] for i in range(args.num_tta): log_prob, targets = utils.predictions(loaders['tta'], model) log_probs.append(log_prob) log_prob = logsumexp(np.dstack(log_probs), axis=2) - np.log(args.num_tta)