util_test.py 文件源码

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

项目:allennlp 作者: allenai 项目源码 文件源码
def test_logsumexp(self):
        # First a simple example where we add probabilities in log space.
        tensor = Variable(torch.FloatTensor([[.4, .1, .2]]))
        log_tensor = tensor.log()
        log_summed = util.logsumexp(log_tensor, dim=-1, keepdim=False)
        assert_almost_equal(log_summed.exp().data.numpy(), [.7])
        log_summed = util.logsumexp(log_tensor, dim=-1, keepdim=True)
        assert_almost_equal(log_summed.exp().data.numpy(), [[.7]])

        # Then some more atypical examples, and making sure this will work with how we handle
        # log masks.
        tensor = Variable(torch.FloatTensor([[float('-inf'), 20.0]]))
        assert_almost_equal(util.logsumexp(tensor).data.numpy(), [20.0])
        tensor = Variable(torch.FloatTensor([[-200.0, 20.0]]))
        assert_almost_equal(util.logsumexp(tensor).data.numpy(), [20.0])
        tensor = Variable(torch.FloatTensor([[20.0, 20.0], [-200.0, 200.0]]))
        assert_almost_equal(util.logsumexp(tensor, dim=0).data.numpy(), [20.0, 200.0])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号