一、train代码 loss的计算方法为neg_log_likelihood(),我们跳转到该方法。 二、neg_log_likelihood() 第一行代码:在输入句子 “我爱饭” 的情况下,经过_get_lstm_features()方法可以得到一个(3*4)的tensor,分别表示“我”、“爱”、“饭” 对于{START,N,V,END}标签的可能性。 如feats[‘爱’][N]=0.2...
2. def neg_log_likelihood(self, sentence, tags) 如果你完整地把代码读完,你会发现neg_log_likelihood()这个函数是loss function. loss=model.neg_log_likelihood(sentence_in,targets) 我们来分析一下neg_log_likelihood()函数代码: defneg_log_likelihood(self,sentence,tags):# feats: 11*5 经过了LSTM+Lin...
def neg_log_likelihood(self, sentence, tags): # CRF损失函数由两部分组成,真实路径的分数和所有路径的总分数。 # 真实路径的分数应该是所有路径中分数最高的。 # log真实路径的分数/log所有可能路径的分数,越大越好,构造crf loss函数取反,loss越小越好 feats = self._get_lstm_features(sentence) forward_s...
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() # Check predictions after training with torch.no_grad(): precheck_sent = prepare_sequence(training_data[0]...
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() # Check predictions after training with torch.no_grad(): precheck_sent = prepare_sequence(training_data[0]...
def neg_log_likelihood(self, sentence, tags): emissions = self._get_emissions(sentence) forward_score = self._forward_alg(emissions) gold_score = self._score_sentence(emissions, tags) return forward_score - gold_score def _forward_alg(self, emissions): ...
def crf_neg_log_likelihood(self, inp, tags, mask=None, inp_logits=False): # [b, seq_len...
def neg_log_likelihood(self, sentence, tags): emissions = self._get_emissions(sentence) forward_score = self._forward_alg(emissions) gold_score = self._score_sentence(emissions, tags) return forward_score - gold_score def _forward_alg(self, emissions): ...
def neg_log_likelihood(self, sentence, tags): #由lstm层计算得的每一时刻属于某一tag的值 feats = self._get_lstm_features(sentence) # 归一项的值 forward_score = self._forward_alg(feats) # 正确路径的值 gold_score = self._score_sentence(feats, tags) ...
[nll, grad] = InstanceNegLogLikelihood(X, y, theta, modelParams): 实验4和5的内容。其中参数X,y,modelParams和前面介绍的一样,注意X矩阵对CRF来说只算一个样本。参数theta为列向量,大小numParams x 1,是整个CRF模型中共享的参数。这2个实验的实现主要按照博文前面介绍的算法来计算,代码如下: ...