NLP新手入门-第N1周:Pytorch文本分类入门

一、课题背景和开发环境

📌第N1周:Pytorch文本分类入门📌

  • Python 3.8.12
  • pytorch==1.8.1+cu111
  • torchtext==0.9.1
  • portalocker==2.7.0

二、环境安装

这是一个使用PyTorch实现的简单文本分类实战案例。在这个例子中,我们将使用AG News数据集进行文本分类。

AG News(AG’s News Topic Classification Dataset)是一个广泛用于文本分类任务的数据集,尤其是在新闻领域。该数据集是由AG’s Corpus of News Articles收集整理而来,包含了四个主要的类别:世界、体育、商业和科技。

首先,确保已经安装了 torchtextportalocker

torchtext 安装版本参考

PyTorch versiontorchtext versionSupported Python version
nightly buildmain>=3.8, <=3.11
1.14.00.15.0>=3.8, <=3.11
1.13.00.14.0>=3.7, <=3.10
1.12.00.13.0>=3.7, <=3.10
1.11.00.12.0>=3.6, <=3.9
1.10.00.11.0>=3.6, <=3.9
1.9.10.10.1>=3.6, <=3.9
1.90.10>=3.6, <=3.9
1.8.10.9.1>=3.6, <=3.9
1.80.9>=3.6, <=3.9
1.7.10.8.1>=3.6, <=3.9
1.70.8>=3.6, <=3.8
1.60.7>=3.6, <=3.8
1.50.6>=3.5, <=3.8
1.40.52.7, >=3.5, <=3.8
0.4 and below0.2.32.7, >=3.5, <=3.8

三、文本分类

1. 加载数据

import os
import sys
import PIL
from PIL import Image
import time
import copy
import random
import pathlib
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchtext.datasets import AG_NEWS
import torchvision
from torchinfo import summary
import torchsummary
import matplotlib.pyplot as plt
import numpy as np
import warnings


''' 下载或读取AG News数据集中的训练集与测试集 '''
def getDataset(root, dataset):
    if not os.path.exists(root) or not os.path.isdir(root):
        os.makedirs(root)
    if not os.path.exists(dataset) or not os.path.isdir(dataset):
        print('Downloading dataset...\n')
        # 下载AG News数据集 直接运行会报网络错误 无法下载  
        train_ds, test_ds = AG_NEWS(root=root, split=("train", "test"))
    else:
        print('Dataset already downloaded, reading...\n')
        # 读取本地AG News数据集 手动下载了train.csv和test.csv后可从本地加载数据
        train_ds, test_ds = AG_NEWS(root=dataset, split=("train", "test"))
    #print("Train:", next(train_ds), len(list(train_ds))+1)
    #print("Test :", next(test_ds), len(list(test_ds))+1)
    return train_ds, test_ds


''' 设置GPU '''
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using {} device\n".format(device))
''' 加载数据 '''
root = './data/'
data_dir = os.path.join(root, 'AG_NEWS.data')
train_ds, test_ds = getDataset(root, data_dir)
Using cuda device

Dataset already downloaded, reading...

Train: (3, "Wall St. Bears Claw Back Into the Black (Reuters) Reuters - Short-sellers, Wall Street's dwindling\\band of ultra-cynics, are seeing green again.") 120000
Test : (3, "Fears for T N pension after talks Unions representing workers at Turner   Newall say they are 'disappointed' after talks with stricken parent firm Federal Mogul.") 7600

2. 构建词典

''' 构建词典 '''
def buildDict(train_ds):
    tokenizer  = get_tokenizer('basic_english') # 返回分词器函数
    def yield_tokens(data_iter):
        for _, text in data_iter:
            yield tokenizer(text)
    vocab = build_vocab_from_iterator(yield_tokens(train_ds))
    text_pipeline  = lambda x: vocab.lookup_indices(tokenizer(x))
    label_pipeline = lambda x: int(x)
    #print(vocab.UNK, vocab._default_unk_index())# 打印默认索引,如果找不到单词,则会选择默认索引
    #print(vocab.lookup_indices(['here', 'is', 'an', 'example']))
    #print(text_pipeline('here is the an example'))
    #print(label_pipeline('10'))
    return vocab, text_pipeline, label_pipeline


# 构建词典
text_pipeline, label_pipeline = buildDict(train_ds)
120001lines [00:04, 27817.88lines/s]
<unk> 0
[471, 22, 31, 5177]
[471, 22, 3, 31, 5177]
10

3. 生成数据批次和迭代器

''' 加载数据,并设置batch_size '''
def loadData(train_ds, test_ds, batch_size=8, device='cpu'):
    # 构建词典
    vocab, text_pipeline, label_pipeline = buildDict(train_ds)
    # 生成数据批次和迭代器
    def collate_batch(batch):
        label_list, text_list, offsets = [], [], [0]
        for (_label, _text) in batch:
            # 标签列表
            label_list.append(label_pipeline(_label))
            # 文本列表
            processed_text = torch.tensor(text_pipeline(_text), dtype=torch.int64)
            text_list.append(processed_text)
            # 偏移量,即语句的总词汇量
            offsets.append(processed_text.size(0))
        label_list = torch.tensor(label_list, dtype=torch.int64)
        text_list  = torch.cat(text_list)
        offsets    = torch.tensor(offsets[:-1]).cumsum(dim=0) #返回维度dim中输入元素的累计和
        return label_list.to(device), text_list.to(device), offsets.to(device)
    # 从 train_ds 加载训练集
    train_dl = torch.utils.data.DataLoader(train_ds,
                                           batch_size=batch_size,
                                           shuffle=False,
                                           collate_fn=collate_batch,
                                           num_workers=0)
    # 从 test_ds 加载测试集
    test_dl  = torch.utils.data.DataLoader(test_ds,
                                           batch_size=batch_size,
                                           shuffle=False,
                                           collate_fn=collate_batch,
                                           num_workers=0)
    
    # 取一个批次查看数据格式
    #data = train_dl.__iter__()
    #print(type(data), data, '\n')
    return vocab, train_dl, test_dl


# 生成数据批次和迭代器
batch_size = 64
train_dl, test_dl = loadData(train_ds, test_ds, batch_size=batch_size, device=device)
120001lines [00:04, 27749.13lines/s]
<class 'torch.utils.data.dataloader._SingleProcessDataLoaderIter'> <torch.utils.data.dataloader._SingleProcessDataLoaderIter object at 0x00000266556204C0>

4. 定义模型

class TextClassificationModel(nn.Module):
    def __init__(self, vocab_size, embed_dim, num_class):
        super(TextClassificationModel, self).__init__()
        self.embedding = nn.EmbeddingBag(vocab_size, embed_dim, sparse=True)
        self.fc = nn.Linear(embed_dim, num_class)
        self.init_weights()

    def init_weights(self):
        initrange = 0.5
        self.embedding.weight.data.uniform_(-initrange, initrange)      # 将tensor用从均匀分布中抽样得到的值填充
        self.fc.weight.data.uniform_(-initrange, initrange)
        self.fc.bias.data.zero_()

    def forward(self, text, offsets):
        embedded = self.embedding(text, offsets)        # torch.Size([64, 64])
        output = self.fc(embedded)      # torch.Size([64, 4])
        return output

5. 定义实例

''' 定义实例 '''
train_iter = AG_NEWS(root='./data/AG_NEWS.data', split=("train"))
num_class  = len(set([label for (label, text) in train_iter]))
vocab_size = len(vocab)
em_size    = 64
model      = TextClassificationModel(vocab_size, em_size, num_class).to(device)
print('num_class', num_class)
print('vocab_size', vocab_size)
print(model)
num_class 4
vocab_size 95812
TextClassificationModel(
  (embedding): EmbeddingBag(95812, 64, mode=mean)
  (fc): Linear(in_features=64, out_features=4, bias=True)
)

6. 定义训练函数与评估函数

def train(dataloader):
    model.train()       # 训练模式
    total_acc, total_count = 0, 0
    log_interval = 500
    start_time = time.time()

    for idx, (label, text, offsets) in enumerate(dataloader):
        optimizer.zero_grad()
        predited_label = model(text, offsets)
        loss = criterion(predited_label, label)
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), 0.1)     # 规定了最大不能超过的max_norm
        optimizer.step()
        total_acc += (predited_label.argmax(1) == label).sum().item()
        total_count += label.size(0)
        if idx % log_interval == 0 and idx > 0:
            elapsed = time.time() - start_time
            print('| epoch {:3d} | {:5d}/{:5d} batches, accuracy {:8.3f}'.format(epoch, idx, len(dataloader), total_acc / total_count))
            total_acc, total_count = 0, 0
            start_time = time.time()
def evaluate(dataloader):
    model.eval()
    total_acc, total_count = 0, 0

    with torch.no_grad():
        for idx, (label, text, offsets) in enumerate(dataloader):
            predited_label = model(text, offsets)
            # loss = criterion(predited_label, label)
            total_acc += (predited_label.argmax(1) == label).sum().item()
            total_count += label.size(0)
    return total_acc / total_count

7. 拆分数据集并运行模型

if __name__ == '__main__':
    # 超参数(Hyperparameters)
    EPOCHS = 10  # epoch
    LR = 5  # learning rate
    BATCH_SIZE = 64  # batch size for training
   
    criterion = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=LR)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1.0, gamma=0.1)
    total_accu = None
    train_iter, test_iter = AG_NEWS(root=path)
    train_dataset = list(train_iter)
    test_dataset = list(test_iter)
    num_train = int(len(train_dataset) * 0.95)
    split_train_, split_valid_ = random_split(train_dataset, [num_train, len(train_dataset) - num_train])
   
    train_dataloader = DataLoader(split_train_, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_batch)      # shuffle表示随机打乱
    valid_dataloader = DataLoader(split_valid_, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_batch)
    test_dataloader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_batch)
   
    for epoch in range(1, EPOCHS + 1):
        epoch_start_time = time.time()
        train(train_dataloader)
        accu_val = evaluate(valid_dataloader)
        if total_accu is not None and total_accu > accu_val:
            scheduler.step()
        else:
            total_accu = accu_val
        print('-' * 59)
        print('| end of epoch {:3d} | time: {:5.2f}s | '
              'valid accuracy {:8.3f} '.format(epoch, time.time() - epoch_start_time, accu_val))
        print('-' * 59)
    
    torch.save(model.state_dict(), 'output\\model_TextClassification.pth')
| epoch   1 |   500/ 1782 batches, accuracy    0.687
| epoch   1 |  1000/ 1782 batches, accuracy    0.856
| epoch   1 |  1500/ 1782 batches, accuracy    0.875
-----------------------------------------------------------
| end of epoch   1 | time: 23.15s | valid accuracy    0.881
-----------------------------------------------------------
| epoch   2 |   500/ 1782 batches, accuracy    0.898
| epoch   2 |  1000/ 1782 batches, accuracy    0.898
| epoch   2 |  1500/ 1782 batches, accuracy    0.903
-----------------------------------------------------------
| end of epoch   2 | time: 16.20s | valid accuracy    0.897
-----------------------------------------------------------
| epoch   3 |   500/ 1782 batches, accuracy    0.917
| epoch   3 |  1000/ 1782 batches, accuracy    0.915
| epoch   3 |  1500/ 1782 batches, accuracy    0.914
-----------------------------------------------------------
| end of epoch   3 | time: 15.98s | valid accuracy    0.902
-----------------------------------------------------------
| epoch   4 |   500/ 1782 batches, accuracy    0.924
| epoch   4 |  1000/ 1782 batches, accuracy    0.924
| epoch   4 |  1500/ 1782 batches, accuracy    0.922
-----------------------------------------------------------
| end of epoch   4 | time: 16.63s | valid accuracy    0.901
-----------------------------------------------------------
| epoch   5 |   500/ 1782 batches, accuracy    0.937
| epoch   5 |  1000/ 1782 batches, accuracy    0.937
| epoch   5 |  1500/ 1782 batches, accuracy    0.938
-----------------------------------------------------------
| end of epoch   5 | time: 16.37s | valid accuracy    0.912
-----------------------------------------------------------
| epoch   6 |   500/ 1782 batches, accuracy    0.938
| epoch   6 |  1000/ 1782 batches, accuracy    0.939
| epoch   6 |  1500/ 1782 batches, accuracy    0.940
-----------------------------------------------------------
| end of epoch   6 | time: 16.17s | valid accuracy    0.912
-----------------------------------------------------------
| epoch   7 |   500/ 1782 batches, accuracy    0.940
| epoch   7 |  1000/ 1782 batches, accuracy    0.938
| epoch   7 |  1500/ 1782 batches, accuracy    0.943
-----------------------------------------------------------
| end of epoch   7 | time: 16.20s | valid accuracy    0.911
-----------------------------------------------------------
| epoch   8 |   500/ 1782 batches, accuracy    0.941
| epoch   8 |  1000/ 1782 batches, accuracy    0.940
| epoch   8 |  1500/ 1782 batches, accuracy    0.942
-----------------------------------------------------------
| end of epoch   8 | time: 16.46s | valid accuracy    0.911
-----------------------------------------------------------
| epoch   9 |   500/ 1782 batches, accuracy    0.941
| epoch   9 |  1000/ 1782 batches, accuracy    0.941
| epoch   9 |  1500/ 1782 batches, accuracy    0.943
-----------------------------------------------------------
| end of epoch   9 | time: 17.50s | valid accuracy    0.912
-----------------------------------------------------------
| epoch  10 |   500/ 1782 batches, accuracy    0.940
| epoch  10 |  1000/ 1782 batches, accuracy    0.942
| epoch  10 |  1500/ 1782 batches, accuracy    0.942
-----------------------------------------------------------
| end of epoch  10 | time: 16.12s | valid accuracy    0.912
-----------------------------------------------------------

8. 使用测试数据集评估模型

print('Checking the results of test dataset.')
test_acc, test_loss = evaluate(test_dataloader)
print('test accuracy {:8.3f}'.format(test_acc))
Checking the results of test dataset.
test accuracy    0.907
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值