-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmnist_utils.py
More file actions
66 lines (58 loc) · 2.41 KB
/
mnist_utils.py
File metadata and controls
66 lines (58 loc) · 2.41 KB
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
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer, OneHotEncoder
import matplotlib.pyplot as plt
import numpy as np
def create_input_pipeline():
input_reshaper = FunctionTransformer(lambda x: x.reshape(x.shape[0], -1),
validate=False)
input_normalizer = FunctionTransformer(lambda x: x.astype(np.float32)/255.0,
validate=True)
return Pipeline([
('reshaper', input_reshaper),
('normalizer', input_normalizer),
])
def create_output_pipeline():
output_reshaper = FunctionTransformer(lambda x: x.reshape(-1, 1),
validate=False)
output_encoder = OneHotEncoder(sparse_output=True, categories='auto')
output_type_changer = FunctionTransformer(lambda x: x.astype(np.float32),
validate=False)
return Pipeline([
('reshaper', output_reshaper),
('binarizer', output_encoder),
('type_changer', output_type_changer),
])
def plot_history(network_history):
plt.figure()
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.plot(network_history.history['loss'])
plt.plot(network_history.history['val_loss'])
plt.legend(['Training', 'Validation'])
plt.figure()
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.plot(network_history.history['accuracy'])
plt.plot(network_history.history['val_accuracy'])
plt.legend(['Training', 'Validation'], loc='lower right')
def plot_confusion_matrix(cm, classes,
normalize=False,
cmap=plt.cm.Blues):
log1p_cm = np.log1p(cm)
if normalize:
cm = cm.astype(np.float32)/cm.sum(axis=1)[:, np.newaxis]
figure, axes = plt.subplots(figsize=(6, 6))
axes.imshow(log1p_cm, interpolation='nearest', cmap=cmap)
axes.set_xticks(classes)
axes.set_yticks(classes)
fmt = '{0:.4f}' if normalize else '{0:d}'
thresh = 0.5*cm.max()
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
axes.text(j, i, fmt.format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black",
fontsize=8)
figure.tight_layout()
axes.set_ylabel('True label')
axes.set_xlabel('Predicted label')