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

how to properly load a model and get a prediction for a specific text #71

Closed
MuhammedTech opened this issue Aug 5, 2021 · 2 comments
Closed

Comments

@MuhammedTech
Copy link

MuhammedTech commented Aug 5, 2021

I completed the training and saved models like the following:
model.save_pretrained("/content/drive/MyDrive/gpt_sentiment/model_rugpt3-trainer", push_to_hub=False) tokenizer.save_pretrained("/content/drive/MyDrive/gpt_sentiment/model_rugpt3-trainer", push_to_hub=False)

then I am loading the model:

import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer

device = torch.device('cuda')
model_name_or_path = "/content/drive/MyDrive/gpt_sentiment/model_rugpt3-trainer"
tokenizer = GPT2Tokenizer.from_pretrained(model_name_or_path,local_files_only=True)
model = GPT2LMHeadModel.from_pretrained(model_name_or_path, local_files_only=True)
model = model.to(device)

text = "Александр Сергеевич Пушкин родился в "
#input_ids = tokenizer.encode(text, return_tensors="pt").cuda()
input_ids = tokenizer(text, return_tensors="pt")['input_ids'].to(device)
out = model.generate(**input_ids,
                     max_length=1024, 
                     do_sample=True,
                     no_repeat_ngram_size=20,
                     pad_token_id = 50258)
generated = decode(out[0])
print(generated)

here i am getting:

TypeError                                 Traceback (most recent call last)
<ipython-input-58-c57a5d1ecfe7> in <module>()
     17                      do_sample=True,
     18                      no_repeat_ngram_size=20,
---> 19                      pad_token_id = 50258)
     20 generated = decode(out[0])
     21 print(generated)

TypeError: generate() argument after ** must be a mapping, not Tensor

how can i properly load it and get the prediction for my text

@Artyrm
Copy link

Artyrm commented Aug 6, 2021

That looks strange to me:

input_ids = tokenizer(text, return_tensors="pt")['input_ids'].to(device)
out = model.generate(**input_ids,

Try maybe:

input_ids = tokenizer(text, return_tensors="pt").to(device)
out = model.generate(input_ids

@king-menin
Copy link
Collaborator

solved by #71 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants