model_consistency_test.py
def _SampleModel(prefix_words, vocab):
  """Predict next words using the given prefix words.
  Args:
    prefix_words: Prefix words.
    vocab: Vocabulary. Contains max word chard id length and converts between
        words and ids.
  """
  targets = np.zeros([BATCH_SIZE, NUM_TIMESTEPS], np.int32)
  weights = np.ones([BATCH_SIZE, NUM_TIMESTEPS], np.float32)

  sess, t, input = _LoadModel()

  if prefix_words.find('<S>') != 0:
    prefix_words = '<S> ' + prefix_words

  prefix = [vocab.word_to_id(w) for w in prefix_words.split()]
  prefix_char_ids = [vocab.word_to_char_ids(w) for w in prefix_words.split()]
  for _ in xrange(FLAGS.num_samples):
    inputs = np.zeros([BATCH_SIZE, NUM_TIMESTEPS], np.int32)
    char_ids_inputs = np.zeros(
        [BATCH_SIZE, NUM_TIMESTEPS, vocab.max_word_length], np.int32)
    samples = prefix[:]
    char_ids_samples = prefix_char_ids[:]
    sent = ''
    while True:
      inputs[0, 0] = samples[0]
      char_ids_inputs[0, 0, :] = char_ids_samples[0]
      samples = samples[1:]
      char_ids_samples = char_ids_samples[1:]

      softmax = sess.run(t['softmax'],
                         feed_dict={input: char_ids_inputs})

      sample = _SampleSoftmax(softmax[0])
      sample_char_ids = vocab.word_to_char_ids(vocab.id_to_word(sample))

      if not samples:
        samples = [sample]
        char_ids_samples = [sample_char_ids]
      sent += vocab.id_to_word(samples[0]) + ' '
      sys.stderr.write('%s\n' % sent)

      if (vocab.id_to_word(samples[0]) == '</S>' or
          len(sent) > FLAGS.max_sample_words):
        break
Predict next words using the given prefix words.
  Args:
    prefix_words: Prefix words.
    vocab: Vocabulary. Contains max word chard id length and converts between
        words and ids.
  
Predict next words using the given prefix words.
Predict next words using the given prefix words .
def _SampleModel(prefix_words, vocab):
  targets = np.zeros([BATCH_SIZE, NUM_TIMESTEPS], np.int32)
  weights = np.ones([BATCH_SIZE, NUM_TIMESTEPS], np.float32)
  sess, t, input = _LoadModel()
  if prefix_words.find('<S>') != 0:
    prefix_words = '<S> ' + prefix_words
  prefix = [vocab.word_to_id(w) for w in prefix_words.split()]
  prefix_char_ids = [vocab.word_to_char_ids(w) for w in prefix_words.split()]
  for _ in xrange(FLAGS.num_samples):
    inputs = np.zeros([BATCH_SIZE, NUM_TIMESTEPS], np.int32)
    char_ids_inputs = np.zeros(
        [BATCH_SIZE, NUM_TIMESTEPS, vocab.max_word_length], np.int32)
    samples = prefix[:]
    char_ids_samples = prefix_char_ids[:]
    sent = ''
    while True:
      inputs[0, 0] = samples[0]
      char_ids_inputs[0, 0, :] = char_ids_samples[0]
      samples = samples[1:]
      char_ids_samples = char_ids_samples[1:]
      softmax = sess.run(t['softmax'],
                         feed_dict={input: char_ids_inputs})
      sample = _SampleSoftmax(softmax[0])
      sample_char_ids = vocab.word_to_char_ids(vocab.id_to_word(sample))
      if not samples:
        samples = [sample]
        char_ids_samples = [sample_char_ids]
      sent += vocab.id_to_word(samples[0]) + ' '
      sys.stderr.write('%s\n' % sent)
      if (vocab.id_to_word(samples[0]) == '</S>' or
          len(sent) > FLAGS.max_sample_words):
        break
def _SampleModel ( prefix_words , vocab ) : targets = np . zeros ( [ BATCH_SIZE , NUM_TIMESTEPS ] , np . int32 ) weights = np . ones ( [ BATCH_SIZE , NUM_TIMESTEPS ] , np . float32 ) sess , t , input = _LoadModel ( ) if prefix_words . find ( '<S>' ) != 0 : prefix_words = '<S> ' + prefix_words prefix = [ vocab . word_to_id ( w ) for w in prefix_words . split ( ) ] prefix_char_ids = [ vocab . word_to_char_ids ( w ) for w in prefix_words . split ( ) ] for _ in xrange ( FLAGS . num_samples ) : inputs = np . zeros ( [ BATCH_SIZE , NUM_TIMESTEPS ] , np . int32 ) char_ids_inputs = np . zeros ( [ BATCH_SIZE , NUM_TIMESTEPS , vocab . max_word_length ] , np . int32 ) samples = prefix [ : ] char_ids_samples = prefix_char_ids [ : ] sent = '' while True : inputs [ 0 , 0 ] = samples [ 0 ] char_ids_inputs [ 0 , 0 , : ] = char_ids_samples [ 0 ] samples = samples [ 1 : ] char_ids_samples = char_ids_samples [ 1 : ] softmax = sess . run ( t [ 'softmax' ] , feed_dict = { input : char_ids_inputs } ) sample = _SampleSoftmax ( softmax [ 0 ] ) sample_char_ids = vocab . word_to_char_ids ( vocab . id_to_word ( sample ) ) if not samples : samples = [ sample ] char_ids_samples = [ sample_char_ids ] sent += vocab . id_to_word ( samples [ 0 ] ) + ' ' sys . stderr . write ( '%s\n' % sent ) if ( vocab . id_to_word ( samples [ 0 ] ) == '</S>' or len ( sent ) > FLAGS . max_sample_words ) : break
Predict next words using the given prefix words.
Predict next words using the given prefix words .
"""Predict next words using the given prefix words.
  Args:
    prefix_words: Prefix words.
    vocab: Vocabulary. Contains max word chard id length and converts between
        words and ids.
  """
prefix_words
None
vocab
Vocabulary. Contains max word chard id length and converts between
words and ids.
Vocabulary . Contains max word chard id length and converts between words and ids .