Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix viterbi len=1 bug #1126

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion paddlenlp/layers/crf.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ def forward(self, inputs, lengths):

# last_ids: batch_size
scores, last_ids = alpha.max(1), alpha.argmax(1)
if max_seq_len == 1:
return scores, last_ids.unsqueeze(1)
# Trace back the best path
# historys: seq_len, batch_size, n_labels
historys = paddle.stack(historys)
Expand All @@ -438,10 +440,14 @@ def forward(self, inputs, lengths):
# hist: batch_size, n_labels
left_length = left_length + 1
gather_idx = batch_offset + last_ids
tag_mask = paddle.cast((left_length >= 0), 'int64')
tag_mask = paddle.cast((left_length > 0), 'int64')
last_ids_update = paddle.gather(hist.flatten(),
gather_idx) * tag_mask
zero_len_mask = paddle.cast((left_length == 0), 'int64')
last_ids_update = last_ids_update * (1 - zero_len_mask
) + last_ids * zero_len_mask
batch_path.append(last_ids_update)
tag_mask = paddle.cast((left_length >= 0), 'int64')
last_ids = last_ids_update + last_ids * (1 - tag_mask)
batch_path = paddle.reverse(paddle.stack(batch_path, 1), [1])
return scores, batch_path
Expand Down