Upload 1039_159_252.py
Browse files- 1039_159_252.py +29 -0
1039_159_252.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""1039.159.252
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/11PzoX_0IuFs7qA7O5sLYwHetx0Cbzmao
|
8 |
+
"""
|
9 |
+
|
10 |
+
import torch
|
11 |
+
|
12 |
+
!pip install torch
|
13 |
+
|
14 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
15 |
+
|
16 |
+
model = AutoModelForCausalLM.from_pretrained("gpt2")
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
18 |
+
|
19 |
+
inputs = tokenizer("Hugging Face is a startup based in New York City and Paris",
|
20 |
+
return_tensors="pt")
|
21 |
+
|
22 |
+
loss = model(input_ids=inputs["input_ids"],
|
23 |
+
labels=inputs["input_ids"]).loss
|
24 |
+
|
25 |
+
loss = model(input_ids=inputs["input_ids"],
|
26 |
+
labels=inputs["input_ids"]).loss
|
27 |
+
ppl = torch.exp(loss)
|
28 |
+
|
29 |
+
print(f"Perplexity: {ppl.item(): 2f}")
|