code: def test_logsumexp(self):
      # First a simple example where we add probabilities in log space.
      tensor = torch.FloatTensor([[0.4, 0.1, 0.2]])
      log_tensor = tensor.log()
      log_summed = util.logsumexp(log_tensor, dim=-1, keepdim=False)
      assert_almost_equal(log_summed.exp().data.numpy(), [0.7])
      log_summed = util.logsumexp(log_tensor, dim=-1, keepdim=True)
      assert_almost_equal(log_summed.exp().data.numpy(), [[0.7]])
  
      # Then some more atypical examples, and making sure this will work with how we handle
      # log masks.
      tensor = torch.FloatTensor([[float("-inf"), 20.0]])
      assert_almost_equal(util.logsumexp(tensor).data.numpy(), [20.0])
      tensor = torch.FloatTensor([[-200.0, 20.0]])
      assert_almost_equal(util.logsumexp(tensor).data.numpy(), [20.0])
      tensor = torch.FloatTensor([[20.0, 20.0], [-200.0, 200.0]])
      assert_almost_equal(
          util.logsumexp(tensor, dim=0).data.numpy(), [20.0, 200.0]
      )
  
imports: from __future__ import with_statement
  from __future__ import division
  from __future__ import absolute_import
  import numpy
  from numpy.testing import assert_array_almost_equal, assert_almost_equal
  import torch
  import pytest
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.nn import util
label_desc: Use image node_id 26 for calling the TestNnUtil obj's underlying member method code with example usage: obj.test_logsumexp() without return types