GPT2 Model for title generation

60 views Asked by At

I'm getting this error after running few iterations of gpt2 model. tensor I'm trying to reshape has 0 elements, which is causing the error. Seems like tensor is not being initialized properly or the dimensions of the tensor are not being set correctly.

runtimeerror: cannot reshape tensor of 0 elements into shape [-1, 0] because the 
unspecified dimension size -1 can be any value and is ambiguous in gpt2 model.

Here is my code. I want to know what I'm doing wrong here? How this can be fixed?

gpt2 = GPT2LMHeadModel.from_pretrained("gpt2-large")

tokenizer.pad_token = tokenizer.eos_token
prompt = tokenizer.encode("machine learning", max_length=30, padding="max_length", truncation=True, return_tensors="pt")
attention_mask = torch.ones(prompt.shape, dtype=torch.long, device=prompt.device)
output = gpt2.generate(prompt, attention_mask=attention_mask, do_sample=True, max_length=100, top_k=10, temperature=0.8)
tokenizer.decode(output[0], skip_special_tokens=True)

class TitleDataset(Dataset):
    def __init__(self,titles):
        self.tokenizer = tokenizer
        self.titles = titles
    
    def __len__(self):
        return len(self.titles)
    
    def __getitem__(self,index):
        title = self.titles[index]
        title_token = self.tokenizer.encode(title, max_length=30, padding="max_length", truncation=True, return_tensors="pt")
        return title_token

df1 = pd.DataFrame({'content': content_corpus, 'subject': subject_corpus})
df1 = df1.loc[(df1 != 0).all(axis=1)]

dset = TitleDataset(df1['subject'].values)
title = next(iter(DataLoader(dset , batch_size = 1,shuffle = True)))

gpt2_model = gpt2

from pytorch_lightning import Trainer
#model = TitleGenerator()
#print(model)
module = TitleDataModule()
trainer = Trainer(max_epochs = 8,accelerator='cpu')
trainer.fit(model,module) 
0

There are 0 answers