-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn_utils.py
47 lines (44 loc) · 1.51 KB
/
nn_utils.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
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def load_data():
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = np.reshape(x_train, (x_train.shape[0], 784))/255.
x_test = np.reshape(x_test, (x_test.shape[0], 784))/255.
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)
return (x_train, y_train), (x_test, y_test)
def plot_random_examples(x, y, p=None):
indices = np.random.choice(range(0, x.shape[0]), 10)
y = np.argmax(y, axis=1)
if p is None:
p = y
plt.figure(figsize=(10, 5))
for i, index in enumerate(indices):
plt.subplot(2, 5, i+1)
plt.imshow(x[index].reshape((28, 28)), cmap='binary')
plt.xticks([])
plt.yticks([])
if y[index] == p[index]:
col = 'g'
else:
col = 'r'
plt.xlabel(str(p[index]), color=col)
return plt
def plot_results(history):
plt.figure(figsize=(12, 4))
epochs = len(history['val_loss'])
plt.subplot(1, 2, 1)
plt.plot(range(epochs), history['val_loss'], label='Val Loss')
plt.plot(range(epochs), history['train_loss'], label='Train Loss')
plt.xticks(list(range(epochs)))
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(range(epochs), history['val_acc'], label='Val Acc')
plt.xticks(list(range(epochs)))
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
return plt