(self, trees, test_trees, reg=1e-3, epochs=8, train_inner_nodes=False)
| 89 | self.f = activation |
| 90 | |
| 91 | def fit(self, trees, test_trees, reg=1e-3, epochs=8, train_inner_nodes=False): |
| 92 | D = self.D |
| 93 | V = self.V |
| 94 | K = self.K |
| 95 | N = len(trees) |
| 96 | |
| 97 | We = init_weight(V, D) |
| 98 | W11 = np.random.randn(D, D, D) / np.sqrt(3*D) |
| 99 | W22 = np.random.randn(D, D, D) / np.sqrt(3*D) |
| 100 | W12 = np.random.randn(D, D, D) / np.sqrt(3*D) |
| 101 | W1 = init_weight(D, D) |
| 102 | W2 = init_weight(D, D) |
| 103 | bh = np.zeros(D) |
| 104 | Wo = init_weight(D, K) |
| 105 | bo = np.zeros(K) |
| 106 | |
| 107 | self.We = theano.shared(We) |
| 108 | self.W11 = theano.shared(W11) |
| 109 | self.W22 = theano.shared(W22) |
| 110 | self.W12 = theano.shared(W12) |
| 111 | self.W1 = theano.shared(W1) |
| 112 | self.W2 = theano.shared(W2) |
| 113 | self.bh = theano.shared(bh) |
| 114 | self.Wo = theano.shared(Wo) |
| 115 | self.bo = theano.shared(bo) |
| 116 | self.params = [self.We, self.W11, self.W22, self.W12, self.W1, self.W2, self.bh, self.Wo, self.bo] |
| 117 | |
| 118 | lr = T.scalar('learning_rate') |
| 119 | words = T.ivector('words') |
| 120 | left_children = T.ivector('left_children') |
| 121 | right_children = T.ivector('right_children') |
| 122 | labels = T.ivector('labels') |
| 123 | |
| 124 | def recurrence(n, hiddens, words, left, right): |
| 125 | w = words[n] |
| 126 | # any non-word will have index -1 |
| 127 | hiddens = T.switch( |
| 128 | T.ge(w, 0), |
| 129 | T.set_subtensor(hiddens[n], self.We[w]), |
| 130 | T.set_subtensor(hiddens[n], |
| 131 | self.f( |
| 132 | hiddens[left[n]].dot(self.W11).dot(hiddens[left[n]]) + |
| 133 | hiddens[right[n]].dot(self.W22).dot(hiddens[right[n]]) + |
| 134 | hiddens[left[n]].dot(self.W12).dot(hiddens[right[n]]) + |
| 135 | hiddens[left[n]].dot(self.W1) + |
| 136 | hiddens[right[n]].dot(self.W2) + |
| 137 | self.bh |
| 138 | ) |
| 139 | ) |
| 140 | ) |
| 141 | return hiddens |
| 142 | |
| 143 | hiddens = T.zeros((words.shape[0], D)) |
| 144 | |
| 145 | h, _ = theano.scan( |
| 146 | fn=recurrence, |
| 147 | outputs_info=[hiddens], |
| 148 | n_steps=words.shape[0], |
no test coverage detected