code: def test_add_positional_features(self):
      # This is hard to test, so we check that we get the same result as the
      # original tensorflow implementation:
      # https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/layers/common_attention.py#L270
      tensor2tensor_result = numpy.asarray(
          [
              [
                  0.00000000e00,
                  0.00000000e00,
                  1.00000000e00,
                  1.00000000e00,
              ],
              [
                  8.41470957e-01,
                  9.99999902e-05,
                  5.40302277e-01,
                  1.00000000e00,
              ],
              [
                  9.09297407e-01,
                  1.99999980e-04,
                  -4.16146845e-01,
                  1.00000000e00,
              ],
          ]
      )
  
      tensor = torch.zeros([2, 3, 4])
      result = util.add_positional_features(
          tensor, min_timescale=1.0, max_timescale=1.0e4
      )
      numpy.testing.assert_almost_equal(
          result[0].detach().cpu().numpy(), tensor2tensor_result
      )
      numpy.testing.assert_almost_equal(
          result[1].detach().cpu().numpy(), tensor2tensor_result
      )
  
      # Check case with odd number of dimensions.
      tensor2tensor_result = numpy.asarray(
          [
              [
                  0.00000000e00,
                  0.00000000e00,
                  0.00000000e00,
                  1.00000000e00,
                  1.00000000e00,
                  1.00000000e00,
                  0.00000000e00,
              ],
              [
                  8.41470957e-01,
                  9.99983307e-03,
                  9.99999902e-05,
                  5.40302277e-01,
                  9.99949992e-01,
                  1.00000000e00,
                  0.00000000e00,
              ],
              [
                  9.09297407e-01,
                  1.99986659e-02,
                  1.99999980e-04,
                  -4.16146815e-01,
                  9.99800026e-01,
                  1.00000000e00,
                  0.00000000e00,
              ],
          ]
      )
  
      tensor = torch.zeros([2, 3, 7])
      result = util.add_positional_features(
          tensor, min_timescale=1.0, max_timescale=1.0e4
      )
      numpy.testing.assert_almost_equal(
          result[0].detach().cpu().numpy(), tensor2tensor_result
      )
      numpy.testing.assert_almost_equal(
          result[1].detach().cpu().numpy(), tensor2tensor_result
      )
  
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 34 for calling the TestNnUtil obj's underlying member method code with example usage: obj.test_add_positional_features() without return types