()
| 24 | |
| 25 | # copy this first part from theano2.py |
| 26 | def main(): |
| 27 | # step 1: get the data and define all the usual variables |
| 28 | Xtrain, Xtest, Ytrain, Ytest = get_normalized_data() |
| 29 | |
| 30 | max_iter = 15 |
| 31 | print_period = 50 |
| 32 | |
| 33 | lr = 0.00004 |
| 34 | reg = 0.01 |
| 35 | |
| 36 | Ytrain_ind = y2indicator(Ytrain) |
| 37 | Ytest_ind = y2indicator(Ytest) |
| 38 | |
| 39 | N, D = Xtrain.shape |
| 40 | batch_sz = 500 |
| 41 | n_batches = N // batch_sz |
| 42 | |
| 43 | # add an extra layer just for fun |
| 44 | M1 = 300 |
| 45 | M2 = 100 |
| 46 | K = 10 |
| 47 | W1_init = np.random.randn(D, M1) / np.sqrt(D) |
| 48 | b1_init = np.zeros(M1) |
| 49 | W2_init = np.random.randn(M1, M2) / np.sqrt(M1) |
| 50 | b2_init = np.zeros(M2) |
| 51 | W3_init = np.random.randn(M2, K) / np.sqrt(M2) |
| 52 | b3_init = np.zeros(K) |
| 53 | |
| 54 | |
| 55 | # define variables and expressions |
| 56 | X = tf.placeholder(tf.float32, shape=(None, D), name='X') |
| 57 | T = tf.placeholder(tf.float32, shape=(None, K), name='T') |
| 58 | W1 = tf.Variable(W1_init.astype(np.float32)) |
| 59 | b1 = tf.Variable(b1_init.astype(np.float32)) |
| 60 | W2 = tf.Variable(W2_init.astype(np.float32)) |
| 61 | b2 = tf.Variable(b2_init.astype(np.float32)) |
| 62 | W3 = tf.Variable(W3_init.astype(np.float32)) |
| 63 | b3 = tf.Variable(b3_init.astype(np.float32)) |
| 64 | |
| 65 | # define the model |
| 66 | Z1 = tf.nn.relu( tf.matmul(X, W1) + b1 ) |
| 67 | Z2 = tf.nn.relu( tf.matmul(Z1, W2) + b2 ) |
| 68 | Yish = tf.matmul(Z2, W3) + b3 # remember, the cost function does the softmaxing! weird, right? |
| 69 | |
| 70 | # softmax_cross_entropy_with_logits take in the "logits" |
| 71 | # if you wanted to know the actual output of the neural net, |
| 72 | # you could pass "Yish" into tf.nn.softmax(logits) |
| 73 | cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(logits=Yish, labels=T)) |
| 74 | |
| 75 | # we choose the optimizer but don't implement the algorithm ourselves |
| 76 | # let's go with RMSprop, since we just learned about it. |
| 77 | # it includes momentum! |
| 78 | train_op = tf.train.RMSPropOptimizer(lr, decay=0.99, momentum=0.9).minimize(cost) |
| 79 | |
| 80 | # we'll use this to calculate the error rate |
| 81 | predict_op = tf.argmax(Yish, 1) |
| 82 | |
| 83 | costs = [] |
no test coverage detected