class_docstr: Kernel-pooling Neural Ranking Model with RBF kernel.
  https://arxiv.org/abs/1706.06613
  
  # Arguments:
  text1_length: Sequence length of text1 (query).
  text2_length: Sequence length of text2 (doc).
  embedding_file: The path to the word embedding file.
                  Currently only the following GloVe files are supported:
                  "glove.6B.50d.txt", "glove.6B.100d.txt", "glove.6B.200d.txt"
                  "glove.6B.300d.txt", "glove.42B.300d.txt", "glove.840B.300d.txt".
                  You can download from: https://nlp.stanford.edu/projects/glove/.
  word_index: Dictionary of word (string) and its corresponding index (int).
              The index is supposed to start from 1 with 0 reserved for unknown words.
              During the prediction, if you have words that are not in the word_index
              for the training, you can map them to index 0.
              Default is None. In this case, all the words in the embedding_file will
              be taken into account and you can call
              WordEmbedding.get_word_index(embedding_file) to retrieve the dictionary.
  train_embed: Boolean. Whether to train the embedding layer or not. Default is True.
  kernel_num: Int > 1. The number of kernels to use. Default is 21.
  sigma: Float. Defines the kernel width, or the range of its softTF count. Default is 0.1.
  exact_sigma: Float. The sigma used for the kernel that harvests exact matches
               in the case where RBF mu=1.0. Default is 0.001.
  target_mode: String. The target mode of the model. Either 'ranking' or 'classification'.
               For ranking, the output will be the relevance score between text1 and text2 and
               you are recommended to use 'rank_hinge' as loss for pairwise training.
               For classification, the last layer will be sigmoid and the output will be the
               probability between 0 and 1 indicating whether text1 is related to text2 and
               you are recommended to use 'binary_crossentropy' as loss for binary classification.
               Default mode is 'ranking'.
class_docstr_tok: ["Kernel-pooling","Neural","Ranking","Model","with","RBF","kernel",".","https",":","\/\/arxiv.org\/abs\/1706.06613","#","Arguments",":","text1_length",":","Sequence","length","of","text1","(","query",")",".","text2_length",":","Sequence","length","of","text2","(","doc",")",".","embedding_file",":","The","path","to","the","word","embedding","file",".","Currently","only","the","following","GloVe","files","are","supported",":","``","glove.6B.50d.txt","''",",","``","glove.6B.100d.txt","''",",","``","glove.6B.200d.txt","''","``","glove.6B.300d.txt","''",",","``","glove.42B.300d.txt","''",",","``","glove.840B.300d.txt","''",".","You","can","download","from",":","https",":","\/\/nlp.stanford.edu\/projects\/glove\/",".","word_index",":","Dictionary","of","word","(","string",")","and","its","corresponding","index","(","int",")",".","The","index","is","supposed","to","start","from","1","with","0","reserved","for","unknown","words",".","During","the","prediction",",","if","you","have","words","that","are","not","in","the","word_index","for","the","training",",","you","can","map","them","to","index","0",".","Default","is","None",".","In","this","case",",","all","the","words","in","the","embedding_file","will","be","taken","into","account","and","you","can","call","WordEmbedding.get_word_index","(","embedding_file",")","to","retrieve","the","dictionary",".","train_embed",":","Boolean",".","Whether","to","train","the","embedding","layer","or","not",".","Default","is","True",".","kernel_num",":","Int",">","1",".","The","number","of","kernels","to","use",".","Default","is","21.","sigma",":","Float",".","Defines","the","kernel","width",",","or","the","range","of","its","softTF","count",".","Default","is","0.1.","exact_sigma",":","Float",".","The","sigma","used","for","the","kernel","that","harvests","exact","matches","in","the","case","where","RBF","mu=1.0",".","Default","is","0.001.","target_mode",":","String",".","The","target","mode","of","the","model",".","Either","'ranking","'","or","'classification","'",".","For","ranking",",","the","output","will","be","the","relevance","score","between","text1","and","text2","and","you","are","recommended","to","use","'rank_hinge","'","as","loss","for","pairwise","training",".","For","classification",",","the","last","layer","will","be","sigmoid","and","the","output","will","be","the","probability","between","0","and","1","indicating","whether","text1","is","related","to","text2","and","you","are","recommended","to","use","'binary_crossentropy","'","as","loss","for","binary","classification",".","Default","mode","is","'ranking","'","."]
code: def build_model(self):
      # Remark: Share weights for embedding is not supported.
      # Thus here the model takes concatenated input and slice to split the input.
      input = Input(
          name="input", shape=(self.text1_length + self.text2_length,)
      )
      embedding = Embedding(
          self.vocab_size,
          self.embed_size,
          weights=self.embed_weights,
          trainable=self.train_embed,
      )(input)
      query_embed = embedding.slice(1, 0, self.text1_length)
      doc_embed = embedding.slice(
          1, self.text1_length, self.text2_length
      )
      mm = A.batch_dot(
          query_embed, doc_embed, axes=[2, 2]
      )  # Translation Matrix.
      KM = []
      for i in range(self.kernel_num):
          mu = (
              1.0 / (self.kernel_num - 1)
              + (2.0 * i) / (self.kernel_num - 1)
              - 1.0
          )
          sigma = self.sigma
          if mu > 1.0:  # Exact match.
              sigma = self.exact_sigma
              mu = 1.0
          mm_exp = A.exp((-0.5) * (mm - mu) * (mm - mu) / sigma / sigma)
          mm_doc_sum = A.sum(mm_exp, axis=2)
          mm_log = A.log(mm_doc_sum + 1.0)
          # Remark: Keep the reduced dimension for the last sum and squeeze after stack.
          # Otherwise, when batch=1, the output will become a Scalar not compatible for stack.
          mm_sum = A.sum(mm_log, axis=1, keepDims=True)
          KM.append(mm_sum)
      Phi = Squeeze(2)(A.stack(KM))
      if self.target_mode == "ranking":
          output = Dense(1, init="uniform")(Phi)
      else:
          output = Dense(1, init="uniform", activation="sigmoid")(Phi)
      model = Model(input=input, output=output)
      return model
  
file_path: analytics-zoo/pyzoo/zoo/models/textmatching/knrm.py
imports: import sys
  import zoo.pipeline.api.autograd
  from zoo.models.common import ZooModel
  from zoo.models.textmatching import TextMatcher
  from zoo.pipeline.api.keras.layers import Input, Embedding, Dense, Squeeze, prepare_embedding
  from zoo.pipeline.api.keras.models import Model
  from bigdl.util.common import JTensor
  from zoo.common.utils import callZooFunc
label_desc: Use image node_id 2 for calling the KNRM obj's underlying member method code with example usage: obj.build_model() and returns: model