def animation_update(frame_no, test_data, xf, im, text_fields):
     """Update an animation frame."""
     steps, inpt, out_raw = test_data
     length = len(steps)
     batch = frame_no / (fps * (l + 4 * xf))
     index = int((frame_no % (fps * (l + 4 * xf))) / fps)
     # Cut output after first padding.
     out = [out_raw[i][batch] for i in xrange(len(text_fields))]
     if 0 in out:
         i = out.index(0)
         out = out[0:i] + [0 for _ in xrange(len(out) - i)]
     # Show the state after the first frames.
     if index >= 2 * xf:
         im.set_array(steps[min(length - 1, index - 2 * xf)][batch])
         for i, t in enumerate(text_fields):
             if index - 2 * xf < length:
                 t.set_text("")
             else:
                 t.set_text(data.to_symbol(out[i]))
     else:
         for i, t in enumerate(text_fields):
             t.set_text(
                 data.to_symbol(inpt[i][batch]) if index < xf else "")
         if index < xf:
             im.set_array(np.zeros_like(steps[0][0]))
         else:
             im.set_array(steps[0][batch])
     return im,
Example #2
0
 def animation_update(frame_no, test_data, xf, im, text_fields):
   """Update an animation frame."""
   steps, inpt, out_raw = test_data
   length = len(steps)
   batch = frame_no / (fps * (l+4*xf))
   index = int((frame_no % (fps * (l+4*xf))) / fps)
   # Cut output after first padding.
   out = [out_raw[i][batch] for i in xrange(len(text_fields))]
   if 0 in out:
     i = out.index(0)
     out = out[0:i] + [0 for _ in xrange(len(out) - i)]
   # Show the state after the first frames.
   if index >= 2*xf:
     im.set_array(steps[min(length - 1, index - 2*xf)][batch])
     for i, t in enumerate(text_fields):
       if index - 2*xf < length:
         t.set_text("")
       else:
         t.set_text(data.to_symbol(out[i]))
   else:
     for i, t in enumerate(text_fields):
       t.set_text(data.to_symbol(inpt[i][batch]) if index < xf else "")
     if index < xf:
       im.set_array(np.zeros_like(steps[0][0]))
     else:
       im.set_array(steps[0][batch])
   return im,
Example #3
0
def interactive():
  """Interactively probe an existing model."""
  with tf.Session() as sess:
    model, _, _, _, _, _ = initialize(sess)
    sys.stdout.write("Input to Neural GPU, e.g., 0 1. Use -1 for PAD.\n")
    sys.stdout.write("> ")
    sys.stdout.flush()
    inpt = sys.stdin.readline()
    while inpt:
      ids = [data.to_id(s) for s in inpt.strip().split()]
      inpt, target = data.get_batch(len(ids), 1, False, "",
                                    preset=(ids, [0 for _ in ids]))
      _, res, _, _ = model.step(sess, inpt, target, False)
      res = [np.argmax(o, axis=1) for o in res]
      res = [o for o in res[:len(ids)] if o > 0]
      print "  " + " ".join([data.to_symbol(output[0]) for output in res])
      sys.stdout.write("> ")
      sys.stdout.flush()
      inpt = sys.stdin.readline()
def interactive():
  """Interactively probe an existing model."""
  with tf.Session() as sess:
    model, _, _, _, _, _ = initialize(sess)
    sys.stdout.write("Input to Neural GPU, e.g., 0 1. Use -1 for PAD.\n")
    sys.stdout.write("> ")
    sys.stdout.flush()
    inpt = sys.stdin.readline()
    while inpt:
      ids = [data.to_id(s) for s in inpt.strip().split()]
      inpt, target = data.get_batch(len(ids), 1, False, "",
                                    preset=(ids, [0 for _ in ids]))
      _, res, _, _ = model.step(sess, inpt, target, False)
      res = [np.argmax(o, axis=1) for o in res]
      res = [o for o in res[:len(ids)] if o > 0]
      print("  " + " ".join([data.to_symbol(output[0]) for output in res]))
      sys.stdout.write("> ")
      sys.stdout.flush()
      inpt = sys.stdin.readline()