-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (60 loc) · 2.23 KB
/
Copy pathutils.py
File metadata and controls
71 lines (60 loc) · 2.23 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from torch import nn
import torch
class AvgMeter:
def __init__(self, name="Metric"):
self.name = name
self.reset()
def reset(self):
self.avg, self.sum, self.count = [0] * 3
def update(self, val, count=1):
self.count += count
self.sum += val * count
self.avg = self.sum / self.count
def __repr__(self):
text = f"{self.name}: {self.avg:.4f}"
return text
class EarlyStopping:
def __init__(self, patience=7, delta=0, path='checkpoint.pt', verbose=False):
"""
Args:
patience (int): How long to wait after last time validation loss improved.
delta (float): Minimum change in the monitored quantity to qualify as an improvement.
path (str): Path for the checkpoint to be saved.
verbose (bool): If True, prints a message for each validation loss improvement.
"""
self.patience = patience
self.delta = delta
self.path = path
self.verbose = verbose
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, val_loss, model):
if self.best_loss is None:
self.best_loss = val_loss
self.save_checkpoint(model)
elif val_loss > self.best_loss - self.delta:
self.counter += 1
if self.verbose:
print(f"EarlyStopping counter: {self.counter} out of {self.patience}")
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_loss = val_loss
self.save_checkpoint(model)
self.counter = 0
def save_checkpoint(self, model):
"""Saves model when validation loss decreases."""
if self.verbose:
print(f"Validation loss decreased. Saving model to {self.path}" )
torch.save(model.state_dict(), self.path)
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group["lr"]
def cross_entropy(preds, targets, reduction='none'):
log_softmax = nn.LogSoftmax(dim=-1)
loss = (-targets * log_softmax(preds)).sum(1)
if reduction == "none":
return loss
elif reduction == "mean":
return loss.mean()