(self, feats)
| 183 | torch.randn(2, 1, self.hidden_dim // 2)) |
| 184 | |
| 185 | def _forward_alg(self, feats): |
| 186 | # Do the forward algorithm to compute the partition function |
| 187 | init_alphas = torch.full((1, self.tagset_size), -10000.) |
| 188 | # START_TAG has all of the score. |
| 189 | init_alphas[0][self.tag_to_ix[START_TAG]] = 0. |
| 190 | |
| 191 | # Wrap in a variable so that we will get automatic backprop |
| 192 | forward_var = init_alphas |
| 193 | |
| 194 | # Iterate through the sentence |
| 195 | for feat in feats: |
| 196 | alphas_t = [] # The forward tensors at this timestep |
| 197 | for next_tag in range(self.tagset_size): |
| 198 | # broadcast the emission score: it is the same regardless of |
| 199 | # the previous tag |
| 200 | emit_score = feat[next_tag].view( |
| 201 | 1, -1).expand(1, self.tagset_size) |
| 202 | # the ith entry of trans_score is the score of transitioning to |
| 203 | # next_tag from i |
| 204 | trans_score = self.transitions[next_tag].view(1, -1) |
| 205 | # The ith entry of next_tag_var is the value for the |
| 206 | # edge (i -> next_tag) before we do log-sum-exp |
| 207 | next_tag_var = forward_var + trans_score + emit_score |
| 208 | # The forward variable for this tag is log-sum-exp of all the |
| 209 | # scores. |
| 210 | alphas_t.append(log_sum_exp(next_tag_var).view(1)) |
| 211 | forward_var = torch.cat(alphas_t).view(1, -1) |
| 212 | terminal_var = forward_var + self.transitions[self.tag_to_ix[STOP_TAG]] |
| 213 | alpha = log_sum_exp(terminal_var) |
| 214 | return alpha |
| 215 | |
| 216 | def _get_lstm_features(self, sentence): |
| 217 | self.hidden = self.init_hidden() |
no test coverage detected