Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPT2 generating repetitive text #666

Closed
DEBADRIBASAK opened this issue Jun 8, 2019 · 7 comments
Closed

GPT2 generating repetitive text #666

DEBADRIBASAK opened this issue Jun 8, 2019 · 7 comments
Labels

Comments

@DEBADRIBASAK
Copy link

I was trying to use the pretrained GPT2LMHeadModel for generating texts by feeding some initial English words. But it is always generating repetitive texts.
Input: All
Output: All All the same, the same, the same, the same, the same, the same, the same, the same, the same, the same, the same, the same,

Here is my code:
`import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from pytorch_pretrained_BERT.pytorch_pretrained_bert.modeling_gpt2 import GPT2LMHeadModel
from pytorch_pretrained_BERT.pytorch_pretrained_bert.tokenization_gpt2 import GPT2Tokenizer
from pytorch_pretrained_BERT.pytorch_pretrained_bert.optimization_openai import OpenAIAdam
from tqdm import tqdm
import torch.optim as optim
import random
import time
import os
import sys
import random
import argparse
from pathlib import Path
from torch.utils.data import Dataset,TensorDataset,DataLoader,SequentialSampler,RandomSampler

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained("gpt2")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
df = pd.read_csv("gpt_test.csv",sep="\t")
df = df.values

#context = tokenizer.encode(context)

model.to(device)
source = []
generated = []

for l in tqdm(range(len(df))):
source.append(str(df[l,-2]))
context = tokenizer.encode(str(df[l,-2]))
past = None
for i in range(40):
input_ids = torch.tensor([context])
input_ids = input_ids.to(device)
pred,_ = model(input_ids=input_ids)
predictions = torch.argmax(pred[0,-1,:]).item()
context.append(predictions)
if(predictions==2):
break
generated_text = tokenizer.decode(context)
generated.append(generated_text)

df1 = pd.DataFrame({'Source': source,'Generated': generated})
df1.to_csv("./result_with_gpt.csv",sep="\t")`
Can someone point out the mistake? I will be highly grateful if the response is fast.

@thomwolf
Copy link
Member

Have you tried the provided GPT-2 generation example? It's here: https://github.com/huggingface/pytorch-pretrained-BERT/blob/master/examples/run_gpt2.py

@nkami
Copy link

nkami commented Jun 23, 2019

Have you tried the provided GPT-2 generation example? It's here: https://github.com/huggingface/pytorch-pretrained-BERT/blob/master/examples/run_gpt2.py

Hey, I encountered the same issue. I tried using the example you provided but it tends to produce repetitive text much more often than earlier versions of the library as well (from around 1-2 months back). Thank you very much for all the work!

@DEBADRIBASAK
Copy link
Author

DEBADRIBASAK commented Jun 25, 2019

Have you tried the provided GPT-2 generation example? It's here: https://github.com/huggingface/pytorch-pretrained-BERT/blob/master/examples/run_gpt2.py

I tried the example. It is working properly. I think with torch.argmax there is chance of repetitive text generation. If we sample using torch.multinomial, there is always some variation.

@HansBambel
Copy link
Contributor

HansBambel commented Jul 10, 2019

torch.argmax is basically top_k with 1 which is very bad for creating "human like" sentences. A better way to sample is using Nucleus Sampling https://arxiv.org/abs/1904.09751.
Not sure whether this is implemented in PyTorch yet.
EDIT: I have found this code from @thomwolf https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317 that implements Nucleus sampling.

@stale
Copy link

stale bot commented Sep 8, 2019

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.

@stale stale bot added the wontfix label Sep 8, 2019
@stale stale bot closed this as completed Sep 15, 2019
@drizzt00s
Copy link

generating repetitive text when using GPU, but this not happens when using CPU, anyone knows how to solve this weird issue?

@pallavimitra
Copy link

when using GPU, but this not happens when using CPU, anyone knows how to solve this weird issue?

I am facing the same issue. Can someone lead in this problem?

alexisflive pushed a commit to alexisflive/transformers that referenced this issue Jun 9, 2024
* Bump vite from 4.5.2 to 4.5.3 in /examples/tokenizer-playground (huggingface#693)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 4.5.2 to 4.5.3.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v4.5.3/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v4.5.3/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump express from 4.18.2 to 4.19.2 (huggingface#666)

Bumps [express](https://github.com/expressjs/express) from 4.18.2 to 4.19.2.
- [Release notes](https://github.com/expressjs/express/releases)
- [Changelog](https://github.com/expressjs/express/blob/master/History.md)
- [Commits](expressjs/express@4.18.2...4.19.2)

---
updated-dependencies:
- dependency-name: express
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 5.0.12 to 5.0.13 in /examples/remove-background-client (huggingface#694)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.0.12 to 5.0.13.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.0.13/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.0.13/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 5.0.12 to 5.0.13 in /examples/zero-shot-classification (huggingface#695)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.0.12 to 5.0.13.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.0.13/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.0.13/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 5.0.12 to 5.0.13 in /examples/depth-anything-client (huggingface#696)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.0.12 to 5.0.13.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.0.13/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.0.13/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 5.0.12 to 5.0.13 in /examples/semantic-audio-search (huggingface#697)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.0.12 to 5.0.13.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.0.13/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.0.13/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 4.5.2 to 4.5.3 in /examples/code-completion (huggingface#698)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 4.5.2 to 4.5.3.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v4.5.3/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v4.5.3/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 5.1.4 to 5.1.7 in /examples/video-object-detection (huggingface#699)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.1.4 to 5.1.7.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.1.7/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.1.7/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 5.1.1 to 5.1.7 in /examples/cross-encoder (huggingface#700)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.1.1 to 5.1.7.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.1.7/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.1.7/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vite from 5.1.1 to 5.1.7 in /examples/adaptive-retrieval (huggingface#701)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.1.1 to 5.1.7.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.1.7/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.1.7/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants