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 lac calculation precision #962

Merged
merged 3 commits into from
Sep 2, 2021
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
21 changes: 9 additions & 12 deletions paddlenlp/layers/crf.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,11 @@ def forward(self, inputs, lengths):
alpha = self._initialize_alpha(batch_size)
else:
alpha = paddle.zeros((batch_size, self.num_tags), dtype='float32')
# avoid call fill_constant in iteration
zeros = paddle.zeros([1], dtype='int64')
ones = paddle.ones([1], dtype='int64')
for i, logit in enumerate(inputs_t[:max_seq_len]):
# if not with_start_stop_tag, the first label has not antecedent tag.
if i == 0 and not self.with_start_stop_tag:
alpha = logit
left_length = left_length - ones
left_length = left_length - 1
continue
alpha_exp = alpha.unsqueeze(2)
# alpha_trn_sum: batch_size, n_labels, n_labels
Expand All @@ -416,36 +413,36 @@ def forward(self, inputs, lengths):
# Now add the emission scores
alpha_nxt = alpha_max + logit

mask = paddle.cast((left_length > zeros), dtype='float32')
alpha = mask * alpha_nxt + (ones - mask) * alpha
mask = paddle.cast((left_length > 0), dtype='float32')
alpha = mask * alpha_nxt + (1 - mask) * alpha

if self.with_start_stop_tag:
mask = paddle.cast((left_length == ones), dtype='float32')
mask = paddle.cast((left_length == 1), dtype='float32')
alpha += mask * trans_exp[:, self.stop_idx]

left_length = left_length - ones
left_length = left_length - 1

# last_ids: batch_size
scores, last_ids = alpha.max(1), alpha.argmax(1)
# Trace back the best path
# historys: seq_len, batch_size, n_labels
historys = paddle.stack(historys)
left_length = left_length[:, 0]
tag_mask = paddle.cast((left_length >= zeros), 'int64')
tag_mask = paddle.cast((left_length >= 0), 'int64')
last_ids_update = last_ids * tag_mask

batch_path = [last_ids_update]
batch_offset = self._get_batch_index(batch_size) * n_label
historys = paddle.reverse(historys, [0])
for hist in historys:
# hist: batch_size, n_labels
left_length = left_length + ones
left_length = left_length + 1
gather_idx = batch_offset + last_ids
tag_mask = paddle.cast((left_length >= zeros), 'int64')
tag_mask = paddle.cast((left_length >= 0), 'int64')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

验证一下动转静

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

last_ids_update = paddle.gather(hist.flatten(),
gather_idx) * tag_mask
batch_path.append(last_ids_update)
last_ids = last_ids_update + last_ids * (ones - tag_mask)
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