-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_lib.py
128 lines (103 loc) · 3.85 KB
/
plot_lib.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
from matplotlib import pyplot as plt
import numpy as np
from IPython.display import HTML, display
def set_default(figsize=(10, 10), dpi=100):
plt.style.use(['dark_background', 'bmh'])
plt.rc('axes', facecolor='k')
plt.rc('figure', facecolor='k')
plt.rc('figure', figsize=figsize, dpi=dpi)
def plot_data(X, y, d=0, auto=False, zoom=1):
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, cmap=plt.cm.Spectral)
plt.axis('square')
plt.axis(np.array((-1.1, 1.1, -1.1, 1.1)) * zoom)
if auto is True: plt.axis('equal')
plt.axis('off')
_m, _c = 0, '.15'
plt.axvline(0, ymin=_m, color=_c, lw=1, zorder=0)
plt.axhline(0, xmin=_m, color=_c, lw=1, zorder=0)
def plot_model(X, y, model):
mesh = np.arange(-1.1, 1.1, 0.01)
xx, yy = np.meshgrid(mesh, mesh)
data = np.vstack((xx.reshape(-1), yy.reshape(-1))).T
Z = model(data)
Z = np.argmax(Z, axis=1).reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.3)
plot_data(X, y)
def show_scatterplot(X, colors, title=''):
# colors = colors.cpu().numpy()
# X = X.cpu().numpy()
plt.figure()
plt.axis('equal')
plt.scatter(X[:, 0], X[:, 1], c=colors, s=30)
plt.grid(True)
plt.title(title)
plt.axis('off')
def plot_bases(bases, width=0.04):
# bases = bases.cpu()
bases[2:] -= bases[:2]
plt.arrow(*bases[0], *bases[2], width=width, color=(1,0,0), zorder=10, alpha=1., length_includes_head=True)
plt.arrow(*bases[1], *bases[3], width=width, color=(0,1,0), zorder=10, alpha=1., length_includes_head=True)
def show_mat(mat, vect, prod, threshold=-1):
# Subplot grid definition
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex=False, sharey=True,
gridspec_kw={'width_ratios':[5,1,1]})
# Plot matrices
cax1 = ax1.matshow(mat.numpy(), clim=(-1, 1))
ax2.matshow(vect.numpy(), clim=(-1, 1))
cax3 = ax3.matshow(prod.numpy(), clim=(threshold, 1))
# Set titles
ax1.set_title(f'A: {mat.size(0)} \u00D7 {mat.size(1)}')
ax2.set_title(f'a^(i): {vect.numel()}')
ax3.set_title(f'p: {prod.numel()}')
# Remove xticks for vectors
ax2.set_xticks(tuple())
ax3.set_xticks(tuple())
# Plot colourbars
fig.colorbar(cax1, ax=ax2)
fig.colorbar(cax3, ax=ax3)
# Fix y-axis limits
ax1.set_ylim(bottom=max(len(prod), len(vect)) - 0.5)
colors = dict(
aqua='#8dd3c7',
yellow='#ffffb3',
lavender='#bebada',
red='#fb8072',
blue='#80b1d3',
orange='#fdb462',
green='#b3de69',
pink='#fccde5',
grey='#d9d9d9',
violet='#bc80bd',
unk1='#ccebc5',
unk2='#ffed6f',
)
def _cstr(s, color='black'):
if s == ' ':
return f'<text style=color:#000;padding-left:10px;background-color:{color}> </text>'
else:
return f'<text style=color:#000;background-color:{color}>{s} </text>'
# print html
def _print_color(t):
display(HTML(''.join([_cstr(ti, color=ci) for ti, ci in t])))
# get appropriate color for value
def _get_clr(value):
colors = ('#85c2e1', '#89c4e2', '#95cae5', '#99cce6', '#a1d0e8',
'#b2d9ec', '#baddee', '#c2e1f0', '#eff7fb', '#f9e8e8',
'#f9e8e8', '#f9d4d4', '#f9bdbd', '#f8a8a8', '#f68f8f',
'#f47676', '#f45f5f', '#f34343', '#f33b3b', '#f42e2e')
value = int((value * 100) / 5)
if value == len(colors): value -= 1 # fixing bugs...
return colors[value]
def _visualise_values(output_values, result_list):
text_colours = []
for i in range(len(output_values)):
text = (result_list[i], _get_clr(output_values[i]))
text_colours.append(text)
_print_color(text_colours)
def plot_results(train_res, test_res, xlabel='Epoch',legend=['train','test'], title='Accuracy'):
xx = range(len(train_res))
plt.plot(xx, train_res)
plt.plot(xx, test_res)
plt.xlabel(xlabel)
plt.legend(legend)
plt.title(title)