(vec)
| 140 | |
| 141 | # Compute log sum exp in a numerically stable way for the forward algorithm |
| 142 | def log_sum_exp(vec): |
| 143 | max_score = vec[0, argmax(vec)] |
| 144 | max_score_broadcast = max_score.view(1, -1).expand(1, vec.size()[1]) |
| 145 | return max_score + \ |
| 146 | torch.log(torch.sum(torch.exp(vec - max_score_broadcast))) |
| 147 | |
| 148 | ##################################################################### |
| 149 | # Create model |