每天五分钟机器学习算法模型实战:运用深度词袋模型完成文本分类
作者头像
  • 萝卜看点
  • 2020-05-11 12:20:54 2

本文重点

本文介绍了文本分类的三种算法模型,其中两种已经实现。本文将实现最后一种算法模型。

算法模型

本文介绍了一种深度连续词袋模型(Deep Continuous Bag of Words, DeepCBOW),该模型适用于文本分类任务。以下是模型的具体实现:

```python class DeepCbow(nn.Module): def init(self, nwords, ntags, nlayers, embsize, hidsize): super(DeepCbow, self).init() self.nlayers = nlayers self.embedding = nn.Embedding(nwords, embsize) nn.init.xavieruniform(self.embedding.weight) self.linears = nn.ModuleList([nn.Linear(embsize if i == 0 else hidsize, hidsize) for i in range(nlayers)]) for i in range(nlayers): nn.init.xavieruniform(self.linears[i].weight) self.out = nn.Linear(hidsize, ntags) nn.init.xavieruniform_(self.out.weight)

def forward(self, words):
    emb = self.embedding(words)
    emb_sum = torch.sum(emb, dim=0)
    h = emb_sum.view(1, -1)
    for i in range(self.nlayers):
        h = self.linears[i](h)
        h = torch.tanh(h)
    out = self.out(h)
    return out

```

代码解析

self.linears = nn.ModuleList([nn.Linear(emb_size if i == 0 else hid_size, hid_size) for i in range(nlayers)]) 这行代码定义了一个包含多个线性层的列表。nlayers 表示要创建的隐藏层数量。对于第一个隐藏层,输入维度为 emb_size;从第二个隐藏层开始,输入维度均为 hid_size。

维度解析

  • 输入 words 的维度为 [文本单词数]
  • 经过 embedding 层后,维度变为 [文本单词数,embedding 编码维度]
  • 对 emb 在 dim=0 维度求和,此时维度变为 [embedding 编码维度]
  • 将其转换为 [1, embedding 编码维度] 后送入全连接层
  • 最终输出维度为 [1, ntags]

全部代码

```python import torch from torch import nn import random import numpy as np from collections import defaultdict

class DeepCbow(nn.Module): def init(self, nwords, ntags, nlayers, embsize, hidsize): super(DeepCbow, self).init() self.nlayers = nlayers self.embedding = nn.Embedding(nwords, embsize) nn.init.xavieruniform(self.embedding.weight) self.linears = nn.ModuleList([nn.Linear(embsize if i == 0 else hidsize, hidsize) for i in range(nlayers)]) for i in range(nlayers): nn.init.xavieruniform(self.linears[i].weight) self.out = nn.Linear(hidsize, ntags) nn.init.xavieruniform_(self.out.weight)

def forward(self, words):
    emb = self.embedding(words)
    emb_sum = torch.sum(emb, dim=0)
    h = emb_sum.view(1, -1)
    for i in range(self.nlayers):
        h = self.linears[i](h)
        h = torch.tanh(h)
    out = self.out(h)
    return out

w2i = defaultdict(lambda: len(w2i)) t2i = defaultdict(lambda: len(t2i)) UNK = w2i[""]

def read_dataset(filename): with open(filename, "r") as f: for line in f: tag, words = line.lower().strip().split(" ||| ") yield ([w2i[x] for x in words.split(" ")], t2i[tag])

train = list(readdataset("train.txt")) w2i = defaultdict(lambda: UNK, w2i) dev = list(readdataset("test.txt"))

nwords = len(w2i) ntags = len(t2i) EMBSIZE = 64 HIDSIZE = 64 NLAYERS = 2

model = DeepCbow(nwords, ntags, NLAYERS, EMBSIZE, HIDSIZE) criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters())

for epoch in range(100): random.shuffle(train) trainloss = 0.0 for words, tag in train: words = torch.tensor(words) tag = torch.tensor([tag]) score = model(words) loss = criterion(score, tag) trainloss += loss.item() optimizer.zero_grad() loss.backward() optimizer.step()

print(f"当前轮次 {epoch},当前损失 {train_loss / len(train)}")

test_correct = 0.0
for words, tag in dev:
    words = torch.tensor(words)
    scores = model(words).detach().numpy()
    predict = np.argmax(scores)
    if predict == tag:
        test_correct += 1

print(f"当前轮次 {epoch},测试集准确率 {test_correct / len(dev)}")

```

以上是经过改写后的文本内容,确保了信息的准确性和完整性,并且避免了与原文的直接引用或过于相似的表达。

    本文来源:图灵汇
责任编辑: : 萝卜看点
声明:本文系图灵汇原创稿件,版权属图灵汇所有,未经授权不得转载,已经协议授权的媒体下载使用时须注明"稿件来源:图灵汇",违者将依法追究责任。
    分享
模型算法实战深度文本运用机器分钟完成每天
    下一篇