# Text Generation, adding random words, weird linebreaks & symbols at random

**URL:** <https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330>\
**Category:** Beginners\
**Created:** [May 24, 2021, 7:42am UTC](https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330 "2021-05-24T07:42:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steelhard](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/steelhard/32/1671_2.png) [@steelhard](https://discuss.huggingface.co/u/steelhard)\
**Post date:** [May 24, 2021, 7:42am UTC](https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330/1 "2021-05-24T07:42:57Z")

</div>

Here’s the code I’m using to generate text.

```
tokenizer = GPT2Tokenizer.from_pretrained("gpt2-medium") 
model = GPT2LMHeadModel.from_pretrained("gpt2-medium" , pad_token_id = tokenizer.eos_token_id)

sentence= tokenizer.encode(kw, return_tensors="pt") output = model.generate(sentence, max_length = 500, no_repeat_ngram_size = 2, do_sample=False) text.append(tokenizer.decode(output[0], skip_special_tokens = True))

```

The issue is that the output often comes like this:

"What are the benefits of using collagen?

,

,  
,

,  
, \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_, \_\_\_\_\_\_\_\_\_\_  
The skin that has collagen has a higher level of hydrophilic (water-loving) proteins. `

or like this:

`Yes, collagen is a natural skin-repairing substance. It is also a powerful anti-inflammatory and antiaging agent. , and, are the most common types of collagen found in skin.`

As you can see, at the start it wrote “, and,” at random and it happens EXTREMELY often, nearly in every single text generation I did.

I don’t know if it’s related to my settings or not but I’d appreciate all the help you guys can give. I want to get my text to be as human-readable as possible & up to 100-500 words each input.

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](https://discuss.huggingface.co/u/BramVanroy)\
**Post date:** [May 24, 2021, 8:15am UTC](https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330/2 "2021-05-24T08:15:04Z")

</div>

It might help if you give more information about the model and tokenizer that you use.

---

<div class="post-metadata">

**Author:** ![steelhard](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/steelhard/32/1671_2.png) [@steelhard](https://discuss.huggingface.co/u/steelhard)\
**Post date:** [May 24, 2021, 9:03am UTC](https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330/3 "2021-05-24T09:03:24Z")

</div>

Sorry I forgot, edited the post

---

<div class="post-metadata">

**Author:** ![ktangri](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ktangri/32/1517_2.png) [@ktangri](https://discuss.huggingface.co/u/ktangri)\
**Post date:** [May 24, 2021, 3:56pm UTC](https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330/4 "2021-05-24T15:56:49Z")

</div>

Could you provide an example input you’re using to run this (e.g. what is kw)? For context, I ran this exact code with the input “What are the benefits of using collagen?”, and my output was reasonable. Given that you’re using greedy decoding (`do_sample=False`) I’d expect the same behavior from the model which is strange.

---

<div class="post-metadata">

**Author:** ![steelhard](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/steelhard/32/1671_2.png) [@steelhard](https://discuss.huggingface.co/u/steelhard)\
**Post date:** [May 24, 2021, 6:41pm UTC](https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330/5 "2021-05-24T18:41:46Z")

</div>

Nevermind stupid mistkae from my part, it’s fixed not related to transformers at all

---

<div class="post-metadata">

**Author:** ![ktangri](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ktangri/32/1517_2.png) [@ktangri](https://discuss.huggingface.co/u/ktangri)\
**Post date:** [May 24, 2021, 9:05pm UTC](https://discuss.huggingface.co/t/text-generation-adding-random-words-weird-linebreaks-symbols-at-random/6330/6 "2021-05-24T21:05:09Z")

</div>

Awesome! Happy to hear that it’s resolved.

_Need more comprehensive support specialized to your use-cases? 🤗 has you covered! Through our [Expert Acceleration Program](https://huggingface.co/support) your business can leverage our expertise to accelerate your NLP roadmap, from modeling to production._
