-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_network_keras.py
49 lines (42 loc) · 1.5 KB
/
neural_network_keras.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
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
try:
del X_train, y_train
del X_test, y_test
print('Clear previously loaded data.')
except:
pass
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print('Training data shape: ', X_train.shape)
print('Training labels shape: ', y_train.shape)
print('Test data shape: ', X_test.shape)
print('Test labels shape: ', y_test.shape)
# Visualize some examples from the dataset.
# We show a few examples of training images from each class.
classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
num_classes = len(classes)
samples_per_class = 7
for y, cls in enumerate(classes):
idxs = np.flatnonzero(y_train == y)
idxs = np.random.choice(idxs, samples_per_class, replace=False)
for i, idx in enumerate(idxs):
plt_idx = i * num_classes + y + 1
plt.subplot(samples_per_class, num_classes, plt_idx)
plt.imshow(X_train[idx].astype('uint8'))
plt.axis('off')
if i == 0:
plt.title(cls)
# Subsample the data for more efficient code execution in this exercise
num_training = 41000
mask = list(range(num_training))
X_train = X_train[mask]
y_train = y_train[mask].T
num_test = 1000
mask = list(range(num_test))
X_test = X_test[mask]
y_test = y_test[mask]
# Reshape the image data into rows, transpose and normalize
X_train = np.reshape(X_train, (X_train.shape[0], -1)).T / 255.
X_test = np.reshape(X_test, (X_test.shape[0], -1)).T / 255.
print(X_train.shape, X_test.shape)