forked from aamini/introtodeeplearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
107 lines (83 loc) · 2.57 KB
/
__init__.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
from lab1 import *
from lab2 import *
# from lab3 import *
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import time
from IPython import display as ipythondisplay
#####################################
def custom_progress_text(message):
import progressbar
from string import Formatter
message_ = message.replace('(', '{')
message_ = message_.replace(')', '}')
keys = [key[1] for key in Formatter().parse(message_)]
ids = {}
for key in keys:
if key is not None:
ids[key] = float('nan')
msg = progressbar.FormatCustomText(message, ids)
return msg
def create_progress_bar(text=None):
import progressbar
if text is None:
text = progressbar.FormatCustomText('')
bar = progressbar.ProgressBar(widgets=[
progressbar.Percentage(),
progressbar.Bar(),
progressbar.AdaptiveETA(), ' ',
text,
])
return bar
def display_model(model):
tf.keras.utils.plot_model(model,
to_file='tmp.png',
show_shapes=True)
from IPython.display import Image
return Image('tmp.png')
def plot_sample(x,y,vae):
plt.figure(figsize=(2,1))
plt.subplot(1, 2, 1)
idx = np.where(y.numpy()==1)[0][0]
plt.imshow(x[idx])
plt.grid(False)
plt.subplot(1, 2, 2)
plt.imshow(vae(x)[idx])
plt.grid(False)
plt.show()
class LossHistory:
def __init__(self, smoothing_factor=0.0):
self.alpha = smoothing_factor
self.loss = []
def append(self, value):
self.loss.append( self.alpha*self.loss[-1] + (1-self.alpha)*value if len(self.loss)>0 else value )
def get(self):
return self.loss
class PeriodicPlotter:
def __init__(self, sec, xlabel='', ylabel='', scale=None):
from IPython import display as ipythondisplay
import matplotlib.pyplot as plt
import time
self.xlabel = xlabel
self.ylabel = ylabel
self.sec = sec
self.scale = scale
self.tic = time.time()
def plot(self, data):
if time.time() - self.tic > self.sec:
plt.cla()
if self.scale is None:
plt.plot(data)
elif self.scale == 'semilogx':
plt.semilogx(data)
elif self.scale == 'semilogy':
plt.semilogy(data)
elif self.scale == 'loglog':
plt.loglog(data)
else:
raise ValueError("unrecognized parameter scale {}".format(self.scale))
plt.xlabel(self.xlabel); plt.ylabel(self.ylabel)
ipythondisplay.clear_output(wait=True)
ipythondisplay.display(plt.gcf())
self.tic = time.time()