| 130 | |
| 131 | |
| 132 | class DNN(object): |
| 133 | def __init__(self, hidden_layer_sizes, UnsupervisedModel=AutoEncoder): |
| 134 | self.hidden_layers = [] |
| 135 | count = 0 |
| 136 | for M in hidden_layer_sizes: |
| 137 | ae = UnsupervisedModel(M, count) |
| 138 | self.hidden_layers.append(ae) |
| 139 | count += 1 |
| 140 | |
| 141 | |
| 142 | def fit(self, X, Y, Xtest, Ytest, |
| 143 | pretrain=True, |
| 144 | train_head_only=False, |
| 145 | learning_rate=0.1, |
| 146 | mu=0.99, |
| 147 | reg=0.0, |
| 148 | epochs=1, |
| 149 | batch_sz=100): |
| 150 | |
| 151 | # cast to float32 |
| 152 | learning_rate = np.float32(learning_rate) |
| 153 | mu = np.float32(mu) |
| 154 | reg = np.float32(reg) |
| 155 | |
| 156 | # greedy layer-wise training of autoencoders |
| 157 | pretrain_epochs = 2 |
| 158 | if not pretrain: |
| 159 | pretrain_epochs = 0 |
| 160 | |
| 161 | current_input = X |
| 162 | for ae in self.hidden_layers: |
| 163 | ae.fit(current_input, epochs=pretrain_epochs) |
| 164 | |
| 165 | # create current_input for the next layer |
| 166 | current_input = ae.hidden_op(current_input) |
| 167 | |
| 168 | # initialize logistic regression layer |
| 169 | N = len(Y) |
| 170 | K = len(set(Y)) |
| 171 | W0 = init_weights((self.hidden_layers[-1].M, K)) |
| 172 | self.W = theano.shared(W0, "W_logreg") |
| 173 | self.b = theano.shared(np.zeros(K, dtype=np.float32), "b_logreg") |
| 174 | |
| 175 | self.params = [self.W, self.b] |
| 176 | if not train_head_only: |
| 177 | for ae in self.hidden_layers: |
| 178 | self.params += ae.forward_params |
| 179 | |
| 180 | X_in = T.matrix('X_in') |
| 181 | targets = T.ivector('Targets') |
| 182 | pY = self.forward(X_in) |
| 183 | |
| 184 | squared_magnitude = [(p*p).sum() for p in self.params] |
| 185 | reg_cost = T.sum(squared_magnitude) |
| 186 | cost = -T.mean( T.log(pY[T.arange(pY.shape[0]), targets]) ) + reg*reg_cost |
| 187 | prediction = self.predict(X_in) |
| 188 | cost_predict_op = theano.function( |
| 189 | inputs=[X_in, targets], |