-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensor.py
81 lines (63 loc) · 2.44 KB
/
tensor.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
import numpy as np
import tensorflow as tf
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
from util import get_normalized_data, cost, error_rate, y2indicator
def main():
X, Y = get_normalized_data()
max_iter = 30
print_period = 10
lr = 0.00003
reg = 0.01
Xtrain = X[:-1000]
Ytrain = Y[:-1000]
Xtest = X[-1000:]
Ytest = Y[-1000:]
Ytrain_ind = y2indicator(Ytrain)
Ytest_ind = y2indicator(Ytest)
N, D = Xtrain.shape
batch_sz = 512
n_batches = int(N / batch_sz)
M1 = 300
M2 = 100
K = 10
W1_init = np.random.randn(D, M1) / (2*np.sqrt(D+M1))
b1_init = np.zeros((M1))
W2_init = np.random.rand(M1,M2) / (2*np.sqrt(M1+M2))
b2_init = np.zeros((M2))
W3_init = np.random.rand(M2,K) / (2*np.sqrt(M2+K))
b3_init = np.zeros((K))
X = tf.placeholder(tf.float32, shape=(None, D), name ='X')
T = tf.placeholder(tf.float32, shape=(None, K), name = 'T')
W1 = tf.Variable(W1_init.astype(np.float32))
b1 = tf.Variable(b1_init.astype(np.float32))
W2 = tf.Variable(W2_init.astype(np.float32))
b2 = tf.Variable(b2_init.astype(np.float32))
W3 = tf.Variable(W3_init.astype(np.float32))
b3 = tf.Variable(b3_init.astype(np.float32))
Z1 = tf.nn.relu( tf.matmul(X, W1) + b1)
Z2 = tf.nn.relu( tf.matmul(Z1, W2) + b2)
Yish = tf.matmul(Z2, W3) + b3
cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=Yish, labels = T))
train_op = tf.train.RMSPropOptimizer(lr, decay = 0.99, momentum=0.9).minimize(cost)
predict_op = tf.argmax(Yish, 1)
LL = []
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for i in range(max_iter):
for j in range(n_batches):
Xbatch = Xtrain[j*batch_sz:(j*batch_sz + batch_sz)]
Ybatch = Ytrain_ind[j*batch_sz:(j*batch_sz + batch_sz)]
sess.run(train_op, feed_dict={X: Xbatch, T: Ybatch})
if j % print_period == 0:
test_cost = sess.run(cost, feed_dict={X: Xtest, T: Ytest_ind})
prediction = sess.run(predict_op, feed_dict={X: Xtest})
err = error_rate(prediction, Ytest)
print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, test_cost, err))
LL.append(test_cost)
plt.plot(LL)
plt.show
if __name__ == '__main__':
main()