-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfnn_helper.py
66 lines (56 loc) · 2.35 KB
/
fnn_helper.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
import keras
import matplotlib.pyplot as plt
from IPython.display import clear_output
class PlotLosses(keras.callbacks.Callback):
def __init__(self, plot_interval=1, evaluate_interval=10, x_val=None, y_val_categorical=None):
self.plot_interval = plot_interval
self.evaluate_interval = evaluate_interval
self.x_val = x_val
self.y_val_categorical = y_val_categorical
#self.model = model
def on_train_begin(self, logs={}):
print('Begin training')
self.i = 0
self.x = []
self.losses = []
self.val_losses = []
self.acc = []
self.val_acc = []
self.logs = []
self.weights = []
self.acc_2 = []
self.loss_2 = []
def on_epoch_end(self, epoch, logs={}):
if self.evaluate_interval is None:
self.logs.append(logs)
self.x.append(self.i)
self.losses.append(logs.get('loss'))
self.val_losses.append(logs.get('val_loss'))
self.acc.append(logs.get('acc'))
self.val_acc.append(logs.get('val_acc'))
self.weights.append(self.model.get_weights())
self.i += 1
if (epoch%self.plot_interval==0):
clear_output(wait=True)
f, (ax1, ax2) = plt.subplots(1, 2, sharex=True, figsize=(20,5))
ax1.plot(self.x, self.losses, label="loss")
ax1.plot(self.x, self.val_losses, label="val_loss")
ax1.legend()
ax2.plot(self.x, self.acc, label="acc")
ax2.plot(self.x, self.val_acc, label="val_acc")
ax2.legend()
plt.show();
#score = self.model.evaluate(x_test, y_test_categorical, verbose=0)
#print("accuracy: ", score[1])
def on_batch_end(self, batch, logs={}):
if self.evaluate_interval is not None:
if (batch%self.evaluate_interval==0):
self.i += 1
self.logs.append(logs)
self.x.append(self.i)
self.losses.append(logs.get('loss'))
self.acc.append(logs.get('acc'))
if self.x_val is not None:
score = self.model.evaluate(self.x_val, self.y_val_categorical, verbose=0)
self.val_losses.append(score[0])
self.val_acc.append(score[1])