forked from eust-w/little_language_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleSentimentAnalysis.py
57 lines (42 loc) · 1.53 KB
/
simpleSentimentAnalysis.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
import os
from transformers import Trainer, TrainingArguments
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased')
train_texts = [
"I love machine learning.",
"Transformers are amazing!",
"I don't like the new update.",
"Natural language processing is fascinating."
]
train_labels = [1, 1, 0, 1] # 假设1代表正面情感,0代表负面情感
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels)
train_encodings = tokenizer(train_texts, truncation=True, padding=True)
train_dataset = CustomDataset(train_encodings, train_labels)
current_file_path = os.path.abspath(__file__)
current_file_name = os.path.basename(current_file_path)
training_args = TrainingArguments(
output_dir='./results'+current_file_name,
num_train_epochs=3,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
warmup_steps=500,
weight_decay=0.01,
logging_dir='./logs'+current_file_name,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
)
trainer.train()