()
| 49 | |
| 50 | |
| 51 | def main(): |
| 52 | train, test = get_data() |
| 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 = rearrange(train['X']) |
| 59 | Ytrain = train['y'].flatten() - 1 |
| 60 | # print len(Ytrain) |
| 61 | del train |
| 62 | Xtrain, Ytrain = shuffle(Xtrain, Ytrain) |
| 63 | |
| 64 | Xtest = rearrange(test['X']) |
| 65 | Ytest = test['y'].flatten() - 1 |
| 66 | del test |
| 67 | |
| 68 | # gradient descent params |
| 69 | max_iter = 6 |
| 70 | print_period = 10 |
| 71 | N = Xtrain.shape[0] |
| 72 | batch_sz = 500 |
| 73 | n_batches = N // batch_sz |
| 74 | |
| 75 | # limit samples since input will always have to be same size |
| 76 | # you could also just do N = N / batch_sz * batch_sz |
| 77 | Xtrain = Xtrain[:73000,] |
| 78 | Ytrain = Ytrain[:73000] |
| 79 | Xtest = Xtest[:26000,] |
| 80 | Ytest = Ytest[:26000] |
| 81 | # print "Xtest.shape:", Xtest.shape |
| 82 | # print "Ytest.shape:", Ytest.shape |
| 83 | |
| 84 | # initial weights |
| 85 | M = 500 |
| 86 | K = 10 |
| 87 | poolsz = (2, 2) |
| 88 | |
| 89 | W1_shape = (5, 5, 3, 20) # (filter_width, filter_height, num_color_channels, num_feature_maps) |
| 90 | W1_init = init_filter(W1_shape, poolsz) |
| 91 | b1_init = np.zeros(W1_shape[-1], dtype=np.float32) # one bias per output feature map |
| 92 | |
| 93 | W2_shape = (5, 5, 20, 50) # (filter_width, filter_height, old_num_feature_maps, num_feature_maps) |
| 94 | W2_init = init_filter(W2_shape, poolsz) |
| 95 | b2_init = np.zeros(W2_shape[-1], dtype=np.float32) |
| 96 | |
| 97 | # vanilla ANN weights |
| 98 | W3_init = np.random.randn(W2_shape[-1]*8*8, M) / np.sqrt(W2_shape[-1]*8*8 + M) |
| 99 | b3_init = np.zeros(M, dtype=np.float32) |
| 100 | W4_init = np.random.randn(M, K) / np.sqrt(M + K) |
| 101 | b4_init = np.zeros(K, dtype=np.float32) |
| 102 | |
| 103 | |
| 104 | # define variables and expressions |
| 105 | # using None as the first shape element takes up too much RAM unfortunately |
| 106 | X = tf.placeholder(tf.float32, shape=(batch_sz, 32, 32, 3), name='X') |
| 107 | T = tf.placeholder(tf.int32, shape=(batch_sz,), name='T') |
| 108 | W1 = tf.Variable(W1_init.astype(np.float32)) |
no test coverage detected