(self, X, learning_rate=10., mu=0.9, reg=0., activation=T.tanh, epochs=500, show_fig=False)
| 23 | self.V = V # vocabulary size |
| 24 | |
| 25 | def fit(self, X, learning_rate=10., mu=0.9, reg=0., activation=T.tanh, epochs=500, show_fig=False): |
| 26 | N = len(X) |
| 27 | D = self.D |
| 28 | M = self.M |
| 29 | V = self.V |
| 30 | |
| 31 | # initial weights |
| 32 | We = init_weight(V, D) |
| 33 | Wx = init_weight(D, M) |
| 34 | Wh = init_weight(M, M) |
| 35 | bh = np.zeros(M) |
| 36 | h0 = np.zeros(M) |
| 37 | # z = np.ones(M) |
| 38 | Wxz = init_weight(D, M) |
| 39 | Whz = init_weight(M, M) |
| 40 | bz = np.zeros(M) |
| 41 | Wo = init_weight(M, V) |
| 42 | bo = np.zeros(V) |
| 43 | |
| 44 | thX, thY, py_x, prediction = self.set(We, Wx, Wh, bh, h0, Wxz, Whz, bz, Wo, bo, activation) |
| 45 | |
| 46 | lr = T.scalar('lr') |
| 47 | |
| 48 | cost = -T.mean(T.log(py_x[T.arange(thY.shape[0]), thY])) |
| 49 | grads = T.grad(cost, self.params) |
| 50 | dparams = [theano.shared(p.get_value()*0) for p in self.params] |
| 51 | |
| 52 | updates = [] |
| 53 | for p, dp, g in zip(self.params, dparams, grads): |
| 54 | new_dp = mu*dp - lr*g |
| 55 | updates.append((dp, new_dp)) |
| 56 | |
| 57 | new_p = p + new_dp |
| 58 | updates.append((p, new_p)) |
| 59 | |
| 60 | self.predict_op = theano.function(inputs=[thX], outputs=prediction) |
| 61 | self.train_op = theano.function( |
| 62 | inputs=[thX, thY, lr], |
| 63 | outputs=[cost, prediction], |
| 64 | updates=updates |
| 65 | ) |
| 66 | |
| 67 | costs = [] |
| 68 | for i in range(epochs): |
| 69 | X = shuffle(X) |
| 70 | n_correct = 0 |
| 71 | n_total = 0 |
| 72 | cost = 0 |
| 73 | for j in range(N): |
| 74 | if np.random.random() < 0.1: |
| 75 | input_sequence = [0] + X[j] |
| 76 | output_sequence = X[j] + [1] |
| 77 | else: |
| 78 | input_sequence = [0] + X[j][:-1] |
| 79 | output_sequence = X[j] |
| 80 | n_total += len(output_sequence) |
| 81 | |
| 82 | # we set 0 to start and 1 to end |
no test coverage detected