question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to generate data using beam search from a custom gpt2 model?

See original GitHub issue

❓ Questions & Help

Details

I have a custom model with classification and an LM head. `

    self.config = AutoConfig.from_pretrained("gpt2", num_labels=3)
    self.base_model = AutoModel.from_pretrained("gpt2", config=self.config)
    self.classifier = nn.Sequential(
        nn.Linear(self.config.hidden_size, self.config.num_labels),
    )

    self.lm_head = nn.Linear(self.base_model.config.n_embd, self.base_model.config.vocab_size, bias=False)`

I want to generate the sentences using this model (given the initial prefix) via beam search. How can I achieve that?

I know that LM with double head exists but it’s not fit for my usecase

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
LysandreJikcommented, Oct 5, 2020

I would recommend you check the source code for the generate method and see how the beam search is implemented. It is not trivial, however.

Maybe @sshleifer and @patrickvonplaten have better tips on how to best do this.

0reactions
talha1503commented, Jun 14, 2022

@nrjvarshney Did you find any suitable way to use generate function for a custom model? I am facing a similar issue with a model of mine, and would be really grateful if you could let me know how to solve the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to generate text: using different decoding methods for ...
We will give a tour of the currently most prominent decoding methods, mainly Greedy search, Beam search, Top-K sampling and Top-p sampling.
Read more >
Constrained Beam Search with Transformers
The new constrained beam search feature with the new force_words_ids argument to the model.generate() function allows us to do exactly that!
Read more >
Text generation with GPT-2 - Model Differently
In this post we will see how to generate text with models based on the Transformers architecture, and we will use this knowledge...
Read more >
Text Generation with HuggingFace - GPT2 | Kaggle
In this notebook, I will explore text generation using a GPT-2 model, ... To use Beam Search, we need only modify some parameters...
Read more >
Much More Quickly and Easily Deploy GPT-2 Using Aitextgen
For example, the user can provide a training dataset, which is used to train the model. Subsequently, the model can then be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found