Divergent Beam Search
Overview
Divergent Beam Search is a variant of the beam search algorithm. Unlike the beam search where answers are constrained, which aims to find the answers with the highest probability of appearing, Divergent Beam Search focuses on finding answers that are not likely to be continued with another answer. Essentially, it finds the answers that maximize the probability of generating an answer before diverging into another subject given the prompt.
The core idea of this algorithm can be roughly summarized in the following optimization problem:
$$\max_{ans \in A} P(ans + diverging\ into\ another\ subject \mid prompt)$$
It is important that the set of answers $A$ is sufficiently exhaustive for this method to work. Otherwise, the algorithm could unjustifiably conclude that an answer is not being followed by the answer while this longer answer exists but is not included in the set $A$.
Installation
To install the package, use the following command:
pip install divergent-beamsearch
Usage
Here's a brief example of how to use divergent-beamsearch:
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from multi_choices_parser import MultiChoicesParser
from divergent_beamsearch import divergent_beamsearch
# Load model and tokenizer
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
# Define input prompt
prompt = "The capital of France is"
input_ids = tokenizer.encode(prompt, return_tensors="pt")
# Define beam search parameters
beam_size = 5
max_length = 10
pad_token_id = tokenizer.eos_token_id
# Define possible answers
possible_answers = [' Paris', ' Paris Hilton']
tokenized_answers = tokenizer(possible_answers).input_ids
multi_choices_parser = MultiChoicesParser([tokenized_answers])
# Perform beam search
scores, solutions = divergent_beamsearch(
input_ids=input_ids,
model=model,
beam_size=beam_size,
max_length=max_length,
multi_choices_parser=multi_choices_parser,
pad_token_id=pad_token_id,
num_solutions=2
)
# Decode solutions
decoded_solutions = [tokenizer.decode(solution, skip_special_tokens=True) for solution in solutions]
print("Scores:", scores)
print("Solutions:", decoded_solutions)
License
This project is licensed under the MIT License. See the LICENSE file for details.
Metadata
Release files for divergent-beamsearch 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| divergent_beamsearch-0.2.3.tar.gz | 38.3 kB | Without provenance | Details for divergent_beamsearch-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| divergent_beamsearch-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for divergent_beamsearch-0.2.3-py3-none-any.whl |
Total release size: 44.4 kB
Release files / divergent_beamsearch-0.2.3.tar.gz
| Download URL | divergent_beamsearch-0.2.3.tar.gz |
|---|---|
| Size | 38.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97e99a5297b7867e76017452db4cfadf426e01be39ed4a038d36c491caee333a
|
|
BLAKE2b-256 checksum How to use checksums |
38cf3fc94842145aeb627e70b7570fe42fdf0babd4fac7ed6c11fcfef4ee9cf8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.10
|
Release files / divergent_beamsearch-0.2.3-py3-none-any.whl
| Download URL | divergent_beamsearch-0.2.3-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d979c1c46cae828d07bc877ba57d69a46532282f0a43602dbb61fa7e6f63fbfb
|
|
BLAKE2b-256 checksum How to use checksums |
3ec62f84c7db76070713a100556dda41257b98269e4ab6c68cc89377bcb4e7c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.10
|
Security for divergent-beamsearch 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.