# [Pytorch]pytorch中的LSTM模型

## 公式表示

Pytorch中LSTM的公式表示为：

${i}_{t}=\sigma \left({W}_{ii}{x}_{t}+{b}_{ii}+{W}_{hi}{h}_{\left(t-1\right)}+{b}_{hi}\right)$

${f}_{t}=\sigma \left({W}_{if}{x}_{t}+{b}_{if}+{W}_{hf}{h}_{\left(t-1\right)}+{b}_{hf}\right)$

${g}_{t}=\mathrm{tanh}\left({W}_{ig}{x}_{t}+{b}_{ig}+{W}_{hg}{h}_{\left(t-1\right)}+{b}_{hg}\right)$

${o}_{t}=\sigma \left({W}_{io}{x}_{t}+{b}_{io}+{W}_{ho}{h}_{\left(t-1\right)}+{b}_{ho}\right)$

${c}_{t}={f}_{t}{c}_{\left(t-1\right)}+{i}_{t}{g}_{t}$

${h}_{t}={o}_{t}\mathrm{tanh}\left({c}_{t}\right)$

## 定义

Pytorch中LSTM的定义如下：

class torch.nn.LSTM(*args, **kwargs)

## 参数列表

• input_size：x的特征维度
• hidden_size：隐藏层的特征维度
• num_layers：lstm隐层的层数，默认为1
• bias：False则${b}_{ih}$$b_{ih}$=0和${b}_{hh}$$b_{hh}$=0. 默认为True
• batch_first：True则输入输出的数据格式为 (batch, seq, feature)
• dropout：除最后一层，每一层的输出都进行dropout，默认为: 0
• bidirectional：True则为双向lstm默认为False
• 输入：input, (${h}_{0}$$h_0$, ${c}_{0}$$c_0$)
• 输出：output, (${h}_{n}$$h_n$,${c}_{n}$$c_n$)

input(seq_len, batch, input_size)
h0(num_layers * num_directions, batch, hidden_size)
c0(num_layers * num_directions, batch, hidden_size)

output(seq_len, batch, hidden_size * num_directions)
hn(num_layers * num_directions, batch, hidden_size)
cn(num_layers * num_directions, batch, hidden_size)

## 实例：基于LSTM的词性标注模型

import torch
import gensim
torch.manual_seed(2)

datas=[('你 叫 什么 名字 ?','n v n n f'),('今天 天气 怎么样 ?','n n adj f'),]
words=[ data[0].split() for data in datas]
tags=[ data[1].split() for data in datas]

id2word=gensim.corpora.Dictionary(words)
word2id=id2word.token2id

id2tag=gensim.corpora.Dictionary(tags)
tag2id=id2tag.token2id

def sen2id(inputs):
return [word2id[word] for word in inputs]
def tags2id(inputs):
return [tag2id[word] for word in inputs]
# print(sen2id('你 叫 什么 名字'.split()))
def formart_input(inputs):
def formart_tag(inputs):

class LSTMTagger(torch.nn.Module):
def __init__(self,embedding_dim,hidden_dim,voacb_size,target_size):
super(LSTMTagger,self).__init__()
self.embedding_dim=embedding_dim
self.hidden_dim=hidden_dim
self.voacb_size=voacb_size
self.target_size=target_size
self.lstm=torch.nn.LSTM(self.embedding_dim,self.hidden_dim)
self.log_softmax=torch.nn.LogSoftmax()
self.embedding=torch.nn.Embedding(self.voacb_size,self.embedding_dim)
self.out2tag=torch.nn.Linear(self.hidden_dim,self.target_size)
def forward(self,inputs):
input=self.embedding((inputs))
out,self.hidden=self.lstm(input.view(-1,1,self.embedding_dim),self.hidden)
tags=self.log_softmax(self.out2tag(out.view(-1,self.hidden_dim)))
return tags

model=LSTMTagger(3,3,len(word2id),len(tag2id))
loss_function=torch.nn.NLLLoss()
optimizer=torch.optim.SGD(model.parameters(),lr=0.1)
for _ in range(100):