A variant of the beam search algorithm that focuses on finding answers that maximize the probability of generating an answer before diverging into another subject.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file divergent_beamsearch-0.2.2.tar.gz.
File metadata
- Download URL: divergent_beamsearch-0.2.2.tar.gz
- Upload date:
- Size: 38.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d40e2020d95d66e6c2f79d376e5d70a6748421bbda28cdc007bb89bcbca01e94
|
|
| MD5 |
83559b5edd652a93cf4f759ea714e35b
|
|
| BLAKE2b-256 |
b2ec840da7777bf3f21cae6dda23720db577e4e47cd8116b319ba53aaa5af90e
|
File details
Details for the file divergent_beamsearch-0.2.2-py3-none-any.whl.
File metadata
- Download URL: divergent_beamsearch-0.2.2-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a05a752590fec0c316137b8a80eaac90d02bfa455ba43a8ec205c4b53991f9e1
|
|
| MD5 |
57fa89c185e5ccc6c77cf7f7a8f6bc76
|
|
| BLAKE2b-256 |
bcb04674c4bb12675be1b48358b39febec23a33493be3e31f7fefb5f9cc68937
|