| 90 | return np.array([self.get_cost(x) for x in X]) |
| 91 | |
| 92 | def set(self, preSoftmaxPi, preSoftmaxA, preSoftmaxB): |
| 93 | self.preSoftmaxPi = theano.shared(preSoftmaxPi) |
| 94 | self.preSoftmaxA = theano.shared(preSoftmaxA) |
| 95 | self.preSoftmaxB = theano.shared(preSoftmaxB) |
| 96 | |
| 97 | pi = T.nnet.softmax(self.preSoftmaxPi).flatten() |
| 98 | # softmax returns 1xD if input is a 1-D array of size D |
| 99 | A = T.nnet.softmax(self.preSoftmaxA) |
| 100 | B = T.nnet.softmax(self.preSoftmaxB) |
| 101 | |
| 102 | # define cost |
| 103 | thx = T.ivector('thx') |
| 104 | def recurrence(t, old_a, x): |
| 105 | a = old_a.dot(A) * B[:, x[t]] |
| 106 | s = a.sum() |
| 107 | return (a / s), s |
| 108 | |
| 109 | [alpha, scale], _ = theano.scan( |
| 110 | fn=recurrence, |
| 111 | sequences=T.arange(1, thx.shape[0]), |
| 112 | outputs_info=[pi*B[:,thx[0]], None], |
| 113 | n_steps=thx.shape[0]-1, |
| 114 | non_sequences=thx |
| 115 | ) |
| 116 | |
| 117 | cost = -T.log(scale).sum() |
| 118 | self.cost_op = theano.function( |
| 119 | inputs=[thx], |
| 120 | outputs=cost, |
| 121 | allow_input_downcast=True, |
| 122 | ) |
| 123 | return thx, cost |
| 124 | |
| 125 | |
| 126 | def fit_coin(): |