第N5周:Pytorch文本分类入门

news/2024/11/1 11:33:23/
  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊

本周任务:

  • 了解文本分类的基本流程
  • 学习常用数据清洗方法
  • 学习如何使用jieba实现英文分词
  • 学习如何构建文本向量

前期准备

加载数据

import torch
import torch.nn as nn
import torchvision
from torchvision import transforms, datasets
import os,PIL,pathlib,warningswarnings.filterwarnings('ignore')device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)

加载AG News数据集

from torchtext.datasets import AG_NEWStrain_iter = AG_NEWS(split='train')

构建词典

from torchtext.data.utils import get_tokenizer
from torchtext.vocab import build_vocab_from_iteratortokenizer = 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_iter), specials=['<unk'])
vocab.set_default_index(vocab['<unk'])
vocab(['here','is','an','example'])
text_pipeline = lambda x: vocab(tokenizer(x))
label_pipeline = lambda x: int(x) - 1text_pipeline('here is the an example')
label_pipeline('10')

生成数据批次和迭代器

from torch.utils.data import DataLoaderdef 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)return label_list.to(device), text_list.to(device), offsets.to(device)datalodaer = DataLoader(train_iter, batch_size=8, shuffle=False, collate_fn=collate_batch)

准备模型

定义模型

from torch import nnclass 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=False)self.fc = nn.Linear(embed_dim, num_class)self.init_weights()def init_weights(self):initrange = 0.5self.embedding.weight.data.uniform_(-initrange,initrange)self.fc.weight.data.uniform_(-initrange,initrange)self.fc.bias.data.zero_()def forward(self,text,offsets):embedded = self.embedding(text,offsets)return self.fc(embedded)

定义实例

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)

定义训练函数与评估函数

import timedef train(dataloader):model.train()total_acc, train_loss, total_count = 0,0,0log_interval = 500start_time = time.time()for idx, (label,text,offsets) in enumerate(dataloader):predicted_label = model(text, offsets)optimizer.zero_grad()loss = criterion(predicted_label, label)loss.backward()optimizer.step()total_acc += (predicted_label.argmax(1) == label).sum().item()train_loss += loss.item()total_count += label.size(0)if idx % log_interval == 0 and idx > 0:elapsed = time.time() - start_timeprint('| epoch {:1d} | {:4d}/{:4d} batches''| train_acc {:4.3f} train_loss {:4.5f}'.format(epoch, idx, len(dataloader),total_acc/total_count, train_loss/total_count))total_acc, train_loss, total_count = 0,0,0start_time = time.time()def evaluate(dataloader):model.eval()total_acc,train_loss, total_count = 0,0,0with torch.no_grad():for idx, (label,text,offsets) in enumerate(dataloader):predicted_label = model(text,offsets)loss = criterion(predicted_label, label)total_acc += (predicted_label.argmax(1) == label).sum().item()train_loss += loss.item()total_count += label.size(0)return total_acc/total_count, train_loss/total_count

训练模型

拆分数据集并运行模型

from torch.utils.data.dataset import random_split
from torchtext.data.functional import to_map_style_datasetEPOCHS = 10
LR = 5
BATCH_SIZE = 64criterion = 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 = Nonetrain_iter, test_iter = AG_NEWS()
train_dataset = to_map_style_dataset(train_iter)
test_dataset = to_map_style_dataset(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)
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)val_acc, val_loss = evaluate(valid_dataloader)if total_accu is not None and total_accu > val_acc:scheduler.step()else:total_accu = val_accprint('-' * 69)print('|epoch {:1d} | time: {:4.2f}s |''valid_acc {:4.3f} valid_loss {:4.3f}'.format(epoch, time.time() - epoch_start_time, val_acc, val_loss))print('-' * 69)
| epoch 1 |  500/1782 batches| train_acc 0.914 train_loss 0.00397
| epoch 1 | 1000/1782 batches| train_acc 0.917 train_loss 0.00385
| epoch 1 | 1500/1782 batches| train_acc 0.913 train_loss 0.00402
---------------------------------------------------------------------
|epoch 1 | time: 9.01s |valid_acc 0.920 valid_loss 0.004
---------------------------------------------------------------------
| epoch 2 |  500/1782 batches| train_acc 0.924 train_loss 0.00356
| epoch 2 | 1000/1782 batches| train_acc 0.925 train_loss 0.00346
| epoch 2 | 1500/1782 batches| train_acc 0.923 train_loss 0.00349
---------------------------------------------------------------------
|epoch 2 | time: 10.16s |valid_acc 0.913 valid_loss 0.004
---------------------------------------------------------------------
| epoch 3 |  500/1782 batches| train_acc 0.941 train_loss 0.00284
| epoch 3 | 1000/1782 batches| train_acc 0.945 train_loss 0.00271
| epoch 3 | 1500/1782 batches| train_acc 0.943 train_loss 0.00273
---------------------------------------------------------------------
|epoch 3 | time: 8.85s |valid_acc 0.924 valid_loss 0.004
---------------------------------------------------------------------
| epoch 4 |  500/1782 batches| train_acc 0.945 train_loss 0.00268
| epoch 4 | 1000/1782 batches| train_acc 0.945 train_loss 0.00267
| epoch 4 | 1500/1782 batches| train_acc 0.946 train_loss 0.00265
---------------------------------------------------------------------
|epoch 4 | time: 8.88s |valid_acc 0.925 valid_loss 0.004
---------------------------------------------------------------------
| epoch 5 |  500/1782 batches| train_acc 0.945 train_loss 0.00269
| epoch 5 | 1000/1782 batches| train_acc 0.948 train_loss 0.00257
| epoch 5 | 1500/1782 batches| train_acc 0.945 train_loss 0.00265
---------------------------------------------------------------------
|epoch 5 | time: 9.23s |valid_acc 0.922 valid_loss 0.004
---------------------------------------------------------------------
| epoch 6 |  500/1782 batches| train_acc 0.948 train_loss 0.00257
| epoch 6 | 1000/1782 batches| train_acc 0.950 train_loss 0.00249
| epoch 6 | 1500/1782 batches| train_acc 0.947 train_loss 0.00259
---------------------------------------------------------------------
|epoch 6 | time: 9.30s |valid_acc 0.925 valid_loss 0.004
---------------------------------------------------------------------
| epoch 7 |  500/1782 batches| train_acc 0.949 train_loss 0.00251
| epoch 7 | 1000/1782 batches| train_acc 0.946 train_loss 0.00264
| epoch 7 | 1500/1782 batches| train_acc 0.950 train_loss 0.00245
---------------------------------------------------------------------
|epoch 7 | time: 8.93s |valid_acc 0.925 valid_loss 0.004
---------------------------------------------------------------------
| epoch 8 |  500/1782 batches| train_acc 0.949 train_loss 0.00251
| epoch 8 | 1000/1782 batches| train_acc 0.946 train_loss 0.00260
| epoch 8 | 1500/1782 batches| train_acc 0.950 train_loss 0.00249
---------------------------------------------------------------------
|epoch 8 | time: 8.79s |valid_acc 0.925 valid_loss 0.004
---------------------------------------------------------------------
| epoch 9 |  500/1782 batches| train_acc 0.947 train_loss 0.00254
| epoch 9 | 1000/1782 batches| train_acc 0.950 train_loss 0.00250
| epoch 9 | 1500/1782 batches| train_acc 0.948 train_loss 0.00258
---------------------------------------------------------------------
|epoch 9 | time: 8.84s |valid_acc 0.925 valid_loss 0.004
---------------------------------------------------------------------
| epoch 10 |  500/1782 batches| train_acc 0.949 train_loss 0.00248
| epoch 10 | 1000/1782 batches| train_acc 0.947 train_loss 0.00256
| epoch 10 | 1500/1782 batches| train_acc 0.951 train_loss 0.00249
---------------------------------------------------------------------
|epoch 10 | time: 9.80s |valid_acc 0.925 valid_loss 0.004
---------------------------------------------------------------------

使用测试数据评估模型

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.909

总结

  • 文本分类常见流程为
    • 准备好原始文本:AG News是广泛用来进行文本分类的数据集
    • 文本清洗:AG News属于已经清洗好的数据集
    • 分词:torchtext库的get_tokenizer()是用于将文本数据分词的函数,它返回一个分词器函数,可以将一个字符串转换为一个单词的列表
    • 文本向量化:其实就是上周的词嵌入过程,这里使用EmbeddingBag方式进行嵌入,将离散的单词映射为固定大小的连续向量。这些向量能较好地捕捉单词间的语义关系。
    • 建模:我们定义的是TextClassificationMode模型,它首先对文本进行嵌入,然后对嵌入结果进行均值聚合

http://www.ppmy.cn/news/1543567.html

相关文章

LeetCode Hot 100:贪心算法

LeetCode Hot 100&#xff1a;贪心算法 121. 买卖股票的最佳时机 class Solution { public:int maxProfit(vector<int>& prices) {int minPrice INT_MAX;int maxProfit 0;for (int& price : prices) {minPrice min(minPrice, price);maxProfit max(maxProf…

AWS CDK 漏洞使黑客能够接管 AWS 账户

Aquasec 的安全研究人员最近在 AWS Cloud Development Kit &#xff08;CDK&#xff09; 中发现了一个关键漏洞&#xff0c;该漏洞可能允许攻击者获得对目标 AWS 账户的完全管理访问权限。 该问题于 2024 年 6 月报告给 AWS&#xff0c;影响使用版本 v2.148.1 或更早版本的 CD…

蓝牙BLE开发——红米手机无法搜索蓝牙设备?

解决 红米手机&#xff0c;无法搜索附近蓝牙设备 具体型号当时忘记查看了&#xff0c;如果你遇到有以下选项&#xff0c;记得打开~ 设置权限

初探Servlet

文章目录 1. Servlet概述1.1 定义1.2 作用 2. 主要知识点2.1 生命周期2.2 请求处理2.3 Servlet配置 3. 案例演示3.1 创建Web应用项目3.2 修改项目工件名3.3 重新部署Web项目3.4 创建WelcomeServlet3.5 编写doGet方法代码3.6 编写doPost方法代码3.7 访问WelcomeServlet 4. 小结 …

Spark窗口函数

1、 Spark中的窗口函数 窗口就是单纯在行后面加一个列 可以套多个窗口函数&#xff0c;但彼此之间不能相互引用&#xff0c;是独立的 窗口函数会产生shuffle over就是用来划分窗口的 (1) 分组聚合里面的函数&#xff0c;基…

大数据-199 数据挖掘 机器学习理论 - 决策树 模型 决策与条件 香农熵计算

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 目前已经更新到了&#xff1a; Hadoop&#xff08;已更完&#xff09;HDFS&#xff08;已更完&#xff09;MapReduce&#xff08;已更完&am…

KVM 虚拟机Anolis OS 8.9 下利用宝塔面板中的 Docker 配置 Nextcloud + onlyoffice

第一部分&#xff1a;安装配置 nextcloud 准备 &#xff08;1&#xff09;启动一个 Anolis OS 8.9 虚拟机&#xff0c;见下图。该虚拟机为 anlisos8…0.2 虚拟机的 ssh、hostname 、IP地址都已配置好。 &#xff08;2&#xff09;宝塔面板也已安装好docker 一、环境 do…

Certimate - 免费开源的 SSL 证书托管、自动续签工具,开发者维护 90 天免费证书的救星

很完美的 SSL 证书托管工具&#xff0c;安全可靠&#xff0c;简单易用。 Certimate 是一个由国人开发的 SSL 证书管理工具&#xff0c;提供一个 web UI 界面让我们可以用简单直观的方式来管理 SSL 证书&#xff0c;申请证书、部署证书&#xff0c;以及证书到期续签都是自动完成…