-
Notifications
You must be signed in to change notification settings - Fork 150
/
util.py
134 lines (100 loc) · 3.21 KB
/
util.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import pandas as pd
from sklearn.utils import shuffle
def init_weight_and_bias(M1, M2):
W = np.random.randn(M1, M2) / np.sqrt(M1)
b = np.zeros(M2)
return W.astype(np.float32), b.astype(np.float32)
def init_filter(shape, poolsz):
w = np.random.randn(*shape) * np.sqrt(2) / np.sqrt(np.prod(shape[1:]) + shape[0]*np.prod(shape[2:] / np.prod(poolsz)))
return w.astype(np.float32)
def relu(x):
return x * (x > 0)
def sigmoid(A):
return 1 / (1 + np.exp(-A))
def softmax(A):
expA = np.exp(A)
return expA / expA.sum(axis=1, keepdims=True)
def sigmoid_cost(T, Y):
return -(T*np.log(Y) + (1-T)*np.log(1-Y)).sum()
def cost(T, Y):
return -(T*np.log(Y)).sum()
def cost2(T, Y):
# same as cost(), just uses the targets to index Y
# instead of multiplying by a large indicator matrix with mostly 0s
N = len(T)
return -np.log(Y[np.arange(N), T]).mean()
def error_rate(targets, predictions):
return np.mean(targets != predictions)
def y2indicator(y):
N = len(y)
K = len(set(y))
ind = np.zeros((N, K))
for i in range(N):
ind[i, y[i]] = 1
return ind
def getData(balance_ones=True, Ntest=1000):
# images are 48x48 = 2304 size vectors
Y = []
X = []
first = True
for line in open('fer2013.csv'):
if first:
first = False
else:
row = line.split(',')
Y.append(int(row[0]))
X.append([int(p) for p in row[1].split()])
X, Y = np.array(X) / 255.0, np.array(Y)
# shuffle and split
X, Y = shuffle(X, Y)
Xtrain, Ytrain = X[:-Ntest], Y[:-Ntest]
Xvalid, Yvalid = X[-Ntest:], Y[-Ntest:]
if balance_ones:
# balance the 1 class
X0, Y0 = Xtrain[Ytrain!=1, :], Ytrain[Ytrain!=1]
X1 = Xtrain[Ytrain==1, :]
X1 = np.repeat(X1, 9, axis=0)
Xtrain = np.vstack([X0, X1])
Ytrain = np.concatenate((Y0, [1]*len(X1)))
return Xtrain, Ytrain, Xvalid, Yvalid
def getImageData():
Xtrain, Ytrain, Xvalid, Yvalid = getData()
N, D = Xtrain.shape
d = int(np.sqrt(D))
Xtrain = Xtrain.reshape(-1, 1, d, d)
Xvalid = Xvalid.reshape(-1, 1, d, d)
return Xtrain, Ytrain, Xvalid, Yvalid
def getBinaryData():
Y = []
X = []
first = True
for line in open('fer2013.csv'):
if first:
first = False
else:
row = line.split(',')
y = int(row[0])
if y == 0 or y == 1:
Y.append(y)
X.append([int(p) for p in row[1].split()])
return np.array(X) / 255.0, np.array(Y)
def crossValidation(model, X, Y, K=5):
# split data into K parts
X, Y = shuffle(X, Y)
sz = len(Y) // K
errors = []
for k in range(K):
xtr = np.concatenate([ X[:k*sz, :], X[(k*sz + sz):, :] ])
ytr = np.concatenate([ Y[:k*sz], Y[(k*sz + sz):] ])
xte = X[k*sz:(k*sz + sz), :]
yte = Y[k*sz:(k*sz + sz)]
model.fit(xtr, ytr)
err = model.score(xte, yte)
errors.append(err)
print("errors:", errors)
return np.mean(errors)