Answers.Body: <p>Example with PyTorch</p>
  <pre><code>from transformers import GPT2Tokenizer, GPT2Model
  
  tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
  model = GPT2Model.from_pretrained('gpt2')
  text = &quot;governor John Gary Evans office 1894&quot;
  encoded_input = tokenizer(text, return_tensors='pt')
  output = model(**encoded_input)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-26T20:58:11.973Z
Answers.Text: Could you please elaborate this example? Should output be a string, a suggested sentence?
Answers.UserDisplayName: Vladimir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-27T07:51:50.830Z
Answers.Text: I did not try on my PC. I see the same example on Hugging Faces website, then I revised it in your context.
Answers.UserDisplayName: Vy Do
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-05T10:50:24.600Z
Answers.Text: I've tried it and it does not generate meaningful output :/
Answers.UserDisplayName: Vladimir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-25T06:12:17.590Z
Answers.LastActivityDate: 2023-05-25T06:12:17.590Z
Answers.UserDisplayName: Vy Do
Body: <p>I have different sets of words as inputs, e.g.,</p>
  <pre><code>{governor, John Gary Evans, office, 1894}
  </code></pre>
  <p>or</p>
  <pre><code>{cheetah, 80km/h, mammal}
  </code></pre>
  <p>I would like to construct a grammatically correct sentence that contains a full set, or a subset of these tokens. So the outputs could be:</p>
  <pre><code>&quot;Governor John Gary Evans took office in 1894.&quot; 
  </code></pre>
  <p>and</p>
  <pre><code>&quot;Cheetahs can run at speeds of about 80km/s.&quot; 
  </code></pre>
  <p>Output should be one sentence only. How to use GPT2 or BERT for this task? Which of the two models is appropriate for this task? I understand one is unidirectional and the other is bidirectional in the way they generate each word of the output. However, i do not know in advance what the grammatically correct order of the input tokens should be, and the missing words might need to be inserted after, in between, or at the beginning of the output sentence.</p>
  
Tags: <bert><gpt-2>
Title: How to generate a sentence containing a specific set of tokens using GPT2 or BERT?
UserDisplayName: Vladimir
ThreadText: # How to generate a sentence containing a specific set of tokens using GPT2 or BERT?
  
  (Asked by: username_0 on 2023-05-24)
  
  I have different sets of words as inputs, e.g.,
  {governor, John Gary Evans, office, 1894}
  
  or
  {cheetah, 80km/h, mammal}
  
  I would like to construct a grammatically correct sentence that contains a full set, or a subset of these tokens. So the outputs could be:
  "Governor John Gary Evans took office in 1894." 
  
  and
  "Cheetahs can run at speeds of about 80km/s." 
  
  Output should be one sentence only. How to use GPT2 or BERT for this task? Which of the two models is appropriate for this task? I understand one is unidirectional and the other is bidirectional in the way they generate each word of the output. However, i do not know in advance what the grammatically correct order of the input tokens should be, and the missing words might need to be inserted after, in between, or at the beginning of the output sentence.
  
  **Tags:** bert, gpt-2
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-05-25. Score: -3
  
  Example with PyTorch
  from transformers import GPT2Tokenizer, GPT2Model
  
  tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
  model = GPT2Model.from_pretrained('gpt2')
  text = "governor John Gary Evans office 1894"
  encoded_input = tokenizer(text, return_tensors='pt')
  output = model(**encoded_input)
  
  > **Comments:**
  > **username_0**: Could you please elaborate this example? Should output be a string, a suggested sentence?  
  > **username_1**: I did not try on my PC. I see the same example on Hugging Faces website, then I revised it in your context.  
  > **username_0**: I've tried it and it does not generate meaningful output :/  
  
  ---
  
  