()
| 48 | |
| 49 | |
| 50 | def main(): |
| 51 | train = loadmat('../large_files/train_32x32.mat') |
| 52 | test = loadmat('../large_files/test_32x32.mat') |
| 53 | |
| 54 | # Need to scale! don't leave as 0..255 |
| 55 | # Y is a N x 1 matrix with values 1..10 (MATLAB indexes by 1) |
| 56 | # So flatten it and make it 0..9 |
| 57 | # Also need indicator matrix for cost calculation |
| 58 | Xtrain = convolve_flatten(train['X'].astype(np.float32)) |
| 59 | Ytrain = train['y'].flatten() - 1 |
| 60 | Xtrain, Ytrain = shuffle(Xtrain, Ytrain) |
| 61 | |
| 62 | Xtest = convolve_flatten(test['X'].astype(np.float32)) |
| 63 | Ytest = test['y'].flatten() - 1 |
| 64 | |
| 65 | # gradient descent params |
| 66 | max_iter = 15 |
| 67 | print_period = 10 |
| 68 | N, D = Xtrain.shape |
| 69 | batch_sz = 500 |
| 70 | n_batches = N // batch_sz |
| 71 | |
| 72 | # initial weights |
| 73 | M1 = 1000 # hidden layer size |
| 74 | M2 = 500 |
| 75 | K = 10 |
| 76 | W1_init = np.random.randn(D, M1) / np.sqrt(D + M1) |
| 77 | b1_init = np.zeros(M1) |
| 78 | W2_init = np.random.randn(M1, M2) / np.sqrt(M1 + M2) |
| 79 | b2_init = np.zeros(M2) |
| 80 | W3_init = np.random.randn(M2, K) / np.sqrt(M2 + K) |
| 81 | b3_init = np.zeros(K) |
| 82 | |
| 83 | # define variables and expressions |
| 84 | X = tf.placeholder(tf.float32, shape=(None, D), name='X') |
| 85 | T = tf.placeholder(tf.int32, shape=(None,), name='T') |
| 86 | W1 = tf.Variable(W1_init.astype(np.float32)) |
| 87 | b1 = tf.Variable(b1_init.astype(np.float32)) |
| 88 | W2 = tf.Variable(W2_init.astype(np.float32)) |
| 89 | b2 = tf.Variable(b2_init.astype(np.float32)) |
| 90 | W3 = tf.Variable(W3_init.astype(np.float32)) |
| 91 | b3 = tf.Variable(b3_init.astype(np.float32)) |
| 92 | |
| 93 | Z1 = tf.nn.relu( tf.matmul(X, W1) + b1 ) |
| 94 | Z2 = tf.nn.relu( tf.matmul(Z1, W2) + b2 ) |
| 95 | Yish = tf.matmul(Z2, W3) + b3 |
| 96 | |
| 97 | cost = tf.reduce_sum( |
| 98 | tf.nn.sparse_softmax_cross_entropy_with_logits( |
| 99 | logits=Yish, |
| 100 | labels=T |
| 101 | ) |
| 102 | ) |
| 103 | |
| 104 | train_op = tf.train.RMSPropOptimizer(0.0001, decay=0.99, momentum=0.9).minimize(cost) |
| 105 | |
| 106 | # we'll use this to calculate the error rate |
| 107 | predict_op = tf.argmax(Yish, 1) |
no test coverage detected