How to use   GPT2LMHeadModel  for conditional generation
Author: rabeehk
Pull request: no

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

Comments:
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>
> .
>


HI @SaveTheBees-n-Seeds ,
 I have a particular use case (which is somewhat related to this quest); 
 
 Context: instead of a text-to-condition, I have directly a  vector, which is an output from some other model (which is also learnable ). 
 In this context, how to do conditional fine-tuning of the GPT-2 model. From the GPT-2 model's perspective, we can think that the dataset to train is a **list of tuples of the form (condition_vec,text_sample)**. But note that condition_vec is an output from some other model that is also involved in the optimisation.

 One naive approach is to feed this vector directly as the context vector (Assume dimensions match). 
 Will it work? (I could test it soon)
 Or is there any better solution?