How to generate data using beam search from a custom gpt2 model?
# ❓ Questions & Help

<!-- The GitHub issue tracker is primarly intended for bugs, feature requests,
     new models and benchmarks, and migration questions. For all other questions,
     we direct you to the Hugging Face forum: https://discuss.huggingface.co/ .
     You can also try Stack Overflow (SO) where a whole community of PyTorch and
     Tensorflow enthusiast can help you out. In this case, make sure to tag your
     question with the right deep learning framework as well as the
     huggingface-transformers tag: 
     https://stackoverflow.com/questions/tagged/huggingface-transformers 
     -->

## 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
Here's an example using beam search with GPT-2:

```py
from transformers import GPT2LMHeadModel, GPT2Tokenizer

tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")

input_ids = tokenizer("The day starts with", return_tensors='pt')['input_ids']
print(tokenizer.decode(model.generate(input_ids, num_beams=3)[0]))
```
Result:
```
The day starts with a long walk to the top of the hill.

The first thing you
```<|||||>[Here's the doc for the `generate` method](https://huggingface.co/transformers/main_classes/model.html#transformers.generation_utils.GenerationMixin.generate)<|||||>Thanks @LysandreJik for the response but I have a custom model, I want to know how can I generate using my model<|||||>@LysandreJik - How can I generate sentences using my custom model?<|||||>I would recommend you check the[ source code for the `generate` method](https://github.com/huggingface/transformers/blob/master/src/transformers/generation_utils.py#L111) 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.<|||||>This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
<|||||>@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.   