直接修改TRAIN()函数,令其运用bert分类器
时间: 2024-12-21 17:30:36 浏览: 15
如果你想直接在`TRAIN()`函数中应用BERT分类器,你需要按照以下步骤进行修改:
首先,导入所需的库:
```python
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizerFast, BertForSequenceClassification, AdamW, get_linear_schedule_with_warmup
```
然后,对数据进行预处理,并创建一个自定义的数据集类(假设数据已经分成了训练集和验证集):
```python
class SentimentDataset(Dataset):
def __init__(self, texts, labels, tokenizer, max_len):
self.texts = texts
self.labels = labels
self.tokenizer = tokenizer
self.max_len = max_len
def __len__(self):
return len(self.texts)
def __getitem__(self, idx):
text = str(self.texts[idx])
label = self.labels[idx]
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors="pt",
)
return {
'text': text,
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'label': torch.tensor(label, dtype=torch.long),
}
# 定义最大长度和BATCH_SIZE
max_len = 128
BATCH_SIZE = 16
train_dataset = SentimentDataset(train_texts, train_labels, tokenizer, max_len)
val_dataset = SentimentDataset(val_texts, val_labels, tokenizer, max_len)
```
接下来,设置BERT模型和优化器:
```python
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=num_classes).to(device)
optimizer = AdamW(model.parameters(), lr=2e-5)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=len(train_loader) * num_epochs)
```
训练循环部分将有所不同:
```python
def train_epoch(model, dataloader, optimizer, device, scheduler):
model.train()
losses = []
for batch in dataloader:
input_ids = batch["input_ids"].to(device)
attention_mask = batch["attention_mask"].to(device)
labels = batch["label"].to(device)
outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
loss = outputs.loss
losses.append(loss.item())
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) # 梯度裁剪
optimizer.step()
scheduler.step() # 更新学习率
optimizer.zero_grad()
return sum(losses) / len(losses)
# 创建DataLoader
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False)
# 训练模型
num_epochs = 3 # 示例值
for epoch in range(num_epochs):
train_loss = train_epoch(model, train_loader, optimizer, device, scheduler)
print(f"Epoch {epoch + 1}/{num_epochs}, Training Loss: {train_loss:.4f}")
```
阅读全文