code: def din_attention(
      query,
      facts,
      attention_size,
      mask,
      stag="null",
      mode="SUM",
      softmax_stag=1,
      time_major=False,
      return_alphas=False,
  ):
      if isinstance(facts, tuple):
          # In case of Bi-RNN, concatenate the forward and the backward RNN outputs.
          facts = tf.concat(facts, 2)
          print("querry_size mismatch")
          query = tf.concat(
              values=[
                  query,
                  query,
              ],
              axis=1,
          )
  
      if time_major:
          # (T,B,D) => (B,T,D)
          facts = tf.array_ops.transpose(facts, [1, 0, 2])
      mask = tf.equal(mask, tf.ones_like(mask))
      facts_size = facts.get_shape().as_list()[
          -1
      ]  # D value - hidden size of the RNN layer
      querry_size = query.get_shape().as_list()[-1]
      queries = tf.tile(query, [1, tf.shape(facts)[1]])
      queries = tf.reshape(queries, tf.shape(facts))
      din_all = tf.concat(
          [queries, facts, queries - facts, queries * facts], axis=-1
      )
      d_layer_1_all = tf.compat.v1.layers.dense(
          din_all, 80, activation=tf.nn.sigmoid, name="f1_att" + stag
      )
      d_layer_2_all = tf.compat.v1.layers.dense(
          d_layer_1_all,
          40,
          activation=tf.nn.sigmoid,
          name="f2_att" + stag,
      )
      d_layer_3_all = tf.compat.v1.layers.dense(
          d_layer_2_all, 1, activation=None, name="f3_att" + stag
      )
      d_layer_3_all = tf.reshape(
          d_layer_3_all, [-1, 1, tf.shape(facts)[1]]
      )
      scores = d_layer_3_all
      # Mask
      # key_masks = tf.sequence_mask(facts_length, tf.shape(facts)[1])   # [B, T]
      key_masks = tf.expand_dims(mask, 1)  # [B, 1, T]
      paddings = tf.ones_like(scores) * (-(2**32) + 1)
      scores = tf.where(key_masks, scores, paddings)  # [B, 1, T]
  
      # Scale
      # scores = scores / (facts.get_shape().as_list()[-1] ** 0.5)
  
      # Activation
      if softmax_stag:
          scores = tf.nn.softmax(scores)  # [B, 1, T]
  
      # Weighted sum
      if mode == "SUM":
          output = tf.matmul(scores, facts)  # [B, 1, H]
          # output = tf.reshape(output, [-1, tf.shape(facts)[-1]])
      else:
          scores = tf.reshape(scores, [-1, tf.shape(facts)[1]])
          output = facts * tf.expand_dims(scores, -1)
          output = tf.reshape(output, tf.shape(facts))
      return output
  
imports: import tensorflow
  from tensorflow.python.ops.rnn_cell import *
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import init_ops
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import variable_scope
  from tensorflow.python.util import nest
  from tensorflow.python.ops import nn_ops
  from bigdl.dllib.utils.log4Error import *
label_desc: Use image node_id 4 for calling a global function with example usage: din_attention(query, facts, attention_size, mask, stag, mode, softmax_stag, time_major, return_alphas) and returns: output