| 109 | return rnn |
| 110 | |
| 111 | def set(self, Wx, Wh, bh, h0, Wo, bo, activation): |
| 112 | self.f = activation |
| 113 | |
| 114 | # redundant - see how you can improve it |
| 115 | self.Wx = theano.shared(Wx) |
| 116 | self.Wh = theano.shared(Wh) |
| 117 | self.bh = theano.shared(bh) |
| 118 | self.h0 = theano.shared(h0) |
| 119 | self.Wo = theano.shared(Wo) |
| 120 | self.bo = theano.shared(bo) |
| 121 | self.params = [self.Wx, self.Wh, self.bh, self.h0, self.Wo, self.bo] |
| 122 | |
| 123 | thX = T.ivector('X') |
| 124 | thY = T.iscalar('Y') |
| 125 | |
| 126 | def recurrence(x_t, h_t1): |
| 127 | # returns h(t), y(t) |
| 128 | h_t = self.f(self.Wx[x_t] + h_t1.dot(self.Wh) + self.bh) |
| 129 | y_t = T.nnet.softmax(h_t.dot(self.Wo) + self.bo) |
| 130 | return h_t, y_t |
| 131 | |
| 132 | [h, y], _ = theano.scan( |
| 133 | fn=recurrence, |
| 134 | outputs_info=[self.h0, None], |
| 135 | sequences=thX, |
| 136 | n_steps=thX.shape[0], |
| 137 | ) |
| 138 | |
| 139 | py_x = y[-1, 0, :] # only interested in the final classification of the sequence |
| 140 | prediction = T.argmax(py_x) |
| 141 | self.predict_op = theano.function( |
| 142 | inputs=[thX], |
| 143 | outputs=prediction, |
| 144 | allow_input_downcast=True, |
| 145 | ) |
| 146 | return thX, thY, py_x, prediction |
| 147 | |
| 148 | |
| 149 | def train_poetry(): |