-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
133 lines (102 loc) · 2.97 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
import matplotlib.pyplot as plt
import time
import sys
import numpy as np
from collections import namedtuple
from datetime import datetime
_format = '%Y-%m-%d-%H-%M-%S-%f'
def time_string():
return datetime.now().strftime(_format)
class ValueWindow(object):
def __init__(self, window_size=100):
self._window_size = window_size
self._values = []
self.val = None
def append(self, x):
self._values = self._values[-(self._window_size - 1):] + [x]\
@property
def sum(self):
return sum(self._values)\
@property
def count(self):
return len(self._values)\
@property
def average(self):
return self.sum / max(1, self.count)
@property
def get_dinwow_size(self):
return self._window_size
@property
def avg(self):
return self.average
def reset(self):
self._values = []
def update(self, val):
self.append(val)
self.val = val
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
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 _json_object_hook(d):
return namedtuple('X', d.keys())(*d.values())
def display(string, variables):
sys.stdout.write(f'\r{string}' % variables)
def num_params(model):
parameters = filter(lambda p: p.requires_grad, model.parameters())
parameters = sum([np.prod(p.size()) for p in parameters]) / 1_000_000
print('Trainable Parameters: %.3f million' % parameters)
def time_since(started):
elapsed = time.time() - started
m = int(elapsed // 60)
s = int(elapsed % 60)
if m >= 60:
h = int(m // 60)
m = m % 60
return f'{h}h {m}m {s}s'
else:
return f'{m}m {s}s'
def plot(array):
fig = plt.figure(figsize=(30, 5))
ax = fig.add_subplot(111)
ax.xaxis.label.set_color('grey')
ax.yaxis.label.set_color('grey')
ax.xaxis.label.set_fontsize(23)
ax.yaxis.label.set_fontsize(23)
ax.tick_params(axis='x', colors='grey', labelsize=23)
ax.tick_params(axis='y', colors='grey', labelsize=23)
plt.plot(array)
def plot_spec(M):
M = np.flip(M, axis=0)
plt.figure(figsize=(18, 4))
plt.imshow(M, interpolation='nearest', aspect='auto')
plt.show()
def plot_alignment(alignment, path, info=None):
fig, ax = plt.subplots()
im = ax.imshow(
alignment,
aspect='auto',
origin='lower',
interpolation='none')
fig.colorbar(im, ax=ax)
xlabel = 'Decoder timestep'
if info is not None:
xlabel += '\n\n' + info
plt.xlabel(xlabel)
plt.ylabel('Encoder timestep')
plt.tight_layout()
plt.savefig(path, format='png')
plt.close()