对观测序列X,状态序列y,其误差函数
利用Softmax函数,我们为每一个正确的tag序列定义一个概率值(Y_x代表所有的tag序列,包括不可能出现的)
因而在训练中,我们只需要最大化似然概率即可,这里我们利用对数似然
最难理解的就是上面公式的log部分的计算,这里用一种简便的方法,对于到词w_(i+1)的路径,可以先把到词 w_i 的log-sum-exp计算出来,有点类似递推的意思。
此外,算法里面涉及一个采用维特比解码解码过程,这是一个DP算法。
code:
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.optim as optim
torch.manual_seed(1)
def argmax(vec):
# return the argmax as a python int
_, idx = torch.max(vec, 1)
return idx.item()
def prepare_sequence(seq, to_ix):
idxs = [to_ix[w] for w in seq]
return torch.tensor(idxs, dtype=torch.long)
#TODO 以一种数值稳定性算法求log_sum_exp: 先标准化,取出行中最大值进行broadcast,让每个元素减去最大值后求exp然后再求和
def log_sum_exp(vec):
max_score = vec[0, argmax(vec)]
max_score_broadcast = max_score.view(1, -1).expand(1, vec.size()[1])
return max_score + torch.log(torch.sum(torch.exp(vec - max_score_broadcast)))
class BiLSTM_CRF(nn.Module):
def __init__(self, vocab_size, tag_to_ix, embedding_dim, hidden_dim):
super(BiLSTM_CRF, self).__init__()
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.vocab_size = vocab_size
self.tag_to_ix = tag_to_ix
self.tagset_size = len(tag_to_ix) # tag_size : 3 + 2
self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim // 2,
num_layers=1, bidirectional=True)
#TODO 把LSTM 输出映射到状态空间(tag space)
self.hidden2tag = nn.Linear(hidden_dim, self.tagset_size)
#TODO 状态转移矩阵参数:T(i,j)表示从状态 j 转移到状态 i 的概率,这样第i行就是所有状态转移到状态i的概率
self.transitions = nn.Parameter(torch.randn(self.tagset_size,self.tagset_size))
#TODO 根据上面矩阵元素定义:限制不能从其他状态转移到起始状态,不能从结束状态转移到其他任何状态
self.transitions.data[tag_to_ix[START_TAG], :] = -10000
self.transitions.data[:, tag_to_ix[STOP_TAG]] = -10000
self.hidden = self.init_hidden()
def forward(self, sentence): # dont confuse this with _forward_alg above.
# Get the emission scores from the BiLSTM
lstm_feats = self._get_lstm_features(sentence)
# Find the best path, given the features.
score, tag_seq = self._viterbi_decode(lstm_feats)
return score, tag_seq
def init_hidden(self):
return (torch.randn(2, 1, self.hidden_dim // 2),
torch.randn(2, 1, self.hidden_dim // 2))
'''
_forward_alg求出的是损失函数的log-sum-exp这一项,另一项比较简单
因为计算误差score需要计算每一条可能路径的分数。这里用一种简便的方法,对于到词w_(i+1)的路径,
可以先把到词w_i的log-sum-exp计算出来,然后累加,类似递推的思想
'''
def _forward_alg(self, feats):
init_alphas = torch.full((1, self.tagset_size), -10000.)
#TODO 起始状态score定义为0
init_alphas[0][self.tag_to_ix[START_TAG]] = 0.
forward_var = init_alphas
#TODO 依次遍历句子中的所有词
# feats : (seq_len,tag_size) LSTM映射到tag space的结果
for feat in feats:
alphas_t = [] # 当前时间步的forward tensor
for next_tag in range(self.tagset_size): # TODO 遍历当前时间步(word)的所有可能状态
#TODO 广播当前状态值为tag_size大小,用于和转移到当前时间步的那些状态操作,即 transiton[next_tag]
emit_score = feat[next_tag].view(1, -1).expand(1, self.tagset_size)
#TODO 其他状态转移到next_tag(当前状态)的概率
trans_score = self.transitions[next_tag].view(1, -1)
# TODO next_tag_var[i] 是 计算log-sum-exp 之前 状态 i -> 到状态 next_tag 值
next_tag_var = forward_var + trans_score + emit_score
alphas_t.append(log_sum_exp(next_tag_var).view(1)) # view(1) 把scalar变为[scalar]
#TODO 此时 forward_var:size (1,tag_size) 作为下一个词word_(i+1)的初始值
forward_var = torch.cat(alphas_t).view(1, -1)
# TODO 最优加上所有状态转移到结束状态的值
terminal_var = forward_var + self.transitions[self.tag_to_ix[STOP_TAG]]
# TODO 求出最终log-sum-exp值
alpha = log_sum_exp(terminal_var)
return alpha
def _get_lstm_features(self, sentence):
self.hidden = self.init_hidden()
# self.word_embeds(sentence).shape : (seq_len,voca_size)
embeds = self.word_embeds(sentence).view(len(sentence), 1, -1)# seq_len x batch_size x voca_size
lstm_out, self.hidden = self.lstm(embeds, self.hidden)
lstm_out = lstm_out.view(len(sentence), self.hidden_dim)
lstm_feats = self.hidden2tag(lstm_out)
return lstm_feats
'''
这里求出损失函数另一项
'''
def _score_sentence(self, feats, tags):
# Gives the score of a provided tag sequence
score = torch.zeros(1)
#TODO 加上起始状态
tags = torch.cat([torch.tensor([self.tag_to_ix[START_TAG]], dtype=torch.long), tags])
# TODO 根据损失函数定义 transitions[tags[i + 1], tags[i]] 表示从 状态i到状态(i+1)的概率
# TODO feat[tag[i]] 表示 word[i] 到 tag[i] 的发射概率,这里用 tag[i+1] 因为前面填充了起始状态,
for i, feat in enumerate(feats):
score = score + self.transitions[tags[i + 1], tags[i]] + feat[tags[i + 1]]
score = score + self.transitions[self.tag_to_ix[STOP_TAG], tags[-1]]
return score
def _viterbi_decode(self, feats):
backpointers = []
# Initialieze the viterbi variables in log spac
init_vvars = torch.full((1, self.tagset_size), -10000.)
init_vvars[0][self.tag_to_ix[START_TAG]] = 0
# forward_var at step i holds the viterbi variables for step i-1
forward_var = init_vvars
for feat in feats:
bptrs_t = [] # holds the backpointers for this step
viterbivars_t = [] # holds the viterbi variables for this step
for next_tag in range(self.tagset_size):
# next_tag_var[i] holds the viterbi variable for tag i at the
# previous step, plus the score of transitioning
# from tag i to next_tag.
# We don't include the emission scores here because the max
# does not depend on them (we add them in below)
next_tag_var = forward_var + self.transitions[next_tag]
best_tag_id = argmax(next_tag_var)
bptrs_t.append(best_tag_id)
viterbivars_t.append(next_tag_var[0][best_tag_id].view(1))
# Now add in the emission scores, and assign forward_var to the set
# of viterbi variables we just computed
forward_var = (torch.cat(viterbivars_t) + feat).view(1, -1)
backpointers.append(bptrs_t)
# Transition to STOP_TAG
terminal_var = forward_var + self.transitions[self.tag_to_ix[STOP_TAG]]
best_tag_id = argmax(terminal_var)
# TODO 到达STOP_TAG最大的分数
path_score = terminal_var[0][best_tag_id]
# Follow the back pointers to decode the best path.
best_path = [best_tag_id]
for bptrs_t in reversed(backpointers):
best_tag_id = bptrs_t[best_tag_id]
best_path.append(best_tag_id)
# Pop off the start tag (we dont want to return that to the caller)
start = best_path.pop()
assert start == self.tag_to_ix[START_TAG] # Sanity check
best_path.reverse()
return path_score, best_path
def neg_log_likelihood(self, sentence, tags):
feats = self._get_lstm_features(sentence) # size : (seq_len,tag_size)
forward_score = self._forward_alg(feats)
gold_score = self._score_sentence(feats, tags)
return forward_score - gold_score
START_TAG = ""
STOP_TAG = ""
EMBEDDING_DIM = 5
HIDDEN_DIM = 4
# Make up some training data
training_data = [(
"the wall street journal reported today that apple corporation made money".split(),
"B I I I O O O B I O O".split()
), (
"georgia tech is a university in georgia".split(),
"B I O O O O B".split()
)]
word_to_ix = {}
for sentence, tags in training_data:
for word in sentence:
if word not in word_to_ix:
word_to_ix[word] = len(word_to_ix)
tag_to_ix = {"B": 0, "I": 1, "O": 2, START_TAG: 3, STOP_TAG: 4}
ix_to_tag = {}
for k,v in tag_to_ix.items():
ix_to_tag[v] = k
model = BiLSTM_CRF(len(word_to_ix), tag_to_ix, EMBEDDING_DIM, HIDDEN_DIM)
optimizer = optim.SGD(model.parameters(), lr=0.01, weight_decay=1e-4)
# # Check predictions before training
# with torch.no_grad():
# precheck_sent = prepare_sequence(training_data[0][0], word_to_ix)
# precheck_tags = torch.tensor([tag_to_ix[t] for t in training_data[0][1]], dtype=torch.long)
# # print(model(precheck_sent))
# Make sure prepare_sequence from earlier in the LSTM section is loaded
for epoch in range(300): # again, normally you would NOT do 300 epochs, it is toy data
for sentence, tags in training_data:
# Step 1. Remember that Pytorch accumulates gradients.
# We need to clear them out before each instance
model.zero_grad()
# Step 2. Get our inputs ready for the network, that is,
# turn them into Tensors of word indices.
sentence_in = prepare_sequence(sentence, word_to_ix)
targets = torch.tensor([tag_to_ix[t] for t in tags], dtype=torch.long)
# Step 3. Run our forward pass.
# TODO 这里区别于一般的训练模型,一般模型是从forward的输出用于计算误差
# TODO 而这里因为加上了CRF模型,在neg_log_likelihood定义了CRF误差用于梯度更新
loss = model.neg_log_likelihood(sentence_in, targets)
# Step 4. Compute the loss, gradients, and update the parameters by
# calling optimizer.step()
loss.backward()
optimizer.step()
def get_entity(char_seq,tag_seq):
length = len(char_seq)
entity = []
for i,(char,tag) in enumerate(zip(char_seq,tag_seq)):
if tag == 'B':
if 'ent' in locals().keys():
entity.append(ent)
del ent
ent = char
if i+1 == length:
entity.append(ent)
if tag =='I':
ent = ent + " " + char
if i+1 == length:
entity.append(ent)
if tag not in ['B','I']:
if 'ent' in locals().keys():
entity.append(ent)
del ent
continue
return entity
# Check predictions after training
with torch.no_grad():
precheck_sent = prepare_sequence(training_data[0][0], word_to_ix)
# print(model(precheck_sent)) # 返回路径最大分数和维特比算法得出的路线
path_score,state_path = model(precheck_sent)
y_pred = [ix_to_tag[x] for x in state_path]
entity_list = get_entity(training_data[0][0],y_pred)
for x in entity_list:
print(x)
# We got it!
相关链接:
LSTM+CRF论文:https://arxiv.org/abs/1603.01360
HMM、CRF、BiLSTM、BiLSTM + CRF:https://zhuanlan.zhihu.com/p/61227299
LSTM+CRF误差计算方法:https://createmomo.github.io/2017/11/11/CRF-Layer-on-the-Top-of-BiLSTM-5/
一个博主关于误差计算方法的理解:https://blog.csdn.net/cuihuijun1hao/article/details/79405740
对于LSTM层的作用解释以及对CRF的通俗理解:https://blog.csdn.net/Tianweidadada/article/details/102677383