Open kaiisongit opened 4 years ago
Hello @kaiisongit
Did you manage to find a solution to this issue? I am facing the same problem.
It would be great if you could share it.
Regards, Paritosh
Hi, I didn't find a solution, but I managed to scramble together this code which does what I need - score is a function that when you pass it a sentence as a string, gives you the loss. Lower numbers are better.
import math
import torch
from pytorch_pretrained_bert import OpenAIGPTTokenizer, OpenAIGPTModel, OpenAIGPTLMHeadModel
model = OpenAIGPTLMHeadModel.from_pretrained('openai-gpt')
model.eval()
model.to('cuda')
tokenizer = OpenAIGPTTokenizer.from_pretrained('openai-gpt')
def score(sentence):
tokenize_input = tokenizer.tokenize(sentence)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenize_input)
tokens_tensor = torch.tensor([indexed_tokens])
tokens_tensor = tokens_tensor.to('cuda')
loss=model(tokens_tensor, lm_labels=tokens_tensor)
return math.exp(loss)
if you aren't using cuda, you can remove the two lines: model.to('cuda') and tokens_tensor = tokens_tensor.to('cuda')
Hello @kaiisongit,
Thanks for sharing your code.
However, I am working on the transformers-based model, and I started getting the same errors during deployment. Hence, I was looking for a solution.
Anyways, thanks for the prompt reply.
I'm getting a similar error:
ModuleNotFoundError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/transformers/file_utils.py in _get_module(self, module_name)
10 frames ModuleNotFoundError: No module named 'transformers.models.gpt2.modeling_gpt2'
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/transformers/file_utils.py in _get_module(self, module_name)
RuntimeError: Failed to import transformers.models.gpt2.modeling_gpt2 because of the following error (look up to see its traceback): No module named 'transformers.models.gpt2.modeling_gpt2'
Was a solution provided?
I have only just tried loading GPT2, havent tried to score a sentence yet. Here is the code:
However, when I run it, I get this error:
Thank you for your work! This seems to be exactly what I need, if only I could get it to work!