How to use   GPT2LMHeadModel  for conditional generation
Hi
could you please provide one single example on how to use   GPT2LMHeadModel  for conditional generation?
thanks
Rabeeh
Hi Rabeeh,

Please take a look at the [run_generation.py](https://github.com/huggingface/pytorch-transformers/blob/master/examples/run_generation.py) example which shows how to do conditional generation with the library's auto-regressive models (GPT/GPT-2/Transformer-XL/XLNet).<|||||>What's cracking Rabeeh, 

look, this code makes the trick for GPT2LMHeadModel.
But, as torch.argmax() is used to derive the next word; there is a lot of repetition.

`
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, help='Initial text for GPT2 model', required=True)
parser.add_argument('--length', type=int, help='Amount of new words added to input', required=True, default=20)
args = parser.parse_args()

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

generated = tokenizer.encode(args.input)
context = torch.tensor([generated])
past = None

for i in range(args.length):
>>>#print("{}=>>{}".format(i,tokenizer.decode(generated)))
>>>output, past = model(context, past=past)
>>>token = torch.argmax(output[0, :])
>>>generated += [token.tolist()]
>>>context = token.unsqueeze(0)

sequence = tokenizer.decode(generated)

print("Final sequence =>>{}".format(sequence))
`

As LysandreJik pointed out, is better to clone the hugginface transformer repo in Git, and go to the examples ---they do it great.<|||||>Hi
Thank you very much, very helpful for me.

On Wed, Jan 29, 2020 at 3:06 PM SaveTheBees-n-Seeds <
notifications@github.com> wrote:

> What's cracking Rabeeh,
>
> look, this code makes the trick for GPT2LMHeadModel.
> But, as torch.argmax() is used to derive the next word; there is a lot of
> repetition.
>
> `
> from transformers import GPT2LMHeadModel, GPT2Tokenizer
> import torch
> import argparse
> parser = argparse.ArgumentParser()
> parser.add_argument('--input', type=str, help='Initial text for GPT2
> model', required=True)
> parser.add_argument('--length', type=int, help='Amount of new words added
> to input', required=True, default=20)
> args = parser.parse_args()
>
> tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
> model = GPT2LMHeadModel.from_pretrained('gpt2')
>
> generated = tokenizer.encode(args.input)
> context = torch.tensor([generated])
> past = None
>
> for i in range(args.length):
> #print("{}=>>{}".format(i,tokenizer.decode(generated)))
> output, past = model(context, past=past)
> token = torch.argmax(output[0, :])
> generated += [token.tolist()]
> context = token.unsqueeze(0)
>
> sequence = tokenizer.decode(generated)
>
> print("Final sequence =>>{}".format(sequence))
> `
>
> As LysandreJik pointed out, is better to clone the hugginface transformer
> repo in Git, and go to the examples ---they do it great.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/huggingface/transformers/issues/970?email_source=notifications&email_token=ABP4ZCF2JQTV7W32PHYGAE3RAGEOLA5CNFSM4IJM4LMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKHJRGA#issuecomment-579770520>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ABP4ZCE4ZATSJAEF35A7B73RAGEOLANCNFSM4IJM4LMA>
> .
>
