编写一个数据位置为D:/xinpython/BTM/data1.xlsx的Bert文本分类模型。并将最后输出的模型结果导出到D:/xinpython/BTM/topic1.xlsx
时间: 2023-12-23 18:02:40 浏览: 96
基于python面向工业用途使用BERT模型做文本分类项目实现
5星 · 资源好评率100%
以下是一个简单的BERT文本分类示例,包括读取数据、预处理、模型训练和保存结果的过程。
首先,需要安装必要的库和下载预训练的BERT模型:
```python
!pip install pandas xlrd openpyxl torch transformers
!wget -P ./model/ https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased.tar.gz
!tar -xzf ./model/bert-base-uncased.tar.gz -C ./model/
```
接着,读取数据并进行预处理,包括分词、编码和划分训练集和测试集:
```python
import pandas as pd
import torch
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
from transformers import BertTokenizer
# 读取数据
df = pd.read_excel('D:/xinpython/BTM/data1.xlsx')
sentences = df['text'].values
labels = df['label'].values
# 加载预训练的BERT模型和分词器
tokenizer = BertTokenizer.from_pretrained('model/bert-base-uncased', do_lower_case=True)
# 对文本进行分词和编码
input_ids = []
attention_masks = []
for sent in sentences:
encoded_dict = tokenizer.encode_plus(sent, add_special_tokens=True, max_length=64, pad_to_max_length=True, return_attention_mask=True, return_tensors='pt')
input_ids.append(encoded_dict['input_ids'])
attention_masks.append(encoded_dict['attention_mask'])
input_ids = torch.cat(input_ids, dim=0)
attention_masks = torch.cat(attention_masks, dim=0)
labels = torch.tensor(labels)
# 划分训练集和测试集
dataset = TensorDataset(input_ids, attention_masks, labels)
train_size = int(0.8 * len(dataset))
test_size = len(dataset) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(dataset, [train_size, test_size])
```
然后,定义BERT分类模型并进行训练:
```python
from transformers import BertForSequenceClassification, AdamW, get_linear_schedule_with_warmup
# 定义模型
model = BertForSequenceClassification.from_pretrained('model/bert-base-uncased', num_labels=2, output_attentions=False, output_hidden_states=False)
# 定义优化器和学习率调度器
optimizer = AdamW(model.parameters(), lr=2e-5, eps=1e-8)
epochs = 4
total_steps = len(train_dataset) * epochs
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=total_steps)
# 定义训练函数
def train(model, dataloader, optimizer, scheduler):
model.train()
for step, batch in enumerate(dataloader):
input_ids = batch[0].to(device)
attention_masks = batch[1].to(device)
labels = batch[2].to(device)
model.zero_grad()
outputs = model(input_ids, token_type_ids=None, attention_mask=attention_masks, labels=labels)
loss = outputs[0]
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
scheduler.step()
# 定义测试函数
def evaluate(model, dataloader):
model.eval()
total_accuracy = 0
total_loss = 0
nb_eval_steps = 0
for batch in dataloader:
input_ids = batch[0].to(device)
attention_masks = batch[1].to(device)
labels = batch[2].to(device)
with torch.no_grad():
outputs = model(input_ids, token_type_ids=None, attention_mask=attention_masks, labels=labels)
logits = outputs[1]
total_loss += outputs[0].item()
logits = logits.detach().cpu().numpy()
label_ids = labels.to('cpu').numpy()
total_accuracy += flat_accuracy(logits, label_ids)
nb_eval_steps += 1
avg_accuracy = total_accuracy / nb_eval_steps
avg_loss = total_loss / nb_eval_steps
return avg_accuracy, avg_loss
# 训练模型并保存
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
train_dataloader = DataLoader(train_dataset, sampler=RandomSampler(train_dataset), batch_size=32)
test_dataloader = DataLoader(test_dataset, sampler=SequentialSampler(test_dataset), batch_size=32)
for epoch in range(epochs):
train(model, train_dataloader, optimizer, scheduler)
accuracy, loss = evaluate(model, test_dataloader)
print('Epoch {} - Accuracy: {:.2f}% - Loss: {:.4f}'.format(epoch + 1, accuracy * 100, loss))
torch.save(model.state_dict(), 'bert_model.pt')
```
最后,加载保存的模型并对新数据进行分类,将结果导出到Excel文件:
```python
# 加载保存的模型
model = BertForSequenceClassification.from_pretrained('model/bert-base-uncased', num_labels=2, output_attentions=False, output_hidden_states=False)
model.load_state_dict(torch.load('bert_model.pt'))
model.to(device)
# 对新数据进行分类
df = pd.read_excel('D:/xinpython/BTM/topic1.xlsx')
sentences = df['text'].values
input_ids = []
attention_masks = []
for sent in sentences:
encoded_dict = tokenizer.encode_plus(sent, add_special_tokens=True, max_length=64, pad_to_max_length=True, return_attention_mask=True, return_tensors='pt')
input_ids.append(encoded_dict['input_ids'])
attention_masks.append(encoded_dict['attention_mask'])
input_ids = torch.cat(input_ids, dim=0)
attention_masks = torch.cat(attention_masks, dim=0)
dataset = TensorDataset(input_ids, attention_masks)
dataloader = DataLoader(dataset, sampler=SequentialSampler(dataset), batch_size=32)
model.eval()
predictions = []
for batch in dataloader:
input_ids = batch[0].to(device)
attention_masks = batch[1].to(device)
with torch.no_grad():
outputs = model(input_ids, token_type_ids=None, attention_mask=attention_masks)
logits = outputs[0]
logits = logits.detach().cpu().numpy()
predictions.extend(logits.argmax(axis=1))
# 将结果导出到Excel文件
df['label'] = predictions
df.to_excel('D:/xinpython/BTM/topic1.xlsx', index=False)
```
阅读全文