-
Notifications
You must be signed in to change notification settings - Fork 32
/
Neptune_Transformers.py
64 lines (51 loc) · 1.62 KB
/
Neptune_Transformers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import neptune
from datasets import load_dataset
from evaluate import load
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
Trainer,
TrainingArguments,
)
from transformers.integrations import NeptuneCallback
# Set the environment variables `NEPTUNE_API_TOKEN` and `NEPTUNE_PROJECT`.
run = neptune.init_run()
task = "cola"
model_checkpoint = "prajjwal1/bert-tiny"
batch_size = 16
dataset = load_dataset("glue", task)
metric = load("glue", task)
num_labels = 2
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, use_fast=True)
def preprocess_function(examples):
return tokenizer(examples["sentence"], truncation=True)
encoded_dataset = dataset.map(preprocess_function, batched=True)
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=num_labels)
model_name = model_checkpoint.split("/")[-1]
args = TrainingArguments(
f"{model_name}-finetuned-{task}",
eval_strategy="epoch",
save_strategy="epoch",
save_safetensors=False,
learning_rate=2e-6,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
num_train_epochs=2,
weight_decay=0.005,
load_best_model_at_end=True,
report_to="none",
)
validation_key = "validation"
neptune_callback = NeptuneCallback(
run=run,
log_checkpoints=None, # Update to "last" or "best" if you want to log model checkpoints to Neptune
)
trainer = Trainer(
model,
args,
train_dataset=encoded_dataset["train"],
eval_dataset=encoded_dataset[validation_key],
callbacks=[neptune_callback],
tokenizer=tokenizer,
)
trainer.train()