Beispiel #1
0
def run_epoch(session, m, vectors_data, labels_data, eval_op, verbose=False):
  """Runs the model on the given data."""
  epoch_size = ((len(vectors_data) // m.batch_size) - 1) // m.num_steps
  start_time = time.time()
  costs = 0.0
  iters = 0
  state = m.initial_state.eval()
  true_count = 0
  for step, (x, y) in enumerate(read_data.ptb_iterator(vectors_data, labels_data, m.batch_size,
                                                    m.num_steps)):
    cost, state, out_logits, _ = session.run([m.cost, m.final_state, m.out_logits, eval_op],
                                 {m.input_data: x,
                                  m.targets: y,
                                  m.initial_state: state})
    costs += cost
    iters += m.num_steps

    labels = tf.reshape(y, [-1])
    correct = tf.nn.in_top_k(tf.cast(out_logits, tf.float32), tf.cast(labels, tf.int32), 1)
    eval_correct = tf.reduce_sum(tf.cast(correct, tf.int32))
    true_count += session.run(eval_correct)

    if verbose and step % (epoch_size // 10) == 10:
      print("%.3f perplexity: %.3f speed: %.0f wps" %
            (step * 1.0 / epoch_size, np.exp(costs / iters),
             iters * m.batch_size / (time.time() - start_time)))

  precision = true_count/(iters*m.batch_size)

  print("precision:")
  print(precision)



  return np.exp(costs / iters)
Beispiel #2
0
def run_epoch(session, m, vectors_data, labels_data, eval_op, summary_writer, verbose=False):
  """Runs the model on the given data."""
  epoch_size = ((len(vectors_data) // m.batch_size) - 1) // m.num_steps
  start_time = time.time()
  costs = 0.0
  iters = 0
  state = m.initial_state.eval()
  true_count = 0
  for step, (x, y) in enumerate(read_data.ptb_iterator(vectors_data, labels_data, m.batch_size,
                                                    m.num_steps)):
    cost, state, out_logits, merged1, _ = session.run([m.cost, m.final_state, m.out_logits, m.merged, eval_op],
                                 {m.input_data: x,
                                  m.targets: y,
                                  m.initial_state: state})
    if (m.num_steps != 1):
      summary_writer.add_summary(merged1,step)

    costs += cost
    iters += m.num_steps

    labels = tf.reshape(y, [-1])
    correct = tf.nn.in_top_k(tf.cast(out_logits, tf.float32), tf.cast(labels, tf.int32), 1, name = "correct")
    eval_correct = tf.reduce_sum(tf.cast(correct, tf.int32))
    true_count += session.run(eval_correct)


    if verbose and step % (epoch_size // 10) == 10:
      print("%.3f perplexity: %.3f speed: %.0f wps" %
            (step * 1.0 / epoch_size, np.exp(costs / iters),
             iters * m.batch_size / (time.time() - start_time)))

  with tf.name_scope("perplexity") as scope:
      perplexity = np.exp(costs / iters)

  perplexity_summary = tf.scalar_summary("perplexity",perplexity)

  with tf.name_scope("precision") as scope:
    precision = true_count/(iters*m.batch_size)

    print("precision:")
    print(precision)

  accuracy_summary = tf.scalar_summary("accuracy", precision)

  merged_2 = tf.merge_summary([perplexity_summary,accuracy_summary])
  merged2 = session.run(merged_2)
  summary_writer.add_summary(merged2)

  return perplexity