| 142 | return rnn |
| 143 | |
| 144 | def set(self, We, Wx, Wh, bh, h0, Wo, bo, activation): |
| 145 | self.f = activation |
| 146 | |
| 147 | # redundant - see how you can improve it |
| 148 | self.We = theano.shared(We) |
| 149 | self.Wx = theano.shared(Wx) |
| 150 | self.Wh = theano.shared(Wh) |
| 151 | self.bh = theano.shared(bh) |
| 152 | self.h0 = theano.shared(h0) |
| 153 | self.Wo = theano.shared(Wo) |
| 154 | self.bo = theano.shared(bo) |
| 155 | self.params = [self.We, self.Wx, self.Wh, self.bh, self.h0, self.Wo, self.bo] |
| 156 | |
| 157 | thX = T.ivector('X') |
| 158 | Ei = self.We[thX] # will be a TxD matrix |
| 159 | thY = T.ivector('Y') |
| 160 | |
| 161 | def recurrence(x_t, h_t1): |
| 162 | # returns h(t), y(t) |
| 163 | h_t = self.f(x_t.dot(self.Wx) + h_t1.dot(self.Wh) + self.bh) |
| 164 | y_t = T.nnet.softmax(h_t.dot(self.Wo) + self.bo) |
| 165 | return h_t, y_t |
| 166 | |
| 167 | [h, y], _ = theano.scan( |
| 168 | fn=recurrence, |
| 169 | outputs_info=[self.h0, None], |
| 170 | sequences=Ei, |
| 171 | n_steps=Ei.shape[0], |
| 172 | ) |
| 173 | |
| 174 | py_x = y[:, 0, :] |
| 175 | prediction = T.argmax(py_x, axis=1) |
| 176 | self.predict_op = theano.function( |
| 177 | inputs=[thX], |
| 178 | outputs=prediction, |
| 179 | allow_input_downcast=True, |
| 180 | ) |
| 181 | |
| 182 | def generate(self, pi, word2idx): |
| 183 | # convert word2idx -> idx2word |