
本文共 4561 字,大约阅读时间需要 15 分钟。
���������������������������������
������������������������NLP���������������������������������������������������������������������������������������������������������������������������������������������������������HMM������������������������������������������������CRF���������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������RNN������������������������������������������������������������������������������������������������������������������������������LSTM������������������������RNN������������������������������������������������������������������
PyTorch������LSTM������
���PyTorch������LSTM���������������������3D������������������������(������������������������������������������)���LSTM������������������������������������������������������Input Gate������������������Forget Gate������������������Cell Gate������������������Output Gate������������������������������������������������������������������������������������������������������������������������������
���������LSTM������������������������
- ��������� i_t = ��(W_{ii}x_t + b_{ii} + W_{hi}h_{t-1} + b_{hi})
- ��������� f_t = ��(W_{if}x_t + b_{if} + W_{hf}h_{t-1} + b_{hf})
- ��������� g_t = tanh(W_{ig}x_t + b_{ig} + W_{hg}h_{t-1} + b_{hg})
- ��������� o_t = ��(W_{io}x_t + b_{io} + W_{ho}h_{t-1} + b_{ho})
- ������������ c_t = f_t���c_{t-1} + i_t���g_t
- ������������ h_t = o_t���tanh(c_t)
���������������������������������������������������������PyTorch���LSTM������������������������������������������������������������������������������������Dropout������������������������������������������������������������������������������������������������������
���������������������PyTorch������LSTM������������
# ������������������������3������������������4���������2���LSTMlstm_layer = nn.LSTM(3, 4, 2)# ���������������������������������������������������5������������3���������������������������������1inputs = [torch.randn(1, 3) for _ in range(5)]input_all = torch.stack(inputs, dim=0) # shape: (5, 1, 3)# ������������������������������������hidden = (torch.randn(2, 1, 4), # shape: (num_layers, batch, hidden_size) torch.randn(2, 1, 4)) # shape: (num_layers, batch, hidden_size)# ���������������������������������������outputs, (hidden_n, cell_n) = lstm_layer(input_all, hidden)# ������������������������������������������print("���������������", outputs.size())
���������������������LSTM������������������������������������������
LSTM���������������������������
LSTM���NLP������������������������������������������������������������������LSTM������������������������������������
def word2id(data, w2i): for ws, tag in data: for w in ws: if w not in w2i: w2i[w] = len(w2i)# ������������������train = [ ("The dog ate the apple".split(), ["DET", "NN", "V", "DET", "NN"]), ("Everyone read that book".split(), ["NN", "V", "DET", "NN"])]# ���������������ID������w2i = {}word2id(train, w2i)t2i = {"DET": 0, "NN": 1, "V": 2}# ������LSTM���������class LstmTag(nn.Module): def __init__(self, embedding_dim, hidden_dim, vocab_size, tag_size): super(LstmTag, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.lstm = nn.LSTM(embedding_dim, hidden_dim) self.lstm2tag = nn.Linear(hidden_dim, tag_size) def forward(self, x): embeds = self.embedding(x) # ������������������������������ # ���������LSTM��������������� (o, h) = self.lstm(embeds.view(len(x), 1, -1)) # ��������� (batch, seq_len, hidden_dim) # ��������������������������������������� tags = self.lstm2tag(o.view(len(x), -1)) return tags_prob # ������������������# ������������������������������������������model = LstmTag(EMBEDDING_DIM, HIDDEN_DIM, len(w2i), len(t2i))loss_function = nn.NLLLoss()opt = optim.SGD(model.parameters(), lr=0.1)for input_x, y in train: # ������������������ID ids = torch.tensor([w2i[w] for w in input_x], dtype=torch.long) # ������������������ID targets = torch.tensor([t2i[w_y] for w_y in y], dtype=torch.long) # ������������ probs = model(ids) # ��������������������������� loss = loss_function(probs, targets) loss.backward() opt.step() # ������������������������������ print("���{}������������={:.4f}".format(iter, loss.item()))
������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
