【问题标题】:Pytorch tutorial LSTMPytorch 教程 LSTM
【发布时间】:2018-02-09 11:49:16
【问题描述】:

我试图用 Pytorch 实现关于序列模型和长短期记忆网络的the exercise。这个想法是添加一个 LSTM 词性标注器字符级功能,但我似乎无法解决。他们暗示应该涉及两个 LSTM,一个将输出字符级表示,另一个将负责预测词性标签。我只是不知道如何循环遍历单词级别(在句子中)和字符(在句子的每个单词中)并在 forward 函数中实现它。有谁知道该怎么做?还是遇到类似情况?

这是我的代码:

class LSTMTaggerAug(nn.Module):
def __init__(self, embedding_dim_words, embedding_dim_chars, hidden_dim_words, hidden_dim_chars, vocab_size, tagset_size, charset_size):
    super(LSTMTaggerAug, self).__init__()
    self.hidden_dim_words = hidden_dim_words
    self.hidden_dim_chars = hidden_dim_chars
    self.word_embeddings = nn.Embedding(vocab_size, embedding_dim_words)
    self.char_embeddings = nn.Embedding(charset_size, embedding_dim_chars)
    self.lstm_char = nn.LSTM(embedding_dim_chars, hidden_dim_chars)
    self.lstm_words = nn.LSTM(embedding_dim_words + hidden_dim_chars, hidden_dim_words)
    self.hidden2tag = nn.Linear(hidden_dim_words, tagset_size)
    self.hidden_char = self.init_hidden(c=False)
    self.hidden_words = self.init_hidden(c=True)

def init_hidden(self, c=True):
    if c:
        return (autograd.Variable(torch.zeros(1, 1, self.hidden_dim_words)),
                autograd.Variable(torch.zeros(1, 1, self.hidden_dim_words)))
    else:
        return (autograd.Variable(torch.zeros(1, 1, self.hidden_dim_chars)),
                autograd.Variable(torch.zeros(1, 1, self.hidden_dim_chars)))


def forward(self, sentence, words):
    # embeds = self.word_embeddings(sentence)
    for ix, word in enumerate(sentence):
        chars = words[ix]
        char_embeds = self.char_embeddings(chars)
        lstm_char_out, self.hidden_char = self.lstm_char(
            char_embeds.view(len(chars), 1, -1), self.hidden_char)
        char_rep = lstm_char_out[-1]
        embeds = self.word_embeddings(word)
        embeds_cat = torch.cat((embeds, char_rep), dim=1)
        lstm_out, self.hidden_words = self.lstm_words(embeds_cat, self.hidden_words)
        tag_space = self.hidden2tag(lstm_out.view(1, -1))
        tag_score = F.log_softmax(tag_space, dim=1)
        if ix==0:
            tag_scores = tag_score
        else:
            tag_scores = torch.cat((tag_scores, tag_score), 0)

    return tag_scores

【问题讨论】:

  • 很容易找到previous exercise 的实现(参见here)。我能找到的最接近相关练习的是here,而且回复是相互矛盾的......
  • @DylanF 谢谢!是的,我看到了这些实现并且和你有同样的印象。相当混乱......

标签: neural-network nlp deep-learning lstm pytorch


【解决方案1】:

根据您的描述,最幼稚的做法是使用去掉标点符号的句子s。然后拆分成单词:

words = s.split()

获取您的第一个字符级 lstm LSTMc 并将其分别应用于每个单词以对单词进行编码(使用 lstm 的最后输出状态对单词进行编码):

encoded_words = []
for word in words:
    state = state_0

    for char in word:
        h, state = LSTMc(one_hot_encoding(char), state)
    encoded_words.append(h)

在对单词进行编码后,将词性标注器 lstm LSTMw 传递给已编码的单词:

state = statew_0
parts_of_speech = []
for enc_word in encoded_words:
    pos, state = LSTMw(enc_word, state)
    parts_of_speech.append(pos)

【讨论】:

  • 谢谢!!我看到的唯一问题是encoded_words 中的元素可能没有相同的大小。此外,您如何跟踪“encoded_words”和标签(初始 POS)?
  • 是的,好点。最好将最后一个输出 h 作为编码词,让我改变它。
  • 当你训练你的模型时,你有输入的单词序列和相应的标签作为输出。这里我只是说encoded_words 可能与标签不对齐。感谢您的有用反馈,我能够更正我的模型,如果您有时间可以查看here@patapouf_ai
  • 我不明白它怎么会不对齐。第一个词对应第一个词性,依此类推。它们始终按照它们出现的顺序保存。
  • 确实,你是对的!我刚刚考虑过了。谢谢
猜你喜欢
  • 2021-02-15
  • 2018-07-27
  • 1970-01-01
  • 2019-01-25
  • 2020-09-28
  • 2021-06-02
  • 2021-11-16
  • 2019-09-28
  • 2018-09-21
相关资源
最近更新 更多