-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_text.py
More file actions
30 lines (24 loc) · 962 Bytes
/
Copy pathtrain_text.py
File metadata and controls
30 lines (24 loc) · 962 Bytes
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
from transformers import BertTokenizer, BertForSequenceClassification
from torch.utils.data import DataLoader
import torch
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
model = BertForSequenceClassification.from_pretrained(
"bert-base-uncased", num_labels=2
)
texts = ["This is human written text", "This essay was generated by AI"]
labels = [0, 1]
encodings = tokenizer(texts, truncation=True, padding=True, return_tensors="pt")
dataset = torch.utils.data.TensorDataset(
encodings["input_ids"], torch.tensor(labels)
)
loader = DataLoader(dataset, batch_size=2, shuffle=True)
optimizer = torch.optim.AdamW(model.parameters(), lr=2e-5)
model.train()
for epoch in range(3):
for batch in loader:
optimizer.zero_grad()
outputs = model(batch[0], labels=batch[1])
loss = outputs.loss
loss.backward()
optimizer.step()
print("Text AI Detector trained successfully")