()
| 61 | |
| 62 | |
| 63 | def main(): |
| 64 | # step 1: load the data, transform as needed |
| 65 | train, test = get_data() |
| 66 | |
| 67 | # Need to scale! don't leave as 0..255 |
| 68 | # Y is a N x 1 matrix with values 1..10 (MATLAB indexes by 1) |
| 69 | # So flatten it and make it 0..9 |
| 70 | # Also need indicator matrix for cost calculation |
| 71 | Xtrain = rearrange(train['X']) |
| 72 | Ytrain = train['y'].flatten() - 1 |
| 73 | del train |
| 74 | Xtrain, Ytrain = shuffle(Xtrain, Ytrain) |
| 75 | |
| 76 | Xtest = rearrange(test['X']) |
| 77 | Ytest = test['y'].flatten() - 1 |
| 78 | del test |
| 79 | |
| 80 | |
| 81 | max_iter = 6 |
| 82 | print_period = 10 |
| 83 | |
| 84 | lr = np.float32(1e-3) |
| 85 | mu = np.float32(0.9) |
| 86 | |
| 87 | N = Xtrain.shape[0] |
| 88 | batch_sz = 500 |
| 89 | n_batches = N // batch_sz |
| 90 | |
| 91 | M = 500 |
| 92 | K = 10 |
| 93 | poolsz = (2, 2) |
| 94 | |
| 95 | # after conv will be of dimension 32 - 5 + 1 = 28 |
| 96 | # after downsample 28 / 2 = 14 |
| 97 | W1_shape = (20, 3, 5, 5) # (num_feature_maps, num_color_channels, filter_width, filter_height) |
| 98 | W1_init = init_filter(W1_shape, poolsz) |
| 99 | b1_init = np.zeros(W1_shape[0], dtype=np.float32) # one bias per output feature map |
| 100 | |
| 101 | # after conv will be of dimension 14 - 5 + 1 = 10 |
| 102 | # after downsample 10 / 2 = 5 |
| 103 | W2_shape = (50, 20, 5, 5) # (num_feature_maps, old_num_feature_maps, filter_width, filter_height) |
| 104 | W2_init = init_filter(W2_shape, poolsz) |
| 105 | b2_init = np.zeros(W2_shape[0], dtype=np.float32) |
| 106 | |
| 107 | # vanilla ANN weights |
| 108 | W3_init = np.random.randn(W2_shape[0]*5*5, M) / np.sqrt(W2_shape[0]*5*5 + M) |
| 109 | b3_init = np.zeros(M, dtype=np.float32) |
| 110 | W4_init = np.random.randn(M, K) / np.sqrt(M + K) |
| 111 | b4_init = np.zeros(K, dtype=np.float32) |
| 112 | |
| 113 | |
| 114 | # step 2: define theano variables and expressions |
| 115 | X = T.tensor4('X', dtype='float32') |
| 116 | Y = T.ivector('T') |
| 117 | W1 = theano.shared(W1_init, 'W1') |
| 118 | b1 = theano.shared(b1_init, 'b1') |
| 119 | W2 = theano.shared(W2_init, 'W2') |
| 120 | b2 = theano.shared(b2_init, 'b2') |
no test coverage detected