-
Notifications
You must be signed in to change notification settings - Fork 81
/
mnist_conv.py
54 lines (48 loc) · 1.46 KB
/
mnist_conv.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
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from dense import Dense
from convolutional import Convolutional
from reshape import Reshape
from activations import Sigmoid
from losses import binary_cross_entropy, binary_cross_entropy_prime
from network import train, predict
def preprocess_data(x, y, limit):
zero_index = np.where(y == 0)[0][:limit]
one_index = np.where(y == 1)[0][:limit]
all_indices = np.hstack((zero_index, one_index))
all_indices = np.random.permutation(all_indices)
x, y = x[all_indices], y[all_indices]
x = x.reshape(len(x), 1, 28, 28)
x = x.astype("float32") / 255
y = np_utils.to_categorical(y)
y = y.reshape(len(y), 2, 1)
return x, y
# load MNIST from server, limit to 100 images per class since we're not training on GPU
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, y_train = preprocess_data(x_train, y_train, 100)
x_test, y_test = preprocess_data(x_test, y_test, 100)
# neural network
network = [
Convolutional((1, 28, 28), 3, 5),
Sigmoid(),
Reshape((5, 26, 26), (5 * 26 * 26, 1)),
Dense(5 * 26 * 26, 100),
Sigmoid(),
Dense(100, 2),
Sigmoid()
]
# train
train(
network,
binary_cross_entropy,
binary_cross_entropy_prime,
x_train,
y_train,
epochs=20,
learning_rate=0.1
)
# test
for x, y in zip(x_test, y_test):
output = predict(network, x)
print(f"pred: {np.argmax(output)}, true: {np.argmax(y)}")