docstr: Helper method for creating the model
docstr_tok: ["Helper","method","for","creating","the","model"]
code: def build_model(self):
      """Helper method for creating the model"""
      vocab = set()
      for story, q, answer in self.train_stories + self.test_stories:
          vocab |= set(story + q + [answer])
      vocab = sorted(vocab)
  
      # Reserve 0 for masking via pad_sequences
      vocab_size = len(vocab) + 1
      story_maxlen = max(
          len(x) for x, _, _ in self.train_stories + self.test_stories
      )
      query_maxlen = max(
          len(x) for _, x, _ in self.train_stories + self.test_stories
      )
  
      word_idx = {c: i + 1 for i, c in enumerate(vocab)}
      (
          self.inputs_train,
          self.queries_train,
          self.answers_train,
      ) = vectorize_stories(
          word_idx, story_maxlen, query_maxlen, self.train_stories
      )
      (
          self.inputs_test,
          self.queries_test,
          self.answers_test,
      ) = vectorize_stories(
          word_idx, story_maxlen, query_maxlen, self.test_stories
      )
  
      # placeholders
      input_sequence = Input((story_maxlen,))
      question = Input((query_maxlen,))
  
      # encoders
      # embed the input sequence into a sequence of vectors
      input_encoder_m = Sequential()
      input_encoder_m.add(
          Embedding(input_dim=vocab_size, output_dim=64)
      )
      input_encoder_m.add(Dropout(self.config.get("dropout", 0.3)))
      # output: (samples, story_maxlen, embedding_dim)
  
      # embed the input into a sequence of vectors of size query_maxlen
      input_encoder_c = Sequential()
      input_encoder_c.add(
          Embedding(input_dim=vocab_size, output_dim=query_maxlen)
      )
      input_encoder_c.add(Dropout(self.config.get("dropout", 0.3)))
      # output: (samples, story_maxlen, query_maxlen)
  
      # embed the question into a sequence of vectors
      question_encoder = Sequential()
      question_encoder.add(
          Embedding(
              input_dim=vocab_size,
              output_dim=64,
              input_length=query_maxlen,
          )
      )
      question_encoder.add(Dropout(self.config.get("dropout", 0.3)))
      # output: (samples, query_maxlen, embedding_dim)
  
      # encode input sequence and questions (which are indices)
      # to sequences of dense vectors
      input_encoded_m = input_encoder_m(input_sequence)
      input_encoded_c = input_encoder_c(input_sequence)
      question_encoded = question_encoder(question)
  
      # compute a "match" between the first input vector sequence
      # and the question vector sequence
      # shape: `(samples, story_maxlen, query_maxlen)`
      match = dot([input_encoded_m, question_encoded], axes=(2, 2))
      match = Activation("softmax")(match)
  
      # add the match matrix with the second input vector sequence
      response = add(
          [match, input_encoded_c]
      )  # (samples, story_maxlen, query_maxlen)
      response = Permute((2, 1))(
          response
      )  # (samples, query_maxlen, story_maxlen)
  
      # concatenate the match matrix with the question vector sequence
      answer = concatenate([response, question_encoded])
  
      # the original paper uses a matrix multiplication.
      # we choose to use a RNN instead.
      answer = LSTM(32)(answer)  # (samples, 32)
  
      # one regularization layer -- more would probably be needed.
      answer = Dropout(self.config.get("dropout", 0.3))(answer)
      answer = Dense(vocab_size)(answer)  # (samples, vocab_size)
      # we output a probability distribution over the vocabulary
      answer = Activation("softmax")(answer)
  
      # build the final model
      model = Model([input_sequence, question], answer)
      return model
  
imports: from __future__ import print_function
  from tensorflow.keras.models import Sequential, Model, load_model
  from tensorflow.keras.layers import Embedding
  from tensorflow.keras.layers import Input, Activation, Dense, Permute, Dropout
  from tensorflow.keras.layers import add, dot, concatenate
  from tensorflow.keras.layers import LSTM
  from tensorflow.keras.optimizers import RMSprop
  from tensorflow.keras.utils import get_file
  from tensorflow.keras.preprocessing.sequence import pad_sequences
  from filelock import FileLock
  import os
  import argparse
  import tarfile
  import numpy
  import re
  from ray import train, tune
label_desc: Use image node_id 1 for calling the MemNNModel obj's underlying member method code with example usage: obj.build_model() and returns: model