xingruispace
commited on
Commit
•
db9d27c
1
Parent(s):
5a07fa0
Create fine-turn
Browse files
fine-turn
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AdamW, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
|
4 |
+
# Same as before
|
5 |
+
checkpoint = "bert-base-uncased"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)
|
8 |
+
sequences = [
|
9 |
+
"I've been waiting for a HuggingFace course my whole life.",
|
10 |
+
"This course is amazing!",
|
11 |
+
]
|
12 |
+
batch = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt")
|
13 |
+
|
14 |
+
# This is new
|
15 |
+
batch["labels"] = torch.tensor([1, 1])
|
16 |
+
|
17 |
+
optimizer = AdamW(model.parameters())
|
18 |
+
loss = model(**batch).loss
|
19 |
+
loss.backward()
|
20 |
+
optimizer.step()
|