| 100 | return np.array([self.get_cost(x) for x, p in zip(X, P) if p < p_cost]) |
| 101 | |
| 102 | def set(self, pi, A, B): |
| 103 | self.pi = theano.shared(pi) |
| 104 | self.A = theano.shared(A) |
| 105 | self.B = theano.shared(B) |
| 106 | |
| 107 | # define cost |
| 108 | thx = T.ivector('thx') |
| 109 | def recurrence(t, old_a, x): |
| 110 | a = old_a.dot(self.A) * self.B[:, x[t]] |
| 111 | s = a.sum() |
| 112 | return (a / s), s |
| 113 | |
| 114 | [alpha, scale], _ = theano.scan( |
| 115 | fn=recurrence, |
| 116 | sequences=T.arange(1, thx.shape[0]), |
| 117 | outputs_info=[self.pi*self.B[:,thx[0]], None], |
| 118 | n_steps=thx.shape[0]-1, |
| 119 | non_sequences=thx |
| 120 | ) |
| 121 | |
| 122 | cost = -T.log(scale).sum() |
| 123 | self.cost_op = theano.function( |
| 124 | inputs=[thx], |
| 125 | outputs=cost, |
| 126 | allow_input_downcast=True, |
| 127 | ) |
| 128 | return thx, cost |
| 129 | |
| 130 | |
| 131 | def fit_coin(): |