Diverse and extensible generation decoding libraries for transformers.
Project description
Decoders for 🤗 transformers
This package provides a convenient interface for extensible and customizable generation strategies -aka decoders- in 🤗 transformers.
It also provides extra implementations out of the box, like the Stochastic Beam Search decoder.
Installation
pip install decoders
Usage
Simple use of the new interface:
from decoders import inject_supervitamined_decoders
from transformers import T5ForConditionalGeneration
model = T5ForConditionalGeneration.from_pretrained('t5-small')
inject_supervitamined_decoders(model)
model.generate(...)
Decoders
Stochastic Beam Search
This decoder is a stochastic version of the Beam Search decoder. It is a HF implementation of the paper Stochastic Beam Search.
It can be used as follows:
from decoders import StochasticBeamSearchDecoder, inject_supervitamined_decoders
from transformers import T5ForConditionalGeneration
model = T5ForConditionalGeneration.from_pretrained('t5-small')
inject_supervitamined_decoders(model)
decoder = StochasticBeamSearchDecoder()
outputs = model.generate(input_ids, generation_strategy=decoder,
num_beams=4, num_return_sequences=4, # sample without repl. = return all beams
length_penalty=0.0, # for correct probabilities, disable length penalty
return_dict_in_generate=True, output_scores=True, early_stopping=True,
# early stopping because without length penalty, we can discard worse sequences
# return_dict_in_generate and output_scores are required for sbs for now,
# as scores keep the past generated gumbel noise, which is used by the logits processor
)
Complete example here.
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
decoders-0.0.4.tar.gz
(81.7 kB
view details)
Built Distribution
decoders-0.0.4-py3-none-any.whl
(104.1 kB
view details)
File details
Details for the file decoders-0.0.4.tar.gz
.
File metadata
- Download URL: decoders-0.0.4.tar.gz
- Upload date:
- Size: 81.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae73220165711103986f35d68c4bcd78c2dda56859717eea989795ad1ee9e343 |
|
MD5 | f899b94ab8d79eb683f867443f772e99 |
|
BLAKE2b-256 | 34630aaef212763dd934cc718cf57bdc0e4366b4b7beecc4f3fc36afa440c535 |
Provenance
File details
Details for the file decoders-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: decoders-0.0.4-py3-none-any.whl
- Upload date:
- Size: 104.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 879ce32acb5eafe3e39c1a55a04a5abb709a8f430aa9c2eea68d9aa42e3a73a4 |
|
MD5 | 78f6dd3808c27ce29b615c264fb35e18 |
|
BLAKE2b-256 | 5c145d99ef977e4a05aa89afaaadd265b004c9b17e77487d9c46154a2eab23f3 |