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])
评论列表
文章目录