class_docstr: DKN model (Deep Knowledge-Aware Network)
  
  :Citation:
  
      H. Wang, F. Zhang, X. Xie and M. Guo, "DKN: Deep Knowledge-Aware Network for News
      Recommendation", in Proceedings of the 2018 World Wide Web Conference on World
      Wide Web, 2018.
class_docstr_tok: ["DKN","model","(","Deep","Knowledge-Aware","Network",")",":","Citation",":","H.","Wang",",","F.","Zhang",",","X.","Xie","and","M.","Guo",",","``","DKN",":","Deep","Knowledge-Aware","Network","for","News","Recommendation","''",",","in","Proceedings","of","the","2018","World","Wide","Web","Conference","on","World","Wide","Web",",","2018","."]
docstr: This function learns the candidate news article's embedding and user embedding.
  User embedding is generated from click history and also depends on the candidate news article via attention mechanism.
  Article embedding is generated via KCNN module.
  Args:
      candidate_word_batch (object): tensor word indices for constructing news article
      candidate_entity_batch (object): tensor entity values for constructing news article
      click_word_batch (object): tensor word indices for constructing user clicked history
      click_entity_batch (object): tensor entity indices for constructing user clicked history
      hparams (object): global hyper-parameters
  Returns:
      click_field_embed_final_batch: user embedding
      news_field_embed_final_batch: candidate news article embedding
docstr_tok: ["This","function","learns","the","candidate","news","article","'s","embedding","and","user","embedding",".","User","embedding","is","generated","from","click","history","and","also","depends","on","the","candidate","news","article","via","attention","mechanism",".","Article","embedding","is","generated","via","KCNN","module",".","Args",":","candidate_word_batch","(","object",")",":","tensor","word","indices","for","constructing","news","article","candidate_entity_batch","(","object",")",":","tensor","entity","values","for","constructing","news","article","click_word_batch","(","object",")",":","tensor","word","indices","for","constructing","user","clicked","history","click_entity_batch","(","object",")",":","tensor","entity","indices","for","constructing","user","clicked","history","hparams","(","object",")",":","global","hyper-parameters","Returns",":","click_field_embed_final_batch",":","user","embedding","news_field_embed_final_batch",":","candidate","news","article","embedding"]
code: def _build_pair_attention(
      self,
      candidate_word_batch,
      candidate_entity_batch,
      click_word_batch,
      click_entity_batch,
      hparams,
  ):
      """This function learns the candidate news article's embedding and user embedding.
      User embedding is generated from click history and also depends on the candidate news article via attention mechanism.
      Article embedding is generated via KCNN module.
      Args:
          candidate_word_batch (object): tensor word indices for constructing news article
          candidate_entity_batch (object): tensor entity values for constructing news article
          click_word_batch (object): tensor word indices for constructing user clicked history
          click_entity_batch (object): tensor entity indices for constructing user clicked history
          hparams (object): global hyper-parameters
      Returns:
          click_field_embed_final_batch: user embedding
          news_field_embed_final_batch: candidate news article embedding
  
      """
      doc_size = hparams.doc_size
      attention_hidden_sizes = hparams.attention_layer_sizes
  
      clicked_words = tf.reshape(click_word_batch, shape=[-1, doc_size])
      clicked_entities = tf.reshape(
          click_entity_batch, shape=[-1, doc_size]
      )
  
      with tf.compat.v1.variable_scope(
          "attention_net", initializer=self.initializer
      ) as scope:  # noqa: F841
          # use kims cnn to get conv embedding
          with tf.compat.v1.variable_scope(
              "kcnn",
              initializer=self.initializer,
              reuse=tf.compat.v1.AUTO_REUSE,
          ) as cnn_scope:  # noqa: F841
              news_field_embed = self._kims_cnn(
                  candidate_word_batch, candidate_entity_batch, hparams
              )
              click_field_embed = self._kims_cnn(
                  clicked_words, clicked_entities, hparams
              )
              click_field_embed = tf.reshape(
                  click_field_embed,
                  shape=[
                      -1,
                      hparams.history_size,
                      hparams.num_filters * len(hparams.filter_sizes),
                  ],
              )
  
          avg_strategy = False
          if avg_strategy:
              click_field_embed_final = tf.reduce_mean(
                  input_tensor=click_field_embed, axis=1, keepdims=True
              )
          else:
              news_field_embed = tf.expand_dims(news_field_embed, 1)
              news_field_embed_repeat = tf.add(
                  tf.zeros_like(click_field_embed), news_field_embed
              )
              attention_x = tf.concat(
                  axis=-1,
                  values=[click_field_embed, news_field_embed_repeat],
              )
              attention_x = tf.reshape(
                  attention_x, shape=[-1, self.num_filters_total * 2]
              )
              attention_w = tf.compat.v1.get_variable(
                  name="attention_hidden_w",
                  shape=[
                      self.num_filters_total * 2,
                      attention_hidden_sizes,
                  ],
                  dtype=tf.float32,
              )
              attention_b = tf.compat.v1.get_variable(
                  name="attention_hidden_b",
                  shape=[attention_hidden_sizes],
                  dtype=tf.float32,
              )
              curr_attention_layer = tf.compat.v1.nn.xw_plus_b(
                  attention_x, attention_w, attention_b
              )
  
              if hparams.enable_BN is True:
                  curr_attention_layer = (
                      tf.compat.v1.layers.batch_normalization(
                          curr_attention_layer,
                          momentum=0.95,
                          epsilon=0.0001,
                          training=self.is_train_stage,
                      )
                  )
  
              activation = hparams.attention_activation
              curr_attention_layer = self._active_layer(
                  logit=curr_attention_layer, activation=activation
              )
              attention_output_w = tf.compat.v1.get_variable(
                  name="attention_output_w",
                  shape=[attention_hidden_sizes, 1],
                  dtype=tf.float32,
              )
              attention_output_b = tf.compat.v1.get_variable(
                  name="attention_output_b", shape=[1], dtype=tf.float32
              )
              attention_weight = tf.compat.v1.nn.xw_plus_b(
                  curr_attention_layer,
                  attention_output_w,
                  attention_output_b,
              )
              attention_weight = tf.reshape(
                  attention_weight, shape=[-1, hparams.history_size, 1]
              )
              norm_attention_weight = tf.nn.softmax(
                  attention_weight, axis=1
              )
              click_field_embed_final = tf.reduce_sum(
                  input_tensor=tf.multiply(
                      click_field_embed, norm_attention_weight
                  ),
                  axis=1,
                  keepdims=True,
              )
              if attention_w not in self.layer_params:
                  self.layer_params.append(attention_w)
              if attention_b not in self.layer_params:
                  self.layer_params.append(attention_b)
              if attention_output_w not in self.layer_params:
                  self.layer_params.append(attention_output_w)
              if attention_output_b not in self.layer_params:
                  self.layer_params.append(attention_output_b)
          self.news_field_embed_final_batch = tf.squeeze(
              news_field_embed
          )
          click_field_embed_final_batch = tf.squeeze(
              click_field_embed_final
          )
  
      return (
          click_field_embed_final_batch,
          self.news_field_embed_final_batch,
      )
  
file_path: recommenders/recommenders/models/deeprec/models/dkn.py
imports: import numpy
  import tensorflow
  from recommenders.models.deeprec.models.base_model import BaseModel
label_desc: Use image node_id 7 for calling the DKN obj's underlying member method code with example usage: obj._build_pair_attention(candidate_word_batch, candidate_entity_batch, click_word_batch, click_entity_batch, hparams) and returns: click_field_embed_final_batch,  self