-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·37 lines (29 loc) · 1.09 KB
/
Copy pathutils.py
File metadata and controls
executable file
·37 lines (29 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import torch
import logging
logger = logging.getLogger('Utils')
def l2_norm(input, axis=1):
norm = torch.norm(input, 2, axis, True)
output = torch.div(input, norm)
return output
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def log_epoch(n_epochs=None, loss=None, acc=None, epoch=None, task=None, time=None, classification=False):
acc_str = f"Task {task + 1}" if task is not None else f""
acc_str += f" Epoch [{epoch + 1}]/[{n_epochs}]" if epoch is not None else f""
acc_str += f"\t Training Loss: {loss:.4f}" if loss is not None else f""
acc_str += f"\t Training Accuracy: {acc:.2f}" if acc is not None else f""
acc_str += f"\t Time: {time:.2f}" if time is not None else f""
if classification:
acc_str = acc_str.replace("Training", "Classification")
logger.info(acc_str)