| 120 | return rnn |
| 121 | |
| 122 | def set(self, We, Wx, Wh, bh, h0, Wxz, Whz, bz, Wo, bo, activation): |
| 123 | self.f = activation |
| 124 | |
| 125 | # redundant - see how you can improve it |
| 126 | self.We = theano.shared(We) |
| 127 | self.Wx = theano.shared(Wx) |
| 128 | self.Wh = theano.shared(Wh) |
| 129 | self.bh = theano.shared(bh) |
| 130 | self.h0 = theano.shared(h0) |
| 131 | self.Wxz = theano.shared(Wxz) |
| 132 | self.Whz = theano.shared(Whz) |
| 133 | self.bz = theano.shared(bz) |
| 134 | self.Wo = theano.shared(Wo) |
| 135 | self.bo = theano.shared(bo) |
| 136 | self.params = [self.We, self.Wx, self.Wh, self.bh, self.h0, self.Wxz, self.Whz, self.bz, self.Wo, self.bo] |
| 137 | |
| 138 | thX = T.ivector('X') |
| 139 | Ei = self.We[thX] # will be a TxD matrix |
| 140 | thY = T.ivector('Y') |
| 141 | |
| 142 | def recurrence(x_t, h_t1): |
| 143 | # returns h(t), y(t) |
| 144 | hhat_t = self.f(x_t.dot(self.Wx) + h_t1.dot(self.Wh) + self.bh) |
| 145 | z_t = T.nnet.sigmoid(x_t.dot(self.Wxz) + h_t1.dot(self.Whz) + self.bz) |
| 146 | h_t = (1 - z_t) * h_t1 + z_t * hhat_t |
| 147 | y_t = T.nnet.softmax(h_t.dot(self.Wo) + self.bo) |
| 148 | return h_t, y_t |
| 149 | |
| 150 | [h, y], _ = theano.scan( |
| 151 | fn=recurrence, |
| 152 | outputs_info=[self.h0, None], |
| 153 | sequences=Ei, |
| 154 | n_steps=Ei.shape[0], |
| 155 | ) |
| 156 | |
| 157 | py_x = y[:, 0, :] |
| 158 | prediction = T.argmax(py_x, axis=1) |
| 159 | self.predict_op = theano.function( |
| 160 | inputs=[thX], |
| 161 | outputs=[py_x, prediction], |
| 162 | allow_input_downcast=True, |
| 163 | ) |
| 164 | return thX, thY, py_x, prediction |
| 165 | |
| 166 | |
| 167 | def generate(self, word2idx): |