temporal_softmax.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:image_captioning 作者: AgrawalAmey 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号