Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

divergent_beamsearch-0.1.8.tar.gz (37.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

divergent_beamsearch-0.1.8-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file divergent_beamsearch-0.1.8.tar.gz.

File metadata

File hashes

Hashes for divergent_beamsearch-0.1.8.tar.gz
Algorithm Hash digest
SHA256 5ecd409bd3c076abe638ec625937c780f992b6425c8c2f715497340000256f0c
MD5 d282f20200aacc056f8eb3af8d1af665
BLAKE2b-256 d0e6a0a08eeecfd5b6255a39e40e1e95ad8368b29c9bcd16dde8e77d096179ef

See more details on using hashes here.

File details

Details for the file divergent_beamsearch-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for divergent_beamsearch-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f28e431d22bd9ccbaf2af1ffdb2170a3a1502ccf2b82e8b195a06f9141a5cf30
MD5 7505c61c9f814725b1b8674855d9118b
BLAKE2b-256 5b532908d8224ed0f1ad8e22790ebad6a2d33d876e701224269a4fb7b6ed6f40

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page