Skip to content

Use log probabilities in beam search. #23

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

Merged
merged 1 commit into from
Jul 26, 2021

Conversation

a-sneddon
Copy link
Contributor

Implemented log-space operations in beam search to avoid underflow when dealing with regular probabilities.

child_beam.pr_text = parent_beam.pr_text * bigram_prob # probability of char sequence
bigram_prob = lm_factor * np.log(lm.get_char_bigram(c1, c2))
if parent_beam.is_empty():
child_beam.pr_text = bigram_prob # first char in beam
Copy link
Owner

Choose a reason for hiding this comment

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

no special case needed (no if else), as an empty beam is initialized with pr_text=log(1)=0, so adding parent_beam.pr_text + bigram_prob is OK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for picking this up.

@@ -75,8 +81,8 @@ def beam_search(mat: np.ndarray, labels: str, beam_width: int = 25, lm: Optional
last = BeamState()
labeling = ()
last.entries[labeling] = BeamEntry()
last.entries[labeling].pr_blank = 1
last.entries[labeling].pr_total = 1
last.entries[labeling].pr_blank = LOG_ZERO
Copy link
Owner

Choose a reason for hiding this comment

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

should be log(1)==0 instead of LOG_ZERO which is -inf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true, that allows the below if statements to be cleaned up also, thanks for picking this up.

# in case of non-empty beam
if labeling:
# probability of paths with repeated last char at the end
pr_non_blank = last.entries[labeling].pr_non_blank * mat[t, labeling[-1]]
Copy link
Owner

@githubharald githubharald Jul 26, 2021

Choose a reason for hiding this comment

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

"cannot add to -inf" -> I think this is due to initialization to log(0) instead of log(1).
-inf + x == -inf, which makes sense, because 0*x=0.


# probability of paths ending with a blank
pr_blank = last.entries[labeling].pr_total * mat[t, blank_idx]
if last.entries[labeling].pr_total == LOG_ZERO:
Copy link
Owner

Choose a reason for hiding this comment

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

same as above


# probability of paths ending with a blank
pr_blank = last.entries[labeling].pr_total * mat[t, blank_idx]
if last.entries[labeling].pr_total == LOG_ZERO:
pr_blank = np.log(mat[t, blank_idx]) # cannot add to -inf
Copy link
Owner

Choose a reason for hiding this comment

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

directly using np.log creates warning outputs for np.log(0).
Define some wrapper function which takes care of that:

def log(x):
    with np.errstate(divide='ignore'):
        return np.log(x)

@githubharald
Copy link
Owner

thanks for your contribution. Using log-probs is a good idea 👍.
There are a few minor things I suggest to change, to have as few if-else branches directly in the main decoding function, and to avoid having numpy print warning messages to the console.

@githubharald
Copy link
Owner

I anyway will push some changes to the repo, so I'll integrate your changes and will do the small modifications myself.
Thanks again for your contribution 👍 .

@githubharald githubharald merged commit 8766399 into githubharald:master Jul 26, 2021
@a-sneddon
Copy link
Contributor Author

Thanks @githubharald for accepting the pull request. I've taken a look at the changes you've implemented since and they look good, thanks for your improvements and for maintaining the repo.

@a-sneddon a-sneddon deleted the log-probs branch July 27, 2021 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy