code: def position_encode(embed, sentences):
      """Position encoding.
  
      It is defined as:
  
      .. math::
  
         m = \\sum_j l_j A x_j,
  
      where :math:`A` is an embed matrix, :math:`x_j` is :math:`j`-th word ID and
  
      .. math::
  
         l_{kj} = (1 - j / J) - (k / d)(1 - 2j / J).
  
      :math:`J` is length of a sentence and :math:`d` is the dimension of the
      embedding.
  
      """
  
      xp = backend.get_array_module(sentences)
      e = embed(sentences)
      n_words, n_units = e.shape[-2:]
  
      # To avoid 0/0, we use max(length, 1) here.
      # Note that when the length is zero, its embedding is always zero and
      # is ignored.
      length = xp.maximum(
          xp.sum((sentences != 0).astype(xp.float32), axis=-1), 1
      )
      length = length.reshape((length.shape + (1, 1)))
      k = xp.arange(1, n_units + 1, dtype=numpy.float32) / n_units
      i = xp.arange(1, n_words + 1, dtype=numpy.float32)[:, None]
      coeff = (1 - i / length) - k * (1 - 2.0 * i / length)
      e = coeff * e
      s = F.sum(e, axis=-2)
      return s
  
imports: import collections
  import json
  import os
  import numpy
  import six
  import chainer
  from chainer import backend
  import chainer.functions
  from chainer import initializers
  import chainer.links
  import babi
label_desc: Use image node_id 2 for calling a global function with example usage: position_encode(embed, sentences) and returns: s