-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
73 lines (57 loc) · 2.19 KB
/
util.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
from __future__ import division
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
def LoadData(fname):
""" Loads data """
npzfile = np.load(fname)
inputs_train = npzfile['inputs_train'].T / 255.0
inputs_valid = npzfile['inputs_valid'].T / 255.0
inputs_test = npzfile['inputs_test'].T / 255.0
target_train = npzfile['target_train'].tolist()
target_valid = npzfile['target_valid'].tolist()
target_test = npzfile['target_test'].tolist()
num_class = max(target_train + target_valid + target_test) + 1
target_train_1hot = np.zeros([num_class, len(target_train)])
target_valid_1hot = np.zeros([num_class, len(target_valid)])
target_test_1hot = np.zeros([num_class, len(target_test)])
for ii, xx in enumerate(target_train):
target_train_1hot[xx, ii] = 1.0
for ii, xx in enumerate(target_valid):
target_valid_1hot[xx, ii] = 1.0
for ii, xx in enumerate(target_test):
target_test_1hot[xx, ii] = 1.0
inputs_train = inputs_train.T
inputs_valid = inputs_valid.T
inputs_test = inputs_test.T
target_train_1hot = target_train_1hot.T
target_valid_1hot = target_valid_1hot.T
target_test_1hot = target_test_1hot.T
return inputs_train, inputs_valid, inputs_test, target_train_1hot, target_valid_1hot, target_test_1hot
def Save(fname, data):
"""Saves the model to a numpy file."""
print('Writing to ' + fname)
np.savez_compressed(fname, **data)
def Load(fname):
"""Loads model from numpy file."""
print('Loading from ' + fname)
return dict(np.load(fname))
def DisplayPlot(train, valid, ylabel, number=0):
"""Displays training curve.
Args:
train: Training statistics.
valid: Validation statistics.
ylabel: Y-axis label of the plot.
"""
plt.figure(number)
plt.clf()
train = np.array(train)
valid = np.array(valid)
plt.plot(train[:, 0], train[:, 1], 'b', label='Train')
plt.plot(valid[:, 0], valid[:, 1], 'g', label='Validation')
plt.xlabel('Epoch')
plt.ylabel(ylabel)
plt.legend()
plt.draw()
plt.pause(0.0001)