-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
72 lines (53 loc) · 1.46 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import sys
def hot_encoding(data, num_label):
return np.eye(num_label)[data]
def classification_rate(Y, P):
n_correct = 0
n_total = 0
misclassified = {
'0': 0,
'1': 0,
'2': 0,
'3': 0,
'4': 0,
'5': 0,
'6': 0,
'7': 0,
'8': 0,
'8': 0,
'9': 0
}
for i in range(len(Y)):
n_total += 1
if Y[i] == P[i]:
n_correct += 1
else:
misclassified[str(Y[i])] += 1
return misclassified, float(n_correct) / n_total
def prepare_X(data):
# X = np.transpose(data)
X = data / 255.
return X
def prepare_Y(data, hot_encoding_labels):
# Y = np.transpose(data)
Y_E = hot_encoding(data, hot_encoding_labels)
return data, Y_E
def unison_shuffled_copies(a, b):
assert len(a) == len(b)
p = np.random.permutation(len(a))
return a[p], b[p]
def ValueInvert(array):
# Flatten the array for looping
flatarray = array.flatten()
# Apply transformation to flattened array
for i in range(flatarray.size):
flatarray[i] = 255 - flatarray[i]
# Return the transformed array, with the original shape
return flatarray.reshape(array.shape)
def read_variable_from_console():
# test_name, num_hidden_layer, learning rate
return str(sys.argv[1]), int(sys.argv[2]), float(sys.argv[3])
def softmax(X):
expA = np.exp(X)
return expA / expA.sum(axis=1, keepdims=True)