pytorch+lstm实现的pos示例

yizhihongxing

在自然语言处理中,词性标注(Part-of-Speech Tagging,POS)是一个重要的任务。它的目标是为给定的文本中的每个单词标注其词性,例如名词、动词、形容词等。在PyTorch中,我们可以使用LSTM模型来实现POS任务。

以下是两个示例代码,展示了如何使用PyTorch和LSTM模型实现POS任务:

示例1:使用PyTorch和LSTM模型实现POS任务

import torch
import torch.nn as nn

# 定义一个LSTM模型
class LSTMTagger(nn.Module):
    def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size):
        super(LSTMTagger, self).__init__()
        self.hidden_dim = hidden_dim
        self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
        self.lstm = nn.LSTM(embedding_dim, hidden_dim)
        self.hidden2tag = nn.Linear(hidden_dim, tagset_size)
        self.hidden = self.init_hidden()

    def init_hidden(self):
        return (torch.zeros(1, 1, self.hidden_dim),
                torch.zeros(1, 1, self.hidden_dim))

    def forward(self, sentence):
        embeds = self.word_embeddings(sentence)
        lstm_out, self.hidden = self.lstm(
            embeds.view(len(sentence), 1, -1), self.hidden)
        tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1))
        tag_scores = nn.functional.log_softmax(tag_space, dim=1)
        return tag_scores

# 定义训练数据和测试数据
training_data = [(
    "The cat ate the mouse".split(),
    "DET NOUN VERB DET NOUN".split()
), (
    "The dog chased the cat".split(),
    "DET NOUN VERB DET NOUN".split()
)]

word_to_ix = {}
for sent, tags in training_data:
    for word in sent:
        if word not in word_to_ix:
            word_to_ix[word] = len(word_to_ix)

tag_to_ix = {"DET": 0, "NOUN": 1, "VERB": 2}

# 定义模型参数
EMBEDDING_DIM = 6
HIDDEN_DIM = 6

# 初始化模型
model = LSTMTagger(EMBEDDING_DIM, HIDDEN_DIM, len(word_to_ix), len(tag_to_ix))
loss_function = nn.NLLLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

# 训练模型
for epoch in range(300):
    for sentence, tags in training_data:
        model.zero_grad()
        model.hidden = model.init_hidden()
        sentence_in = torch.tensor([word_to_ix[word] for word in sentence], dtype=torch.long)
        targets = torch.tensor([tag_to_ix[tag] for tag in tags], dtype=torch.long)
        tag_scores = model(sentence_in)
        loss = loss_function(tag_scores, targets)
        loss.backward()
        optimizer.step()

# 测试模型
with torch.no_grad():
    inputs = torch.tensor([word_to_ix[word] for word in "The cat chased the mouse".split()], dtype=torch.long)
    tag_scores = model(inputs)
    print(tag_scores)

在上面的示例代码中,我们首先定义了一个LSTM模型,包含一个嵌入层、一个LSTM层和一个线性层。然后,我们定义了训练数据和测试数据,并使用这些数据训练和测试模型。在训练模型时,我们使用随机梯度下降(SGD)优化器和负对数似然损失函数。在测试模型时,我们使用torch.no_grad()上下文管理器禁用梯度计算,并将测试数据传入模型进行前向计算。

示例2:使用PyTorch和LSTM模型实现POS任务(使用预训练的词向量)

import torch
import torch.nn as nn
import torchtext

# 加载预训练的词向量
glove = torchtext.vocab.GloVe(name="6B", dim=100)

# 定义一个LSTM模型
class LSTMTagger(nn.Module):
    def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size, pretrained_embeddings):
        super(LSTMTagger, self).__init__()
        self.hidden_dim = hidden_dim
        self.word_embeddings = nn.Embedding.from_pretrained(pretrained_embeddings)
        self.lstm = nn.LSTM(embedding_dim, hidden_dim)
        self.hidden2tag = nn.Linear(hidden_dim, tagset_size)
        self.hidden = self.init_hidden()

    def init_hidden(self):
        return (torch.zeros(1, 1, self.hidden_dim),
                torch.zeros(1, 1, self.hidden_dim))

    def forward(self, sentence):
        embeds = self.word_embeddings(sentence)
        lstm_out, self.hidden = self.lstm(
            embeds.view(len(sentence), 1, -1), self.hidden)
        tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1))
        tag_scores = nn.functional.log_softmax(tag_space, dim=1)
        return tag_scores

# 定义训练数据和测试数据
training_data = [(
    "The cat ate the mouse".split(),
    "DET NOUN VERB DET NOUN".split()
), (
    "The dog chased the cat".split(),
    "DET NOUN VERB DET NOUN".split()
)]

word_to_ix = {}
for sent, tags in training_data:
    for word in sent:
        if word not in word_to_ix:
            word_to_ix[word] = len(word_to_ix)

tag_to_ix = {"DET": 0, "NOUN": 1, "VERB": 2}

# 定义模型参数
EMBEDDING_DIM = 100
HIDDEN_DIM = 6

# 初始化模型
pretrained_embeddings = torch.zeros(len(word_to_ix), EMBEDDING_DIM)
for word, idx in word_to_ix.items():
    if word in glove.stoi:
        pretrained_embeddings[idx] = glove[word]
model = LSTMTagger(EMBEDDING_DIM, HIDDEN_DIM, len(word_to_ix), len(tag_to_ix), pretrained_embeddings)
loss_function = nn.NLLLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

# 训练模型
for epoch in range(300):
    for sentence, tags in training_data:
        model.zero_grad()
        model.hidden = model.init_hidden()
        sentence_in = torch.tensor([word_to_ix[word] for word in sentence], dtype=torch.long)
        targets = torch.tensor([tag_to_ix[tag] for tag in tags], dtype=torch.long)
        tag_scores = model(sentence_in)
        loss = loss_function(tag_scores, targets)
        loss.backward()
        optimizer.step()

# 测试模型
with torch.no_grad():
    inputs = torch.tensor([word_to_ix[word] for word in "The cat chased the mouse".split()], dtype=torch.long)
    tag_scores = model(inputs)
    print(tag_scores)

在上面的示例代码中,我们首先加载了预训练的词向量,并使用这些词向量初始化了嵌入层。然后,我们定义了一个LSTM模型,包含一个嵌入层、一个LSTM层和一个线性层。在初始化模型时,我们使用预训练的词向量初始化了嵌入层。在训练模型时,我们使用随机梯度下降(SGD)优化器和负对数似然损失函数。在测试模型时,我们使用torch.no_grad()上下文管理器禁用梯度计算,并将测试数据传入模型进行前向计算。

需要注意的是,在实际应用中,我们可能需要使用更大的数据集和更复杂的模型来实现更好的POS性能。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:pytorch+lstm实现的pos示例 - Python技术站

(0)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • pytorch–(MisMatch in shape & invalid index of a 0-dim tensor)

    在尝试运行CVPR2019一篇行为识别论文的代码时,遇到了两个问题,记录如下。但是,原因没懂,如果看此文章的你了解原理,欢迎留言交流吖。 github代码链接: 方法1: 根据定位的错误位置,我的是215行,将criticD_real.bachward(mone)改为criticD_real.bachward(mone.mean())上一行注释。保存后运行,…

    PyTorch 2023年4月6日
    00
  • Pycharm中切换pytorch的环境和配置的教程详解

    Pycharm中切换PyTorch的环境和配置的教程详解 PyTorch是一个流行的深度学习框架,而PyCharm是一个流行的Python IDE。在PyCharm中使用PyTorch时,您可能需要切换PyTorch的环境和配置。本文将提供详细的教程,以帮助您在PyCharm中成功切换PyTorch的环境和配置。 步骤一:安装Anaconda 首先,您需要安…

    PyTorch 2023年5月16日
    00
  • pytorch(二十一):交叉验证

    一、K折交叉验证 将训练集分成K份,一份做验证集,其他做测试集。这K份都有机会做验证集             二、代码 1 import torch 2 import torch.nn as nn 3 import torchvision 4 from torchvision import datasets,transforms 5 from torch.…

    PyTorch 2023年4月7日
    00
  • pytorch 多gpu训练

    pytorch 多gpu训练 用nn.DataParallel重新包装一下 数据并行有三种情况 前向过程 device_ids=[0, 1, 2] model = model.cuda(device_ids[0]) model = nn.DataParallel(model, device_ids=device_ids) 只要将model重新包装一下就可以。…

    PyTorch 2023年4月6日
    00
  • pytorch梯度剪裁方式

    在PyTorch中,梯度剪裁是一种常用的技术,用于防止梯度爆炸或梯度消失问题。梯度剪裁可以通过限制梯度的范数来实现。下面是一个简单的示例,演示如何在PyTorch中使用梯度剪裁。 示例一:使用nn.utils.clip_grad_norm_()函数进行梯度剪裁 在这个示例中,我们将使用nn.utils.clip_grad_norm_()函数来进行梯度剪裁。下…

    PyTorch 2023年5月15日
    00
  • [转] pytorch指定GPU

    查过好几次这个命令,总是忘,转一篇mark一下吧 转自:http://www.cnblogs.com/darkknightzh/p/6836568.html PyTorch默认使用从0开始的GPU,如果GPU0正在运行程序,需要指定其他GPU。 有如下两种方法来指定需要使用的GPU。 1. 类似tensorflow指定GPU的方式,使用CUDA_VISIBL…

    PyTorch 2023年4月8日
    00
  • pytorch 5 classification 分类

    import torch from torch.autograd import Variable import torch.nn.functional as F import matplotlib.pyplot as plt n_data = torch.ones(100, 2) # 100个具有2个属性的数据 shape=(100,2) x0 = torc…

    2023年4月8日
    00
  • WARNING: Ignoring invalid distribution -ip (d:\anaconda\envs\pytorch1_7\lib\site-packages)

    错误提示:    解决办法: 1.找到该目录    2.删除带~的文件夹(这种情况是由插件安装失败/中途退出引起的,这会导致插件安装异常)  

    2023年4月7日
    00
合作推广
合作推广
分享本页
返回顶部