-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
178 lines (134 loc) · 5.19 KB
/
utils.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import csv
import torch
from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report, confusion_matrix
import shutil
import numpy as np
class AverageMeter(object):
"""Computes and stores the average and current value"""
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
class Logger(object):
def __init__(self, path, header,opening_mode='w'):
self.log_file = open(path, opening_mode)
self.logger = csv.writer(self.log_file, delimiter='\t')
if opening_mode =='w':
self.logger.writerow(header)
self.header = header
def __del(self):
self.log_file.close()
def log(self, values):
write_values = []
for col in self.header:
assert col in values
write_values.append(values[col])
self.logger.writerow(write_values)
self.log_file.flush()
class Queue:
# Constructor creates a list
def __init__(self, max_size, n_classes):
self.queue = list(np.zeros((max_size, n_classes), dtype=float).tolist())
self.max_size = max_size
self.median = None
self.ma = None
self.ewma = None
# Adding elements to queue
def enqueue(self, data):
self.queue.insert(0, data)
self.median = self._median()
self.ma = self._ma()
self.ewma = self._ewma()
return True
# Removing the last element from the queue
def dequeue(self):
if len(self.queue) > 0:
return self.queue.pop()
return ("Queue Empty!")
# Getting the size of the queue
def size(self):
return len(self.queue)
# printing the elements of the queue
def printQueue(self):
return self.queue
# Average
def _ma(self):
return np.array(self.queue[:self.max_size]).mean(axis=0)
# Median
def _median(self):
return np.median(np.array(self.queue[:self.max_size]), axis=0)
# Exponential average
def _ewma(self):
weights = np.exp(np.linspace(-1., 0., self.max_size))
weights /= weights.sum()
average = weights.reshape(1, self.max_size).dot(np.array(self.queue[:self.max_size]))
return average.reshape(average.shape[1], )
def LevenshteinDistance(a, b):
# This is a straightforward implementation of a well-known algorithm, and thus
# probably shouldn't be covered by copyright to begin with. But in case it is,
# the author (Magnus Lie Hetland) has, to the extent possible under law,
# dedicated all copyright and related and neighboring rights to this software
# to the public domain worldwide, by distributing it under the CC0 license,
# version 1.0. This software is distributed without any warranty. For more
# information, see <http://creativecommons.org/publicdomain/zero/1.0>
"Calculates the Levenshtein distance between a and b."
n, m = len(a), len(b)
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a, b = b, a
n, m = m, n
current = range(n + 1)
for i in range(1, m + 1):
previous, current = current, [i] + [0] * n
for j in range(1, n + 1):
add, delete = previous[j] + 1, current[j - 1] + 1
change = previous[j - 1]
if a[j - 1] != b[i - 1]:
change = change + 1
current[j] = min(add, delete, change)
if current[n]<0:
return 0
else:
return current[n]
def load_value_file(file_path):
with open(file_path, 'r') as input_file:
value = float(input_file.read().rstrip('\n\r'))
return value
def calculate_accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def calculate_precision(outputs, targets):
_, pred = outputs.topk(1, 1, True)
pred = pred.t()
return precision_score(targets.view(-1), pred.view(-1), average = 'macro')
def calculate_recall(outputs, targets):
_, pred = outputs.topk(1, 1, True)
pred = pred.t()
return recall_score(targets.view(-1), pred.view(-1), average = 'macro')
def save_checkpoint(state, is_best, opt):
torch.save(state, '%s/%s_checkpoint.pth' % (opt.result_path, opt.store_name))
if is_best:
shutil.copyfile('%s/%s_checkpoint.pth' % (opt.result_path, opt.store_name),'%s/%s_best.pth' % (opt.result_path, opt.store_name))
def adjust_learning_rate(optimizer, epoch, opt):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
lr_new = opt.learning_rate * (0.1 ** (sum(epoch >= np.array(opt.lr_steps))))
for param_group in optimizer.param_groups:
param_group['lr'] = lr_new
#param_group['lr'] = opt.learning_rate