-
Notifications
You must be signed in to change notification settings - Fork 485
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
Added generate.probabilities
for BeamSearch
#895
Open
LouisHernandez17
wants to merge
12
commits into
dottxt-ai:main
Choose a base branch
from
craft-ai:probabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
390e86d
Added generate.probabilities for BeamSearch
LouisHernandez17 3493539
Added tests for generate.probabilities
LouisHernandez17 a433ad9
Returns a dict and not a list when batch_size=1
LouisHernandez17 9e311a0
Merge branch 'main' into probabilities
LouisHernandez17 4f0766b
Added back errors to filterwarning
LouisHernandez17 761b64b
removed useless modifications
LouisHernandez17 c40db35
Added default sampler + docs + correct protocal for BeamSearch
LouisHernandez17 aa71280
Merge branch 'main' into probabilities
LouisHernandez17 11d55ab
Merge branch 'main' into probabilities
LouisHernandez17 0853cc8
Merge branch 'main' into probabilities
LouisHernandez17 9ea14c8
Merge branch 'main' into probabilities
LouisHernandez17 a3a9cc3
Merge branch 'main' into probabilities
LouisHernandez17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Multiple choices with Probabilities | ||
|
||
Outlines allows you to generate probabilities for different options, giving you insights into the model's confidence for each choice. | ||
|
||
```python | ||
from outlines import models, generate | ||
|
||
model = models.transformers("mistralai/Mistral-7B-v0.1") | ||
probabilities = generate.probabilities(model, ["skirt", "dress", "pen", "jacket"]) | ||
answer = probabilities("Pick the odd word out: skirt, dress, pen, jacket") | ||
print(answer) | ||
``` | ||
|
||
!!! Warning "Compatibility" | ||
|
||
`generate.probabilities` uses a beam search sampler. It is not compatible with other samplers. Ensure that no other samplers are used in conjunction with this method. | ||
|
||
## How It Works | ||
|
||
|
||
Beam search is a heuristic search algorithm used to explore the most promising sequences in a limited set. In text generation, it maintains the top `k` sequences (beams) at each step based on their cumulative probabilities. Each sequence has a weight, which is the product of the probabilities of its tokens, representing the likelihood of the sequence according to the model. | ||
|
||
!!! Warning "Probabilities Summation" | ||
|
||
The probabilities returned by `generate.probabilities` might not sum to one because the `topk` limitation only keeps the best sequences. This means other sequences with potentially non-negligible probabilities are not taken into account, leading to an incomplete probability distribution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import List | ||
|
||
from outlines.generate.api import SequenceGenerator | ||
from outlines.samplers import BeamSearchSampler, Sampler, beam_search | ||
|
||
from .regex import regex | ||
|
||
|
||
def probabilities( | ||
model, choices: List[str], sampler: Sampler = beam_search() | ||
) -> SequenceGenerator: | ||
regex_str = r"(" + r"|".join(choices) + r")" | ||
assert isinstance( | ||
sampler, BeamSearchSampler | ||
), "Only BeamSearchSampler is supported for probabilities" | ||
generator = regex(model, regex_str, sampler) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns a |
||
generator.format_sequence = lambda x: x | ||
generator.probabilities = choices | ||
|
||
return generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please see the answer printed?