8516950fa0683319a31f91e80148ddd62ff56a49,python/baseline/pytorch/crf.py,,viterbi,#Any#Any#Any#Any#Any#Any#,164

Before Change


    
    seq_len, batch_size, tag_size = unary.size()
    min_length = torch.min(lengths)
    batch_range = torch.arange(batch_size, dtype=torch.int64)
    backpointers = []

    // Alphas: [B, 1, N]
    alphas = torch.Tensor(batch_size, 1, tag_size).fill_(-1e4).to(unary.device)
    alphas[:, 0, start_idx] = 0
    alphas = F.log_softmax(alphas, dim=-1) if norm else alphas

    for i, unary_t in enumerate(unary):
        next_tag_var = alphas + trans
        viterbi, best_tag_ids = torch.max(next_tag_var, 2)
        backpointers.append(best_tag_ids.data)
        new_alphas = viterbi + unary_t
        new_alphas.unsqueeze_(1)
        if i >= min_length:
            mask = (i < lengths).view(-1, 1, 1)
            alphas = alphas.masked_fill(mask, 0) + new_alphas.masked_fill(mask == 0, 0)
        else:
            alphas = new_alphas

    // Add end tag
    terminal_var = alphas.squeeze(1) + trans[:, end_idx]
    _, best_tag_id = torch.max(terminal_var, 1)
    path_score = terminal_var[batch_range, best_tag_id]  // Select best_tag from each batch

    best_path = [best_tag_id]
    // Flip lengths
    rev_len = seq_len - lengths - 1
    for i, backpointer_t in enumerate(reversed(backpointers)):
        // Get new best tag candidate
        new_best_tag_id = backpointer_t[batch_range, best_tag_id]
        // We are going backwards now, if you passed your flipped length then you aren"t in your real results yet
        mask = (i > rev_len)
        best_tag_id = best_tag_id.masked_fill(mask, 0) + new_best_tag_id.masked_fill(mask == 0, 0)
        best_path.append(best_tag_id)

After Change


    rev_len = seq_len - lengths - 1
    for i, backpointer_t in enumerate(reversed(backpointers)):
        // Get new best tag candidate
        new_best_tag_id = backpointer_t.gather(1, best_tag_id.unsqueeze(1)).squeeze(1)
        // We are going backwards now, if you passed your flipped length then you aren"t in your real results yet
        mask = (i > rev_len)
        best_tag_id = best_tag_id.masked_fill(mask, 0) + new_best_tag_id.masked_fill(mask == 0, 0)
        best_path.append(best_tag_id)
Italian Trulli
In pattern: SUPERPATTERN

Frequency: 3

Non-data size: 6

Instances


Project Name: dpressel/mead-baseline
Commit Name: 8516950fa0683319a31f91e80148ddd62ff56a49
Time: 2019-01-10
Author: blester125@users.noreply.github.com
File Name: python/baseline/pytorch/crf.py
Class Name:
Method Name: viterbi


Project Name: dpressel/mead-baseline
Commit Name: 3218a3987ef9f83d1ad5efd6f96d6f6c741c3cfa
Time: 2019-09-27
Author: dpressel@gmail.com
File Name: python/eight_mile/pytorch/layers.py
Class Name:
Method Name: viterbi


Project Name: NVIDIA/sentiment-discovery
Commit Name: 65c9a4857e41dcde4f5dae8a77e6cdf03953646a
Time: 2018-03-19
Author: raulp@dbcluster.nvidia.com
File Name: model/model.py
Class Name: RNNFeaturizer
Method Name: forward