基于卷积神经网络对文本数据集进行二分类,文本为两列分别为lebal和review,lebal为标签列,reveiw为内容列,对标签列进行读取,标签分别为0和1共两类,随机将两类别的各80%作为训练集,20%作为训练集,在训练时首先对reveiw列文本转换为数字序列,选择最长的句子作为序列定长,其他长度进行按0填充,并对其他建立一个传统卷积神经网络,卷积核大小为3,池化层为平均池化,训练轮数为50,输出训练集和测试集每一轮的准确率和损失率并作一个折线图,基于PYtorch建立模型,给出详细的代码
时间: 2023-11-28 08:04:39 浏览: 237
cnn-text-classification:在Yelp,IMDB和句子极性数据集v1.0上使用卷积神经网络进行文本分类
以下是基于PyTorch建立卷积神经网络进行文本二分类的详细代码:
```
import torch
import torch.nn as nn
import torch.optim as optim
from torchtext.data import Field, TabularDataset, BucketIterator
# 设置随机种子
SEED = 1234
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
# 定义Field
TEXT = Field(tokenize='spacy', batch_first=True, include_lengths=True)
LABEL = Field(sequential=False, is_target=True)
# 构建TabularDataset
fields = {'label': ('label', LABEL), 'review': ('text', TEXT)}
train_data, test_data = TabularDataset.splits(
path='.', train='train.csv', test='test.csv', format='csv', fields=fields)
# 构建词汇表
TEXT.build_vocab(train_data, max_size=25000, vectors='glove.6B.100d')
LABEL.build_vocab(train_data)
# 定义模型
class CNN(nn.Module):
def __init__(self, vocab_size, embedding_dim, n_filters, filter_sizes, output_dim, dropout):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.convs = nn.ModuleList([nn.Conv2d(in_channels=1, out_channels=n_filters, kernel_size=(fs, embedding_dim)) for fs in filter_sizes])
self.fc = nn.Linear(len(filter_sizes) * n_filters, output_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, text, text_lengths):
embedded = self.embedding(text)
embedded = embedded.unsqueeze(1)
conved = [nn.functional.avg_pool2d(nn.functional.relu(conv(embedded)), (text.shape[1], 1)).squeeze(3) for conv in self.convs]
cat = self.dropout(torch.cat(conved, dim=1))
return self.fc(cat)
# 定义超参数
BATCH_SIZE = 64
EMBEDDING_DIM = 100
N_FILTERS = 100
FILTER_SIZES = [3]
OUTPUT_DIM = 1
DROPOUT = 0.5
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 构建迭代器
train_iterator, test_iterator = BucketIterator.splits(
(train_data, test_data), batch_size=BATCH_SIZE, device=device, sort_key=lambda x: len(x.text), sort_within_batch=True)
# 初始化模型、优化器和损失函数
model = CNN(len(TEXT.vocab), EMBEDDING_DIM, N_FILTERS, FILTER_SIZES, OUTPUT_DIM, DROPOUT).to(device)
optimizer = optim.Adam(model.parameters())
criterion = nn.BCEWithLogitsLoss().to(device)
# 训练模型
def train(model, iterator, optimizer, criterion):
epoch_loss = 0
epoch_acc = 0
model.train()
for batch in iterator:
optimizer.zero_grad()
text, text_lengths = batch.text
predictions = model(text, text_lengths).squeeze(1)
loss = criterion(predictions, batch.label.float())
acc = ((predictions > 0.5) == batch.label.byte()).sum().item() / len(batch.label)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc
return epoch_loss / len(iterator), epoch_acc / len(iterator)
# 测试模型
def evaluate(model, iterator, criterion):
epoch_loss = 0
epoch_acc = 0
model.eval()
with torch.no_grad():
for batch in iterator:
text, text_lengths = batch.text
predictions = model(text, text_lengths).squeeze(1)
loss = criterion(predictions, batch.label.float())
acc = ((predictions > 0.5) == batch.label.byte()).sum().item() / len(batch.label)
epoch_loss += loss.item()
epoch_acc += acc
return epoch_loss / len(iterator), epoch_acc / len(iterator)
N_EPOCHS = 50
best_valid_loss = float('inf')
train_loss_list, train_acc_list, valid_loss_list, valid_acc_list = [], [], [], []
for epoch in range(N_EPOCHS):
train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
valid_loss, valid_acc = evaluate(model, test_iterator, criterion)
train_loss_list.append(train_loss)
train_acc_list.append(train_acc)
valid_loss_list.append(valid_loss)
valid_acc_list.append(valid_acc)
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict(), 'cnn-model.pt')
print(f'Epoch: {epoch+1:02}, Train Loss: {train_loss:.3f}, Train Acc: {train_acc*100:.2f}%, Val. Loss: {valid_loss:.3f}, Val. Acc: {valid_acc*100:.2f}%')
# 绘制训练集和测试集每一轮的准确率和损失率折线图
import matplotlib.pyplot as plt
plt.plot(train_loss_list, label='Training Loss')
plt.plot(valid_loss_list, label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(train_acc_list, label='Training Accuracy')
plt.plot(valid_acc_list, label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
```
在这个代码中,我们首先定义了 `Field`,并使用 `TabularDataset` 加载数据集。然后,我们使用 `Field` 构建了词汇表,并使用 `BucketIterator` 构建了迭代器。接着,我们定义了卷积神经网络模型,并使用 `Adam` 优化器和 `BCEWithLogitsLoss` 损失函数进行训练。我们使用了 `train` 和 `evaluate` 两个函数分别进行训练和测试。最后,我们使用 `matplotlib` 绘制了训练集和测试集每一轮的准确率和损失率折线图。
阅读全文