forked from seanstappas/neural-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
52 lines (35 loc) · 1.29 KB
/
preprocessing.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
from __future__ import division
import numpy as np
def convert_mnist_images_logistic(images):
data = flatten_input_data(images)
return normalize_logistic(data)
def normalize_logistic(data):
return data / 255.0
def convert_mnist_images_train_tanh(images):
data = flatten_input_data(images)
return normalize_tanh(data)
def convert_mnist_images_test_tanh(images, mean_shift, std_scale):
data = flatten_input_data(images)
data -= mean_shift
data /= std_scale
return data
def normalize_tanh(data):
mean_shift = np.mean(data)
data -= mean_shift
std_scale = np.std(data)
data /= std_scale
return data, mean_shift, std_scale
def flatten_input_data(images):
return np.array(images.reshape((images.shape[0], 1, images.shape[1] * images.shape[2])), dtype='float32')
def convert_mnist_labels_one_hot(labels, positive, negative):
data = []
for label in labels:
label_one_hot = negative * np.ones(10)
label_one_hot[label] = positive
data.append(np.array([label_one_hot]))
return np.array(data)
def get_prediction_accuracy(nn, test_input, test_output):
prediction = nn.predict(test_input)
diff = np.argmax(prediction, 2) - np.argmax(test_output, 2)
error = np.count_nonzero(diff) / diff.size
return 1.0 - error