| 23 | |
| 24 | |
| 25 | class RNN: |
| 26 | def __init__(self, D, hidden_layer_sizes, V, K): |
| 27 | self.hidden_layer_sizes = hidden_layer_sizes |
| 28 | self.D = D |
| 29 | self.V = V |
| 30 | self.K = K |
| 31 | |
| 32 | def fit(self, X, Y, learning_rate=1e-4, mu=0.99, epochs=30, show_fig=True, activation=T.nnet.relu, RecurrentUnit=GRU, normalize=False): |
| 33 | D = self.D |
| 34 | V = self.V |
| 35 | N = len(X) |
| 36 | |
| 37 | We = init_weight(V, D) |
| 38 | self.hidden_layers = [] |
| 39 | Mi = D |
| 40 | for Mo in self.hidden_layer_sizes: |
| 41 | ru = RecurrentUnit(Mi, Mo, activation) |
| 42 | self.hidden_layers.append(ru) |
| 43 | Mi = Mo |
| 44 | |
| 45 | Wo = init_weight(Mi, self.K) |
| 46 | bo = np.zeros(self.K) |
| 47 | |
| 48 | self.We = theano.shared(We) |
| 49 | self.Wo = theano.shared(Wo) |
| 50 | self.bo = theano.shared(bo) |
| 51 | self.params = [self.Wo, self.bo] |
| 52 | for ru in self.hidden_layers: |
| 53 | self.params += ru.params |
| 54 | |
| 55 | thX = T.ivector('X') |
| 56 | thY = T.ivector('Y') |
| 57 | |
| 58 | Z = self.We[thX] |
| 59 | for ru in self.hidden_layers: |
| 60 | Z = ru.output(Z) |
| 61 | py_x = T.nnet.softmax(Z.dot(self.Wo) + self.bo) |
| 62 | |
| 63 | testf = theano.function( |
| 64 | inputs=[thX], |
| 65 | outputs=py_x, |
| 66 | ) |
| 67 | testout = testf(X[0]) |
| 68 | print("py_x.shape:", testout.shape) |
| 69 | |
| 70 | prediction = T.argmax(py_x, axis=1) |
| 71 | |
| 72 | cost = -T.mean(T.log(py_x[T.arange(thY.shape[0]), thY])) |
| 73 | grads = T.grad(cost, self.params) |
| 74 | dparams = [theano.shared(p.get_value()*0) for p in self.params] |
| 75 | |
| 76 | dWe = theano.shared(self.We.get_value()*0) |
| 77 | gWe = T.grad(cost, self.We) |
| 78 | dWe_update = mu*dWe - learning_rate*gWe |
| 79 | We_update = self.We + dWe_update |
| 80 | if normalize: |
| 81 | We_update /= We_update.norm(2) |
| 82 | |