调整tensorflow渴望代码以执行图

时间:2019-02-18 01:12:17

标签: tensorflow

我正在努力编写渴望与图形兼容的代码,您将如何编写此代码,使其既适合渴望模式又适合图形模式? 这是来自Tensorflow教程的关于文本生成的推断函数,位于https://www.tensorflow.org/tutorials/sequences/text_generation


def generate_text(model, start_string):
  # Evaluation step (generating text using the learned model)

  # Number of characters to generate
  num_generate = 1000

  # Converting our start string to numbers (vectorizing) 
  input_eval = [char2idx[s] for s in start_string]
  input_eval = tf.expand_dims(input_eval, 0)
  print(input_eval)

  # Empty string to store our results
  text_generated = []

  # Low temperatures results in more predictable text.
  # Higher temperatures results in more surprising text.
  # Experiment to find the best setting.
  temperature = 1.0

  # Here batch size == 1
  model.reset_states()
  for i in range(num_generate):
      #predictions = model(input_eval)
      predictions = model(input_eval)

      # remove the batch dimension
      predictions = tf.squeeze(predictions, 0)


      # using a multinomial distribution to predict the word returned by the model
      predictions = predictions / temperature
      predicted_id = tf.multinomial(predictions, num_samples=1)[-1,0].numpy()

      # We pass the predicted word as the next input to the model
      # along with the previous hidden state
      input_eval = tf.expand_dims([predicted_id], 0)

      text_generated.append(idx2char[predicted_id])

  return (start_string + ''.join(text_generated))

0 个答案:

没有答案