def get_y_prob(self, h, y):
"""
:param h: 1D: n_words, 2D: Batch, 3D: n_y
:param y: 1D: n_words, 2D: Batch
:return: gradient of cross entropy: 1D: Batch
"""
batch_index = T.arange(h.shape[1])
z_score0 = self.BOS + h[0] # 1D: batch, 2D: n_y
y_score0 = z_score0[batch_index, y[0]] # 1D: batch
[_, y_scores, z_scores], _ = theano.scan(fn=self._forward_step,
sequences=[h[1:], y[1:]],
outputs_info=[y[0], y_score0, z_score0],
non_sequences=[self.W_t, batch_index])
y_score = y_scores[-1]
z_score = logsumexp(z_scores[-1], axis=1).flatten()
return y_score - z_score
classifier.py 文件源码
python
阅读 36
收藏 0
点赞 0
评论 0
评论列表
文章目录