def forward(self, x, y, mask):
self.N = x.get_shape()[0].value
self.T = x.get_shape()[1].value
self.V = x.get_shape()[2].value
x_flat = tf.reshape(x, [self.N * self.T, self.V])
y_flat = tf.reshape(y, [self.N * self.T])
mask_flat = tf.cast(tf.reshape(mask, [self.N * self.T]), tf.float64)
probs = tf.exp(x_flat - tf.reduce_max(x_flat, reduction_indices=[1], keep_dims=True))
probs /= tf.reduce_sum(probs, reduction_indices=[1], keep_dims=True)
coords = tf.transpose(tf.pack([tf.range(self.N * self.T), y_flat]))
loss = -tf.reduce_sum(mask_flat * tf.log(tf.gather_nd(probs, coords))) / self.N
self.y_flat, self.mask_flat, self.probs = y_flat, mask_flat, probs
return loss
评论列表
文章目录