【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例

💖作者简介:大家好,我是 车神哥,府学路18号的车神🥇
⚡About—> 车神:从 寝室实验室快3分钟,最 慢3分半(那半分钟其实是等 绿
📝个人主页:车手只需要车和手,压力来自论文_府学路18号车神_CSDN博客
🥇 官方认证: 人工智能领域优质创作者
🎉 点赞评论收藏 == 养成习惯一键三连)😋
⚡希望大家多多支持🤗~一起加油 😁

不定期学习《20天掌握Pytorch实战》,有兴趣就跟着专栏一起吧~

开源自由,知识无价~

图片数据建模流程范例

所用到的源代码及书籍+数据集以帮各位小伙伴下载放在文末,自取即可~

😁概览

【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例

; 一、 🎉准备数据(存在问题)

流程和之前都一样。

imdb数据集的目标是根据电影评论的文本内容预测评论的情感标签。

训练集有 20000条电影评论文本, 测试集5000条电影评论文本,其中 正面评论和 负面评论都各 占一半

文本数据预处理较为繁琐,包括 中文切词(本示例不涉及), 构建词典编码转换序列填充构建数据管道等等。

torch中预处理文本数据一般使用 torchtext或者自定义 Datasettorchtext功能非常强大,可以 构建文本分类序列标注问答模型机器翻译NLP任务的数据集。

下面仅演示使用它来 构建文本分类数据集的方法。

较完整的教程可以参考以下知乎文章:《pytorch学习笔记—Torchtext》

https://zhuanlan.zhihu.com/p/65833208

这个链接好像已经失效了,另外我找了C站的一个关于 Torchtext的讲解:

https://blog.csdn.net/nlpuser/article/details/88067167

【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例
关于 torchtext常见API一览:
  • torchtext.data.Example : 用来表示一个样本,数据和标签
  • torchtext.vocab.Vocab: 词汇表,可以导入一些预训练词向量
  • torchtext.data.Datasets: 数据集类,**getitem**返回 Example实例, torchtext.data.TabularDataset是其子类。
  • torchtext.data.Field : 用来定义字段的处理方法(文本字段,标签字段)创建 Example时的 预处理,batch 时的一些处理操作。
  • torchtext.data.Iterator: 迭代器,用来生成 batch
  • torchtext.datasets: 包含了常见的数据集.

下面开始预处理数据(标准化的构建流程,自推自敲一遍较为适宜):

注意一点,这里用到了新的库函数,直接 pip即可

  • pip install torchtext
    【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例
import torch
import string,re
import torchtext

MAX_WORDS = 10000
MAX_LEN = 200
BATCH_SIZE = 20

tokenizer = lambda x:re.sub('[%s]'%string.punctuation,"",x).split(" ")

def filterLowFreqWords(arr,vocab):
    arr = [[x if x<MAX_WORDS else 0 for x in example]
           for example in arr]
    return arr

TEXT = torchtext.data.Field(sequential=True, tokenize=tokenizer, lower=True,
                  fix_length=MAX_LEN,postprocessing = filterLowFreqWords)

LABEL = torchtext.data.Field(sequential=False, use_vocab=False)

ds_train, ds_valid = torchtext.data.TabularDataset.splits(
        path='./data/imdb', train='train.tsv',test='test.tsv', format='tsv',
        fields=[('label', LABEL), ('text', TEXT)],skip_header = False)

TEXT.build_vocab(ds_train)

train_iter, valid_iter = torchtext.data.Iterator.splits(
        (ds_train, ds_valid),  sort_within_batch=True,sort_key=lambda x: len(x.text),
        batch_sizes=(BATCH_SIZE,BATCH_SIZE))

print(ds_train[0].text)
print(ds_train[0].label)

注意:这里会报错,好像是版本更新了,之前的用法已经不能用了

  • AttributeError: module 'torchtext.data' has no attribute 'Field'
    【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例

解决方法

from torchtext.data import Field 替换为 from torchtext.legacy.data import Field (但是这个方法对于torchtext 0.12.0版本不适用),在 torchtext 0.11版本中 field&#x65B9;&#x6CD5;被移到了 torchtext.legacy下,所以会看到其他博客的评论区里出现下面代码适用的情况,但是在 torchtext 0.12.0版本中 legacy目录和 field方法都没了,所以上面的代码无法再适用,会报错。
唯一的办法就是下载旧版本,哎~
下面是对应的版本,对照着更新吧

【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例
安装低版本的 torchtext方法:
conda install -c pytorch torchtext==版本号

直接安装0.6或者0.8都行,然后就可以使用啦~

直接pip安装也行:

pip install torchtext==0.4

解决完上一个问题后,下一个问题又来啦

【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例

OverflowError: Python int too large to convert to C long

尝试了各种办法也没有解决,哎~

打印显示的文档结果为:

['it', 'really', 'boggles', 'my', 'mind', 'when', 'someone', 'comes', 'across', 'a', 'movie', 'like', 'this', 'and', 'claims', 'it', 'to', 'be', 'one', 'of', 'the', 'worst', 'slasher', 'films', 'out', 'there', 'this', 'is', 'by', 'far', 'not', 'one', 'of', 'the', 'worst', 'out', 'there', 'still', 'not', 'a', 'good', 'movie', 'but', 'not', 'the', 'worst', 'nonetheless', 'go', 'see', 'something', 'like', 'death', 'nurse', 'or', 'blood', 'lake', 'and', 'then', 'come', 'back', 'to', 'me', 'and', 'tell', 'me', 'if', 'you', 'think', 'the', 'night', 'brings', 'charlie', 'is', 'the', 'worst', 'the', 'film', 'has', 'decent', 'camera', 'work', 'and', 'editing', 'which', 'is', 'way', 'more', 'than', 'i', 'can', 'say', 'for', 'many', 'more', 'extremely', 'obscure', 'slasher', 'filmsbr', 'br', 'the', 'film', 'doesnt', 'deliver', 'on', 'the', 'onscreen', 'deaths', 'theres', 'one', 'death', 'where', 'you', 'see', 'his', 'pruning', 'saw', 'rip', 'into', 'a', 'neck', 'but', 'all', 'other', 'deaths', 'are', 'hardly', 'interesting', 'but', 'the', 'lack', 'of', 'onscreen', 'graphic', 'violence', 'doesnt', 'mean', 'this', 'isnt', 'a', 'slasher', 'film', 'just', 'a', 'bad', 'onebr', 'br', 'the', 'film', 'was', 'obviously', 'intended', 'not', 'to', 'be', 'taken', 'too', 'seriously', 'the', 'film', 'came', 'in', 'at', 'the', 'end', 'of', 'the', 'second', 'slasher', 'cycle', 'so', 'it', 'certainly', 'was', 'a', 'reflection', 'on', 'traditional', 'slasher', 'elements', 'done', 'in', 'a', 'tongue', 'in', 'cheek', 'way', 'for', 'example', 'after', 'a', 'kill', 'charlie', 'goes', 'to', 'the', 'towns', 'welcome', 'sign', 'and', 'marks', 'the', 'population', 'down', 'one', 'less', 'this', 'is', 'something', 'that', 'can', 'only', 'get', 'a', 'laughbr', 'br', 'if', 'youre', 'into', 'slasher', 'films', 'definitely', 'give', 'this', 'film', 'a', 'watch', 'it', 'is', 'slightly', 'different', 'than', 'your', 'usual', 'slasher', 'film', 'with', 'possibility', 'of', 'two', 'killers', 'but', 'not', 'by', 'much', 'the', 'comedy', 'of', 'the', 'movie', 'is', 'pretty', 'much', 'telling', 'the', 'audience', 'to', 'relax', 'and', 'not', 'take', 'the', 'movie', 'so', 'god', 'darn', 'serious', 'you', 'may', 'forget', 'the', 'movie', 'you', 'may', 'remember', 'it', 'ill', 'remember', 'it', 'because', 'i', 'love', 'the', 'name']
0

继续查看词典信息


print(len(TEXT.vocab))

print(TEXT.vocab.itos[0])
print(TEXT.vocab.itos[1])

print(TEXT.vocab.stoi[''])
print(TEXT.vocab.stoi[''])

print(TEXT.vocab.freqs[''])
print(TEXT.vocab.freqs['a'])
print(TEXT.vocab.freqs['good'])

输出结果为:

108197
<unk>
<pad>
0
1
0
129453
11457

再查看:


for batch in train_iter:
    features = batch.text
    labels = batch.label
    print(features)
    print(features.shape)
    print(labels)
    break

打印结果:

tensor([[  17,   31,  148,  ...,   54,   11,  201],
        [   2,    2,  904,  ...,  335,    7,  109],
        [1371, 1737,   44,  ...,  806,    2,   11],
        ...,
        [   6,    5,   62,  ...,    1,    1,    1],
        [ 170,    0,   27,  ...,    1,    1,    1],
        [  15,    0,   45,  ...,    1,    1,    1]])
torch.Size([200, 20])
tensor([0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0])

class DataLoader:
    def __init__(self,data_iter):
        self.data_iter = data_iter
        self.length = len(data_iter)

    def __len__(self):
        return self.length

    def __iter__(self):

        for batch in self.data_iter:
            yield(torch.transpose(batch.text,0,1),
                  torch.unsqueeze(batch.label.float(),dim = 1))

dl_train = DataLoader(train_iter)
dl_valid = DataLoader(valid_iter)

上面的问题还没解决,下面就不做过多的解释了,基本思路都差不多,如果上面问题有解决的,留言说一下哈~

二、🎉定义模型

使用Pytorch通常有三种方式构建模型:使用nn.Sequential按层顺序构建模型,继承nn.Module基类构建自定义模型,继承nn.Module基类构建模型并辅助应用模型容器(nn.Sequential,nn.ModuleList,nn.ModuleDict)进行封装。

此处选择使用第三种方式进行构建。

和图片还有Day01结构化一致,后面应该殴打his这么定义模型了,就不解释了,只是对于不同的对象建模有区别,大致都是标准化模型(调包嘛,不寒颤)

  • 导入库
import torch
from torch import nn
from torchkeras import LightModel,summary
  • 定义模型
torch.random.seed()
import torch
from torch import nn

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()

        self.embedding = nn.Embedding(num_embeddings = MAX_WORDS,embedding_dim = 3,padding_idx = 1)
        self.conv = nn.Sequential()
        self.conv.add_module("conv_1",nn.Conv1d(in_channels = 3,out_channels = 16,kernel_size = 5))
        self.conv.add_module("pool_1",nn.MaxPool1d(kernel_size = 2))
        self.conv.add_module("relu_1",nn.ReLU())
        self.conv.add_module("conv_2",nn.Conv1d(in_channels = 16,out_channels = 128,kernel_size = 2))
        self.conv.add_module("pool_2",nn.MaxPool1d(kernel_size = 2))
        self.conv.add_module("relu_2",nn.ReLU())

        self.dense = nn.Sequential()
        self.dense.add_module("flatten",nn.Flatten())
        self.dense.add_module("linear",nn.Linear(6144,1))
        self.dense.add_module("sigmoid",nn.Sigmoid())

    def forward(self,x):
        x = self.embedding(x).transpose(1,2)
        x = self.conv(x)
        y = self.dense(x)
        return y

net = Net()
print(net)

summary(net, input_shape = (200,),input_dtype = torch.LongTensor)

输出打印构建好的网络模型:

`python
Net(
(embedding): Embedding(10000, 3, padding_idx=1)
(conv): Sequential(
(conv_1): Conv1d(3, 16, kernel_size=(5,), stride=(1,))
(pool_1): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(relu_1): ReLU()
(conv_2): Conv1d(16, 128, kernel_size=(2,), stride=(1,))
(pool_2): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(relu_2): ReLU()
)
(dense): Sequential(
(flatten): Flatten()
(linear): Linear(in_features=6144, out_features=1, bias=True)
(sigmoid): Sigmoid()
)
)
Input size (MB): 0.000763
Forward/backward pass size (MB): 0.287796
Params size (MB): 0.154972
Estimated Total Size (MB): 0.443531

Original: https://blog.csdn.net/weixin_44333889/article/details/124186466
Author: 府学路18号车神
Title: 【进阶篇】全流程学习《20天掌握Pytorch实战》纪实 | Day03 | 文本数据建模流程范例

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/709653/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球