Skip to content

Commit 158b5ac

Browse files
committed
update
1 parent 8d31f5a commit 158b5ac

File tree

6 files changed

+282
-36
lines changed

6 files changed

+282
-36
lines changed

ann_class2/tensorflow2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def main():
7474
# softmax_cross_entropy_with_logits take in the "logits"
7575
# if you wanted to know the actual output of the neural net,
7676
# you could pass "Yish" into tf.nn.softmax(logits)
77-
cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=Yish, labels=T))
77+
cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(logits=Yish, labels=T))
7878

7979
# we choose the optimizer but don't implement the algorithm ourselves
8080
# let's go with RMSprop, since we just learned about it.

cnn_class/benchmark.py

+12-19
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717
from datetime import datetime
1818

1919

20-
def y2indicator(y):
21-
N = len(y)
22-
ind = np.zeros((N, 10))
23-
for i in range(N):
24-
ind[i, y[i]] = 1
25-
return ind
26-
27-
2820
def error_rate(p, t):
2921
return np.mean(p != t)
3022

@@ -76,21 +68,19 @@ def main():
7668
# Y is a N x 1 matrix with values 1..10 (MATLAB indexes by 1)
7769
# So flatten it and make it 0..9
7870
# Also need indicator matrix for cost calculation
79-
Xtrain = flatten(train['X'].astype(np.float32) / 255)
71+
Xtrain = flatten(train['X'].astype(np.float32) / 255.)
8072
Ytrain = train['y'].flatten() - 1
8173
Xtrain, Ytrain = shuffle(Xtrain, Ytrain)
82-
Ytrain_ind = y2indicator(Ytrain)
8374

84-
Xtest = flatten(test['X'].astype(np.float32) / 255)
75+
Xtest = flatten(test['X'].astype(np.float32) / 255.)
8576
Ytest = test['y'].flatten() - 1
86-
Ytest_ind = y2indicator(Ytest)
8777

8878
# gradient descent params
8979
max_iter = 20
9080
print_period = 10
9181
N, D = Xtrain.shape
9282
batch_sz = 500
93-
n_batches = N / batch_sz
83+
n_batches = N // batch_sz
9484

9585
# initial weights
9686
M1 = 1000 # hidden layer size
@@ -105,7 +95,7 @@ def main():
10595

10696
# define variables and expressions
10797
X = tf.placeholder(tf.float32, shape=(None, D), name='X')
108-
T = tf.placeholder(tf.float32, shape=(None, K), name='T')
98+
T = tf.placeholder(tf.int32, shape=(None,), name='T')
10999
W1 = tf.Variable(W1_init.astype(np.float32))
110100
b1 = tf.Variable(b1_init.astype(np.float32))
111101
W2 = tf.Variable(W2_init.astype(np.float32))
@@ -115,16 +105,19 @@ def main():
115105

116106
Z1 = tf.nn.relu( tf.matmul(X, W1) + b1 )
117107
Z2 = tf.nn.relu( tf.matmul(Z1, W2) + b2 )
118-
Yish = tf.matmul(Z2, W3) + b3
108+
logits = tf.matmul(Z2, W3) + b3
119109

120110
cost = tf.reduce_sum(
121-
tf.nn.softmax_cross_entropy_with_logits(logits=Yish, labels=T)
111+
tf.nn.sparse_softmax_cross_entropy_with_logits(
112+
logits=logits,
113+
labels=T
114+
)
122115
)
123116

124117
train_op = tf.train.RMSPropOptimizer(0.0001, decay=0.99, momentum=0.9).minimize(cost)
125118

126119
# we'll use this to calculate the error rate
127-
predict_op = tf.argmax(Yish, 1)
120+
predict_op = tf.argmax(logits, 1)
128121

129122
t0 = datetime.now()
130123
LL = []
@@ -135,11 +128,11 @@ def main():
135128
for i in range(max_iter):
136129
for j in range(n_batches):
137130
Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
138-
Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
131+
Ybatch = Ytrain[j*batch_sz:(j*batch_sz + batch_sz),]
139132

140133
session.run(train_op, feed_dict={X: Xbatch, T: Ybatch})
141134
if j % print_period == 0:
142-
test_cost = session.run(cost, feed_dict={X: Xtest, T: Ytest_ind})
135+
test_cost = session.run(cost, feed_dict={X: Xtest, T: Ytest})
143136
prediction = session.run(predict_op, feed_dict={X: Xtest})
144137
err = error_rate(prediction, Ytest)
145138
print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, test_cost, err))

cnn_class/cnn_tf.py

+49-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from scipy.io import loadmat
2121
from sklearn.utils import shuffle
2222

23-
from benchmark import get_data, y2indicator, error_rate
23+
from benchmark import get_data, error_rate
2424

2525

2626
def convpool(X, W, b):
@@ -61,12 +61,10 @@ def main():
6161
# print len(Ytrain)
6262
del train
6363
Xtrain, Ytrain = shuffle(Xtrain, Ytrain)
64-
Ytrain_ind = y2indicator(Ytrain)
6564

6665
Xtest = rearrange(test['X'])
6766
Ytest = test['y'].flatten() - 1
6867
del test
69-
Ytest_ind = y2indicator(Ytest)
7068

7169
# gradient descent params
7270
max_iter = 6
@@ -81,7 +79,6 @@ def main():
8179
Ytrain = Ytrain[:73000]
8280
Xtest = Xtest[:26000,]
8381
Ytest = Ytest[:26000]
84-
Ytest_ind = Ytest_ind[:26000,]
8582
# print "Xtest.shape:", Xtest.shape
8683
# print "Ytest.shape:", Ytest.shape
8784

@@ -108,7 +105,7 @@ def main():
108105
# define variables and expressions
109106
# using None as the first shape element takes up too much RAM unfortunately
110107
X = tf.placeholder(tf.float32, shape=(batch_sz, 32, 32, 3), name='X')
111-
T = tf.placeholder(tf.float32, shape=(batch_sz, K), name='T')
108+
T = tf.placeholder(tf.int32, shape=(batch_sz,), name='T')
112109
W1 = tf.Variable(W1_init.astype(np.float32))
113110
b1 = tf.Variable(b1_init.astype(np.float32))
114111
W2 = tf.Variable(W2_init.astype(np.float32))
@@ -126,7 +123,7 @@ def main():
126123
Yish = tf.matmul(Z3, W4) + b4
127124

128125
cost = tf.reduce_sum(
129-
tf.nn.softmax_cross_entropy_with_logits(
126+
tf.nn.sparse_softmax_cross_entropy_with_logits(
130127
logits=Yish,
131128
labels=T
132129
)
@@ -139,14 +136,16 @@ def main():
139136

140137
t0 = datetime.now()
141138
LL = []
139+
W1_val = None
140+
W2_val = None
142141
init = tf.global_variables_initializer()
143142
with tf.Session() as session:
144143
session.run(init)
145144

146145
for i in range(max_iter):
147146
for j in range(n_batches):
148147
Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz),]
149-
Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz),]
148+
Ybatch = Ytrain[j*batch_sz:(j*batch_sz + batch_sz),]
150149

151150
if len(Xbatch) == batch_sz:
152151
session.run(train_op, feed_dict={X: Xbatch, T: Ybatch})
@@ -157,17 +156,59 @@ def main():
157156
prediction = np.zeros(len(Xtest))
158157
for k in range(len(Xtest) // batch_sz):
159158
Xtestbatch = Xtest[k*batch_sz:(k*batch_sz + batch_sz),]
160-
Ytestbatch = Ytest_ind[k*batch_sz:(k*batch_sz + batch_sz),]
159+
Ytestbatch = Ytest[k*batch_sz:(k*batch_sz + batch_sz),]
161160
test_cost += session.run(cost, feed_dict={X: Xtestbatch, T: Ytestbatch})
162161
prediction[k*batch_sz:(k*batch_sz + batch_sz)] = session.run(
163162
predict_op, feed_dict={X: Xtestbatch})
164163
err = error_rate(prediction, Ytest)
165164
print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, test_cost, err))
166165
LL.append(test_cost)
166+
167+
W1_val = W1.eval()
168+
W2_val = W2.eval()
167169
print("Elapsed time:", (datetime.now() - t0))
168170
plt.plot(LL)
169171
plt.show()
170172

171173

174+
W1_val = W1_val.transpose(3, 2, 0, 1)
175+
W2_val = W2_val.transpose(3, 2, 0, 1)
176+
177+
178+
# visualize W1 (20, 3, 5, 5)
179+
# W1_val = W1.get_value()
180+
grid = np.zeros((8*5, 8*5))
181+
m = 0
182+
n = 0
183+
for i in range(20):
184+
for j in range(3):
185+
filt = W1_val[i,j]
186+
grid[m*5:(m+1)*5,n*5:(n+1)*5] = filt
187+
m += 1
188+
if m >= 8:
189+
m = 0
190+
n += 1
191+
plt.imshow(grid, cmap='gray')
192+
plt.title("W1")
193+
plt.show()
194+
195+
# visualize W2 (50, 20, 5, 5)
196+
# W2_val = W2.get_value()
197+
grid = np.zeros((32*5, 32*5))
198+
m = 0
199+
n = 0
200+
for i in range(50):
201+
for j in range(20):
202+
filt = W2_val[i,j]
203+
grid[m*5:(m+1)*5,n*5:(n+1)*5] = filt
204+
m += 1
205+
if m >= 32:
206+
m = 0
207+
n += 1
208+
plt.imshow(grid, cmap='gray')
209+
plt.title("W2")
210+
plt.show()
211+
212+
172213
if __name__ == '__main__':
173214
main()

0 commit comments

Comments
 (0)