jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _perform_update_steps(
      self, observ, action, old_policy_params, reward, length):
    """Perform multiple update steps of value function and policy.

    The advantage is computed once at the beginning and shared across
    iterations. We need to decide for the summary of one iteration, and thus
    choose the one after half of the iterations.

    Args:
      observ: Sequences of observations.
      action: Sequences of actions.
      old_policy_params: Parameters of the behavioral policy.
      reward: Sequences of rewards.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    return_ = utility.discounted_return(
        reward, length, self._config.discount)
    value = self._network(observ, length).value
    if self._config.gae_lambda:
      advantage = utility.lambda_advantage(
          reward, value, length, self._config.discount,
          self._config.gae_lambda)
    else:
      advantage = return_ - value
    mean, variance = tf.nn.moments(advantage, axes=[0, 1], keep_dims=True)
    advantage = (advantage - mean) / (tf.sqrt(variance) + 1e-8)
    advantage = tf.Print(
        advantage, [tf.reduce_mean(return_), tf.reduce_mean(value)],
        'return and value: ')
    advantage = tf.Print(
        advantage, [tf.reduce_mean(advantage)],
        'normalized advantage: ')
    episodes = (observ, action, old_policy_params, reward, advantage)
    value_loss, policy_loss, summary = parts.iterate_sequences(
        self._update_step, [0., 0., ''], episodes, length,
        self._config.chunk_length,
        self._config.batch_size,
        self._config.update_epochs,
        padding_value=1)
    print_losses = tf.group(
        tf.Print(0, [tf.reduce_mean(value_loss)], 'value loss: '),
        tf.Print(0, [tf.reduce_mean(policy_loss)], 'policy loss: '))
    with tf.control_dependencies([value_loss, policy_loss, print_losses]):
      return summary[self._config.update_epochs // 2]
//  [python]
def _perform_update_steps(
      self, observ, action, old_policy_params, reward, length):
    """Perform multiple update steps of value function and policy.

    The advantage is computed once at the beginning and shared across
    iterations. We need to decide for the summary of one iteration, and thus
    choose the one after half of the iterations.

    Args:
      observ: Sequences of observations.
      action: Sequences of actions.
      old_policy_params: Parameters of the behavioral policy.
      reward: Sequences of rewards.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    return_ = utility.discounted_return(
        reward, length, self._config.discount)
    value = self._network(observ, length).value
    if self._config.gae_lambda:
      advantage = utility.lambda_advantage(
          reward, value, length, self._config.discount,
          self._config.gae_lambda)
    else:
      advantage = return_ - value
    mean, variance = tf.nn.moments(advantage, axes=[0, 1], keep_dims=True)
    advantage = (advantage - mean) / (tf.sqrt(variance) + 1e-8)
    advantage = tf.print(
        advantage, [tf.reduce_mean(return_), tf.reduce_mean(value)],
        'return and value: ')
    advantage = tf.print(
        advantage, [tf.reduce_mean(advantage)],
        'normalized advantage: ')
    episodes = (observ, action, old_policy_params, reward, advantage)
    value_loss, policy_loss, summary = parts.iterate_sequences(
        self._update_step, [0., 0., ''], episodes, length,
        self._config.chunk_length,
        self._config.batch_size,
        self._config.update_epochs,
        padding_value=1)
    print_losses = tf.group(
        tf.print(0, [tf.reduce_mean(value_loss)], 'value loss: '),
        tf.print(0, [tf.reduce_mean(policy_loss)], 'policy loss: '))
    with tf.control_dependencies([value_loss, policy_loss, print_losses]):
      return summary[self._config.update_epochs // 2]
//  [python]
def _testNestedWhile_2(self, use_gpu):
    # Test the cases that A -> Enter and Exit -> A are partitioned.
    with self.test_session(use_gpu=use_gpu):
      s0 = tf.constant(2.0)
      def inner_loop(s):
        c = lambda s: tf.less(s, 20.0)
        def b(s):
          s1 = tf.add(s, s)
          return s1
        r_s = tf.while_loop(c, b, [s], parallel_iterations=1)
        return r_s

      outer_c = lambda x: tf.less(x, 3000.0)
      def outer_b(x):
        x = tf.Print(x, [x])  # Edge "Print -> Enter" is partitioned
        x = inner_loop(x)
        with tf.device("/cpu:0"):
          x = tf.square(x)    # Edge "Exit -> Square" is partitioned
        return x
      r = tf.while_loop(outer_c, outer_b, [s0], parallel_iterations=1)
      self.assertEqual(1048576.0, r.eval())
//  [python]
def _testNestedWhile_2(self, use_gpu):
    # Test the cases that A -> Enter and Exit -> A are partitioned.
    with self.test_session(use_gpu=use_gpu):
      s0 = tf.constant(2.0)
      def inner_loop(s):
        c = lambda s: tf.less(s, 20.0)
        def b(s):
          s1 = tf.add(s, s)
          return s1
        r_s = tf.while_loop(c, b, [s], parallel_iterations=1)
        return r_s

      outer_c = lambda x: tf.less(x, 3000.0)
      def outer_b(x):
        x = tf.print(x)  # Edge "Print -> Enter" is partitioned
        x = inner_loop(x)
        with tf.device("/cpu:0"):
          x = tf.square(x)    # Edge "Exit -> Square" is partitioned
        return x
      r = tf.while_loop(outer_c, outer_b, [s0], parallel_iterations=1)
      self.assertEqual(1048576.0, r.eval())
//  [python]
def tfprint(tensor: tf.Tensor, name='') -> tf.Tensor:
    if name == '':
        name = tensor.name
    return tf.Print(tensor, [tensor], message="\n"+name+": ", summarize=10000)
//  [python]
def tfprint(tensor: tf.Tensor, name='') -> tf.Tensor:
    if name == '':
        name = tensor.name
    return tf.print(tensor, [tensor], message="\n"+name+": ", summarize=10000)
//  [python]
def inference(self):
		neg_log_prob = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.log_probs, labels=self.action)
		# self.log_probs = tf.Print(self.log_probs, [self.log_probs])
		loss = tf.reduce_mean(neg_log_prob * self.reward)
		
		probs = tf.nn.softmax(self.log_probs)

		entropy = tf.reduce_mean(tf.reduce_mean(- probs * tf.log(tf.clip_by_value(probs,1e-10,1.0)), 1), 0)
		

		loss = tf.Print(loss, [loss])
		entropy = tf.Print(entropy, [entropy])

		totloss = loss - 10 * entropy

		tf.summary.scalar(self.name, totloss)
		return totloss
//  [python]
def inference(self):
		neg_log_prob = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.log_probs, labels=self.action)
		loss = tf.reduce_mean(neg_log_prob * self.reward)
		
		probs = tf.nn.softmax(self.log_probs)

		entropy = tf.reduce_mean(tf.reduce_mean(- probs * tf.log(tf.clip_by_value(probs,1e-10,1.0)), 1), 0)
		

		loss = tf.print(loss, [loss])
		entropy = tf.print(entropy, [entropy])

		totloss = loss - 10 * entropy

		tf.summary.scalar(self.name, totloss)
		return totloss
//  [python]
def decoder(hypes, logits, train=True, skip=True, debug=False):
    fcn_in = logits['fcn_in']
    num_classes = hypes['arch']['num_classes']

    fcn_in = tf.Print(fcn_in, [tf.shape(fcn_in)],
                      message='Shape of %s' % fcn_in.name,
                      summarize=4, first_n=1)

    if 'scale_down' in hypes:
        sd = hypes['scale_down']
    else:
        sd = 1

    he_init = tf.contrib.layers.variance_scaling_initializer()

    l2_regularizer = tf.contrib.layers.l2_regularizer(hypes['wd'])

    # Build score_fr layer
    score_fr = tf.layers.conv2d(
        fcn_in, kernel_size=[1, 1], filters=num_classes, padding='SAME',
        name='score_fr', kernel_initializer=he_init,
        kernel_regularizer=l2_regularizer)

    _activation_summary(score_fr)

    # Do first upsampling
    upscore2 = _upscore_layer(
        score_fr, upshape=tf.shape(logits['feed2']),
        num_classes=num_classes, name='upscore2', ksize=4, stride=2)

    he_init2 = tf.contrib.layers.variance_scaling_initializer(factor=2.0*sd)
    # Score feed2
    score_feed2 = tf.layers.conv2d(
        logits['feed2'], kernel_size=[1, 1], filters=num_classes,
        padding='SAME', name='score_feed2', kernel_initializer=he_init2,
        kernel_regularizer=l2_regularizer)

    _activation_summary(score_feed2)

    if skip:
        # Create skip connection
        fuse_feed2 = tf.add(upscore2, score_feed2)
    else:
        fuse_feed2 = upscore2
        fuse_feed2.set_shape(score_feed2.shape)

    # Do second upsampling
    upscore4 = _upscore_layer(
        fuse_feed2, upshape=tf.shape(logits['feed4']),
        num_classes=num_classes, name='upscore4', ksize=4, stride=2)

    he_init4 = tf.contrib.layers.variance_scaling_initializer(factor=2.0*sd*sd)
    # Score feed4
    score_feed4 = tf.layers.conv2d(
        logits['feed4'], kernel_size=[1, 1], filters=num_classes,
        padding='SAME', name='score_feed4', kernel_initializer=he_init4,
        kernel_regularizer=l2_regularizer)

    _activation_summary(score_feed4)

    if skip:
        # Create second skip connection
        fuse_pool3 = tf.add(upscore4, score_feed4)
    else:
        fuse_pool3 = upscore4
        fuse_pool3.set_shape(score_feed4.shape)

    # Do final upsampling
    upscore32 = _upscore_layer(
        fuse_pool3, upshape=tf.shape(logits['images']),
        num_classes=num_classes, name='upscore32', ksize=16, stride=8)

    decoded_logits = {}
    decoded_logits['logits'] = upscore32
    decoded_logits['softmax'] = _add_softmax(hypes, upscore32)

    return decoded_logits
//  [python]
def decoder(hypes, logits, train=True, skip=True, debug=False):
    fcn_in = logits['fcn_in']
    num_classes = hypes['arch']['num_classes']

    fcn_in = tf.print(fcn_in, [tf.shape(fcn_in)],
                      message='Shape of %s' % fcn_in.name,
                      summarize=4, first_n=1)

    if 'scale_down' in hypes:
        sd = hypes['scale_down']
    else:
        sd = 1

    he_init = tf.contrib.layers.variance_scaling_initializer()

    l2_regularizer = tf.contrib.layers.l2_regularizer(hypes['wd'])

    # Build score_fr layer
    score_fr = tf.layers.conv2d(
        fcn_in, kernel_size=[1, 1], filters=num_classes, padding='SAME',
        name='score_fr', kernel_initializer=he_init,
        kernel_regularizer=l2_regularizer)

    _activation_summary(score_fr)

    # Do first upsampling
    upscore2 = _upscore_layer(
        score_fr, upshape=tf.shape(logits['feed2']),
        num_classes=num_classes, name='upscore2', ksize=4, stride=2)

    he_init2 = tf.contrib.layers.variance_scaling_initializer(factor=2.0*sd)
    # Score feed2
    score_feed2 = tf.layers.conv2d(
        logits['feed2'], kernel_size=[1, 1], filters=num_classes,
        padding='SAME', name='score_feed2', kernel_initializer=he_init2,
        kernel_regularizer=l2_regularizer)

    _activation_summary(score_feed2)

    if skip:
        # Create skip connection
        fuse_feed2 = tf.add(upscore2, score_feed2)
    else:
        fuse_feed2 = upscore2
        fuse_feed2.set_shape(score_feed2.shape)

    # Do second upsampling
    upscore4 = _upscore_layer(
        fuse_feed2, upshape=tf.shape(logits['feed4']),
        num_classes=num_classes, name='upscore4', ksize=4, stride=2)

    he_init4 = tf.contrib.layers.variance_scaling_initializer(factor=2.0*sd*sd)
    # Score feed4
    score_feed4 = tf.layers.conv2d(
        logits['feed4'], kernel_size=[1, 1], filters=num_classes,
        padding='SAME', name='score_feed4', kernel_initializer=he_init4,
        kernel_regularizer=l2_regularizer)

    _activation_summary(score_feed4)

    if skip:
        # Create second skip connection
        fuse_pool3 = tf.add(upscore4, score_feed4)
    else:
        fuse_pool3 = upscore4
        fuse_pool3.set_shape(score_feed4.shape)

    # Do final upsampling
    upscore32 = _upscore_layer(
        fuse_pool3, upshape=tf.shape(logits['images']),
        num_classes=num_classes, name='upscore32', ksize=16, stride=8)

    decoded_logits = {}
    decoded_logits['logits'] = upscore32
    decoded_logits['softmax'] = _add_softmax(hypes, upscore32)

    return decoded_logits
//  [python]
def print_tensor(x, message=''):
    """Prints `message` and the tensor value when evaluated.

     Note that `print_tensor` returns a new tensor identical to `x`
     which should be used in the following code. Otherwise the
     print operation is not taken into account during evaluation.

     # Example
     ```python
         >>> x = K.print_tensor(x, message="x is: ")
     ```

    # Arguments
        x: Tensor to print.
        message: Message to print jointly with the tensor.

    # Returns
        The same tensor `x`, unchanged.
    """
    return tf.Print(x, [x], message)
//  [python]
def print_tensor(x, message=''):
    """Prints `message` and the tensor value when evaluated.

     Note that `print_tensor` returns a new tensor identical to `x`
     which should be used in the following code. Otherwise the
     print operation is not taken into account during evaluation.

     # Example
     
         >>> x = K.print_tensor(x, message="x is: ")
     

    # Arguments
        x: Tensor to print.
        message: Message to print jointly with the tensor.

    # Returns
        The same tensor `x`, unchanged.
    """
    return tf.print(x, message)
//  [python]
def FIFO_queue_demo_no_coord():
    # first let's create a simple random normal Tensor to act as dummy input data
    # this operation should be run more than once, everytime the queue needs filling
    # back up.  However, it isn't in this case, because of our lack of a co-ordinator/
    # proper threading
    dummy_input = tf.random_normal([3], mean=0, stddev=1)
    # let's print so we can see when this operation is called
    dummy_input = tf.Print(dummy_input, data=[dummy_input],
                           message='New dummy inputs have been created: ', summarize=6)
    # create a FIFO queue object
    q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)
    # load up the queue with our dummy input data
    enqueue_op = q.enqueue_many(dummy_input)
    # grab some data out of the queue
    data = q.dequeue()
    # now print how much is left in the queue
    data = tf.Print(data, data=[q.size()], message='This is how many items are left in q: ')
    # create a fake graph that we can call upon
    fg = data + 1
    # now run some operations
    with tf.Session() as sess:
        # first load up the queue
        sess.run(enqueue_op)
        # now dequeue a few times, and we should see the number of items
        # in the queue decrease
        sess.run(fg)
        sess.run(fg)
        sess.run(fg)
        # by this stage the queue will be emtpy, if we run the next time, the queue
        # will block waiting for new data
        sess.run(fg)
        # this will never print:
        print("We're here!")
//  [python]
def FIFO_queue_demo_no_coord():
    # first let's create a simple random normal Tensor to act as dummy input data
    # this operation should be run more than once, everytime the queue needs filling
    # back up.  However, it isn't in this case, because of our lack of a co-ordinator/
    # proper threading
    dummy_input = tf.random_normal([3], mean=0, stddev=1)
    # let's print so we can see when this operation is called
    dummy_input = tf.print(dummy_input, message='New dummy inputs have been created: ', summarize=6)
    # create a FIFO queue object
    q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)
    # load up the queue with our dummy input data
    enqueue_op = q.enqueue_many(dummy_input)
    # grab some data out of the queue
    data = q.dequeue()
    # now print how much is left in the queue
    data = tf.print(data, message='This is how many items are left in q: ')
    # create a fake graph that we can call upon
    fg = data + 1
    # now run some operations
    with tf.Session() as sess:
        # first load up the queue
        sess.run(enqueue_op)
        # now dequeue a few times, and we should see the number of items
        # in the queue decrease
        sess.run(fg)
        sess.run(fg)
        sess.run(fg)
        # by this stage the queue will be emtpy, if we run the next time, the queue
        # will block waiting for new data
        sess.run(fg)
        # this will never print:
        print("We're here!")
//  [python]
def testPrintShape(self):
    inp = tf.constant(2.0, shape=[100, 32])
    inp_printed = tf.Print(inp, [inp])
    self.assertEqual(inp.get_shape(), inp_printed.get_shape())
//  [python]
def testPrintShape(self):
    inp = tf.constant(2.0, shape=[100, 32])
    inp_printed = tf.print(inp, [inp])
    self.assertEqual(inp.get_shape(), inp_printed.get_shape())
//  [python]
def AIS(proposal_log_prob_fn,
        target_log_prob_fn,
        z_init,
        num_steps=10000,
        step_size=0.1,
        num_leapfrog_steps=10,
        bijector=None):
  tf.logging.info("About to call tfp ais")

  def MakeKernelFn(tlp_fn):
    kernel = tfp.mcmc.HamiltonianMonteCarlo(
        target_log_prob_fn=tlp_fn,
        step_size=step_size,
        num_leapfrog_steps=num_leapfrog_steps)
    if bijector is not None:
      kernel = tfp.mcmc.TransformedTransitionKernel(
          inner_kernel=kernel, bijector=bijector)
    return kernel

  z_fin, ais_weights, ais_outputs = tfp.mcmc.sample_annealed_importance_chain(
      num_steps=num_steps,
      proposal_log_prob_fn=proposal_log_prob_fn,
      target_log_prob_fn=target_log_prob_fn,
      current_state=z_init,
      make_kernel_fn=MakeKernelFn)
  tf.logging.info("Tfp ais done")
  log_accept_ratio = ais_outputs.inner_results.log_accept_ratio
  log_accept_ratio = tf.Print(
      log_accept_ratio, [log_accept_ratio], "Log accept\n", summarize=999)
  p_accept = tf.reduce_mean(tf.exp(tf.minimum(log_accept_ratio, 0.)))

  return AISOutputs(log_p=ais_weights, p_accept=p_accept, z_fin=z_fin)
//  [python]
def AIS(proposal_log_prob_fn,
        target_log_prob_fn,
        z_init,
        num_steps=10000,
        step_size=0.1,
        num_leapfrog_steps=10,
        bijector=None):
  tf.logging.info("About to call tfp ais")

  def MakeKernelFn(tlp_fn):
    kernel = tfp.mcmc.HamiltonianMonteCarlo(
        target_log_prob_fn=tlp_fn,
        step_size=step_size,
        num_leapfrog_steps=num_leapfrog_steps)
    if bijector is not None:
      kernel = tfp.mcmc.TransformedTransitionKernel(
          inner_kernel=kernel, bijector=bijector)
    return kernel

  z_fin, ais_weights, ais_outputs = tfp.mcmc.sample_annealed_importance_chain(
      num_steps=num_steps,
      proposal_log_prob_fn=proposal_log_prob_fn,
      target_log_prob_fn=target_log_prob_fn,
      current_state=z_init,
      make_kernel_fn=MakeKernelFn)
  tf.logging.info("Tfp ais done")
  log_accept_ratio = ais_outputs.inner_results.log_accept_ratio
  log_accept_ratio = tf.print(
      log_accept_ratio, [log_accept_ratio], "Log accept\n", summarize=999)
  p_accept = tf.reduce_mean(tf.exp(tf.minimum(log_accept_ratio, 0.)))

  return AISOutputs(log_p=ais_weights, p_accept=p_accept, z_fin=z_fin)
//  [python]
def testPrintGradient(self):
    with self.test_session():
      inp = tf.constant(2.0, shape=[100, 32], name="in")
      w = tf.constant(4.0, shape=[10, 100], name="w")
      wx = tf.matmul(w, inp, name="wx")
      wx_print = tf.Print(wx, [w, w, w])
      wx_grad = tf.gradients(wx, w)[0]
      wx_print_grad = tf.gradients(wx_print, w)[0]
      wxg = wx_grad.eval()
      wxpg = wx_print_grad.eval()
    self.assertAllEqual(wxg, wxpg)
//  [python]
def testPrintGradient(self):
    with self.test_session():
      inp = tf.constant(2.0, shape=[100, 32], name="in")
      w = tf.constant(4.0, shape=[10, 100], name="w")
      wx = tf.matmul(w, inp, name="wx")
      wx_print = tf.print(w, w, w)
      wx_grad = tf.gradients(wx, w)[0]
      wx_print_grad = tf.gradients(wx_print, w)[0]
      wxg = wx_grad.eval()
      wxpg = wx_print_grad.eval()
    self.assertAllEqual(wxg, wxpg)
//  [python]
def debug_tensor(s, msg=None, summarize=10):
    """Print the shape and value of a tensor at test time. Return a new tensor."""
    if not msg:
        msg = s.name
    return tf.Print(s, [tf.shape(s), s], msg + " ", summarize=summarize)
//  [python]
def debug_tensor(s, msg=None, summarize=10):
    """Print the shape and value of a tensor at test time. Return a new tensor."""
    if not msg:
        msg = s.name
    return tf.print(s, [tf.shape(s), s], msg + " ", summarize=summarize)
//  [python]
def FIFO_queue_demo_with_coord():
    # first let's create a simple random normal Tensor to act as dummy input data
    # this operation should be run more than once, everytime the queue needs filling
    # back up.  However, it isn't in this case, because of our lack of a co-ordinator/
    # proper threading
    dummy_input = tf.random_normal([5], mean=0, stddev=1)
    # let's print so we can see when this operation is called
    dummy_input = tf.Print(dummy_input, data=[dummy_input],
                           message='New dummy inputs have been created: ', summarize=6)
    # create a FIFO queue object
    q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)
    # load up the queue with our dummy input data
    enqueue_op = q.enqueue_many(dummy_input)

    # now setup a queue runner to handle enqueue_op outside of the main thread asynchronously
    qr = tf.train.QueueRunner(q, [enqueue_op] * 1)
    # now we need to add qr to the TensorFlow queue runners collection
    tf.train.add_queue_runner(qr)

    # grab some data out of the queue
    data = q.dequeue()
    # now print how much is left in the queue
    data = tf.Print(data, data=[q.size(), data], message='This is how many items are left in q: ')
    # create a fake graph that we can call upon
    fg = data + 1
    # now run some operations
    with tf.Session() as sess:
        # we first create a TensorFlow coordinator instance which will handle
        # all the asynchronous threads and their interactions
        coord = tf.train.Coordinator()
        # now we have to start all our queue runners - if we neglect to do this
        # the main thread will hang waiting for them to be started
        threads = tf.train.start_queue_runners(coord=coord)
        # As opposed to the previous function, we don't have to call sess.run(enqueue_op)
        # because our queue runner will figure out when this needs to be called.  It
        # will do so at the beginning, and also when the queue runs out of values

        # now dequeue a few times, and we should see the number of items
        # in the queue decrease
        sess.run(fg)
        sess.run(fg)
        sess.run(fg)
        # previously the main thread blocked / hung at this point, as it was waiting
        # for the queue to be filled.  However, it won't this time around, as we
        # now have a queue runner on another thread making sure the queue is
        # filled asynchronously
        sess.run(fg)
        sess.run(fg)
        sess.run(fg)
        # this will print, but not necessarily after the 6th call of sess.run(fg)
        # due to the asynchronous operations
        print("We're here!")

        # we have to request all threads now stop, then we can join the queue runner
        # thread back to the main thread and finish up
        coord.request_stop()
        coord.join(threads)
//  [python]
def FIFO_queue_demo_with_coord():
    # first let's create a simple random normal Tensor to act as dummy input data
    # this operation should be run more than once, everytime the queue needs filling
    # back up.  However, it isn't in this case, because of our lack of a co-ordinator/
    # proper threading
    dummy_input = tf.random_normal([5], mean=0, stddev=1)
    # let's print so we can see when this operation is called
    dummy_input = tf.print(dummy_input, data=[dummy_input],
                           message='New dummy inputs have been created: ', summarize=6)
    # create a FIFO queue object
    q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)
    # load up the queue with our dummy input data
    enqueue_op = q.enqueue_many(dummy_input)

    # now setup a queue runner to handle enqueue_op outside of the main thread asynchronously
    qr = tf.train.QueueRunner(q, [enqueue_op] * 1)
    # now we need to add qr to the TensorFlow queue runners collection
    tf.train.add_queue_runner(qr)

    # grab some data out of the queue
    data = q.dequeue()
    # now print how much is left in the queue
    data = tf.print(data, data=[q.size(), data], message='This is how many items are left in q: ')
    # create a fake graph that we can call upon
    fg = data + 1
    # now run some operations
    with tf.Session() as sess:
        # we first create a TensorFlow coordinator instance which will handle
        # all the asynchronous threads and their interactions
        coord = tf.train.Coordinator()
        # now we have to start all our queue runners - if we neglect to do this
        # the main thread will hang waiting for them to be started
        threads = tf.train.start_queue_runners(coord=coord)
        # As opposed to the previous function, we don't have to call sess.run(enqueue_op)
        # because our queue runner will figure out when this needs to be called.  It
        # will do so at the beginning, and also when the queue runs out of values

        # now dequeue a few times, and we should see the number of items
        # in the queue decrease
        sess.run(fg)
        sess.run(fg)
        sess.run(fg)
        # previously the main thread blocked / hung at this point, as it was waiting
        # for the queue to be filled.  However, it won't this time around, as we
        # now have a queue runner on another thread making sure the queue is
        # filled asynchronously
        sess.run(fg)
        sess.run(fg)
        sess.run(fg)
        # this will print, but not necessarily after the 6th call of sess.run(fg)
        # due to the asynchronous operations
        print("We're here!")

        # we have to request all threads now stop, then we can join the queue runner
        # thread back to the main thread and finish up
        coord.request_stop()
        coord.join(threads)
//  [python]
def log_quaternion_loss(predictions, labels, params):
  """A helper function to compute the mean error between batches of quaternions.

  The caller is expected to add the loss to the graph.

  Args:
    predictions: A Tensor of size [batch_size, 4].
    labels: A Tensor of size [batch_size, 4].
    params: A dictionary of parameters. Expecting 'use_logging', 'batch_size'.

  Returns:
    A Tensor of size 1, denoting the mean error between batches of quaternions.
  """
  use_logging = params['use_logging']
  logcost = log_quaternion_loss_batch(predictions, labels, params)
  logcost = tf.reduce_sum(logcost, [0])
  batch_size = params['batch_size']
  logcost = tf.multiply(logcost, 1.0 / batch_size, name='log_quaternion_loss')
  if use_logging:
    logcost = tf.Print(
        logcost, [logcost], '[logcost]', name='log_quaternion_loss_print')
  return logcost
//  [python]
def log_quaternion_loss(predictions, labels, params):
  """A helper function to compute the mean error between batches of quaternions.

  The caller is expected to add the loss to the graph.

  Args:
    predictions: A Tensor of size [batch_size, 4].
    labels: A Tensor of size [batch_size, 4].
    params: A dictionary of parameters. Expecting 'use_logging', 'batch_size'.

  Returns:
    A Tensor of size 1, denoting the mean error between batches of quaternions.
  """
  use_logging = params['use_logging']
  logcost = log_quaternion_loss_batch(predictions, labels, params)
  logcost = tf.reduce_sum(logcost, [0])
  batch_size = params['batch_size']
  logcost = tf.multiply(logcost, 1.0 / batch_size, name='log_quaternion_loss')
  if use_logging:
    logcost = tf.print(
        logcost, [logcost], '[logcost]', name='log_quaternion_loss_print')
  return logcost
//  [python]
def make_test_summaries(self, names_to_values):
        # Create the summary ops such that they also print out to std output:
        summary_ops = []
        for metric_name, metric_value in names_to_values.iteritems():
            op = tf.summary.scalar(metric_name, metric_value)
            op = tf.Print(op, [metric_value], metric_name)
            summary_ops.append(op)
        return summary_ops
//  [python]
def make_test_summaries(self, names_to_values):
        # Create the summary ops such that they also print out to std output:
        summary_ops = []
        for metric_name, metric_value in names_to_values.iteritems():
            op = tf.summary.scalar(metric_name, metric_value)
            op = tf.print(metric_value, metric_name)
            summary_ops.append(op)
        return summary_ops
//  [python]
def log_quaternion_loss_batch(predictions, labels, params):
  """A helper function to compute the error between quaternions.

  Args:
    predictions: A Tensor of size [batch_size, 4].
    labels: A Tensor of size [batch_size, 4].
    params: A dictionary of parameters. Expecting 'use_logging', 'batch_size'.

  Returns:
    A Tensor of size [batch_size], denoting the error between the quaternions.
  """
  use_logging = params['use_logging']
  assertions = []
  if use_logging:
    assertions.append(
        tf.Assert(
            tf.reduce_all(
                tf.less(
                    tf.abs(tf.reduce_sum(tf.square(predictions), [1]) - 1),
                    1e-4)),
            ['The l2 norm of each prediction quaternion vector should be 1.']))
    assertions.append(
        tf.Assert(
            tf.reduce_all(
                tf.less(
                    tf.abs(tf.reduce_sum(tf.square(labels), [1]) - 1), 1e-4)),
            ['The l2 norm of each label quaternion vector should be 1.']))

  with tf.control_dependencies(assertions):
    product = tf.multiply(predictions, labels)
  internal_dot_products = tf.reduce_sum(product, [1])

  if use_logging:
    internal_dot_products = tf.Print(internal_dot_products, [
        internal_dot_products,
        tf.shape(internal_dot_products)
    ], 'internal_dot_products:')

  logcost = tf.log(1e-4 + 1 - tf.abs(internal_dot_products))
  return logcost
//  [python]
def log_quaternion_loss_batch(predictions, labels, params):
  """A helper function to compute the error between quaternions.

  Args:
    predictions: A Tensor of size [batch_size, 4].
    labels: A Tensor of size [batch_size, 4].
    params: A dictionary of parameters. Expecting 'use_logging', 'batch_size'.

  Returns:
    A Tensor of size [batch_size], denoting the error between the quaternions.
  """
  use_logging = params['use_logging']
  assertions = []
  if use_logging:
    assertions.append(
        tf.Assert(
            tf.reduce_all(
                tf.less(
                    tf.abs(tf.reduce_sum(tf.square(predictions), [1]) - 1),
                    1e-4)),
            ['The l2 norm of each prediction quaternion vector should be 1.']))
    assertions.append(
        tf.Assert(
            tf.reduce_all(
                tf.less(
                    tf.abs(tf.reduce_sum(tf.square(labels), [1]) - 1), 1e-4)),
            ['The l2 norm of each label quaternion vector should be 1.']))

  with tf.control_dependencies(assertions):
    product = tf.multiply(predictions, labels)
  internal_dot_products = tf.reduce_sum(product, [1])

  if use_logging:
    internal_dot_products = tf.print(internal_dot_products, [
        internal_dot_products,
        tf.shape(internal_dot_products)
    ], 'internal_dot_products:')

  logcost = tf.log(1e-4 + 1 - tf.abs(internal_dot_products))
  return logcost
//  [python]
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 2, 2])
x = tf.Print(x, [x], message="P1")
//  [python]
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 2, 2])
x = tf.print(x, message="P1")
//  [python]
def print_tensors(**tensors):
  """Host call function to print Tensors from the TPU during training."""
  tf.logging.info("Print these tensors: %s", tensors)
  first_n = 25
  print_op = tf.no_op()
  for name in sorted(tensors):
    with tf.control_dependencies([print_op]):
      tensor = tensors[name]
      print_op = tf.Print(
          tensor, [tensor],
          message=name + "=",
          first_n=first_n,
          summarize=1024 * 2 * 8)
  with tf.control_dependencies([print_op]):
    return tf.Print(0., [0.], message="------", first_n=first_n)
//  [python]
def print_tensors(**tensors):
  """Host call function to print Tensors from the TPU during training."""
  tf.logging.info("Print these tensors: %s", tensors)
  first_n = 25
  print_op = tf.no_op()
  for name in sorted(tensors):
    with tf.control_dependencies([print_op]):
      tensor = tensors[name]
      print_op = tf.print(
          tensor,
          message=name + "=",
          first_n=first_n,
          summarize=1024 * 2 * 8)
  with tf.control_dependencies([print_op]):
    return tf.print(0., message="------", first_n=first_n)
//  [python]
def print_act_stats(x, _str="", flatten=False):
    if False:
        return x
    _x = ew.float_cast(x, dtype=tf.float32)
    if flatten:
        _x = tf.reshape(_x, [-1])
    if len(_x.get_shape()) == 1:
        x_mean, x_var = tf.nn.moments(_x, [0], keep_dims=True)
    if len(_x.get_shape()) == 2:
        x_mean, x_var = tf.nn.moments(_x, [0], keep_dims=True)
    if len(_x.get_shape()) == 4:
        x_mean, x_var = tf.nn.moments(_x, [0,2,3], keep_dims=True)
    stats = [tf.reduce_min(x_mean), tf.reduce_mean(x_mean), tf.reduce_max(x_mean),\
            tf.reduce_min(tf.sqrt(x_var)), tf.reduce_mean(tf.sqrt(x_var)), tf.reduce_max(tf.sqrt(x_var))]
    __str = "["+_str+"] "+x.name
    print(__str)
    return tf.Print(x, stats, __str)
//  [python]
def print_act_stats(x, _str="", flatten=False):
    if False:
        return x
    _x = ew.float_cast(x, dtype=tf.float32)
    if flatten:
        _x = tf.reshape(_x, [-1])
    if len(_x.get_shape()) == 1:
        x_mean, x_var = tf.nn.moments(_x, [0], keep_dims=True)
    if len(_x.get_shape()) == 2:
        x_mean, x_var = tf.nn.moments(_x, [0], keep_dims=True)
    if len(_x.get_shape()) == 4:
        x_mean, x_var = tf.nn.moments(_x, [0,2,3], keep_dims=True)
    stats = [tf.reduce_min(x_mean), tf.reduce_mean(x_mean), tf.reduce_max(x_mean),\
            tf.reduce_min(tf.sqrt(x_var)), tf.reduce_mean(tf.sqrt(x_var)), tf.reduce_max(tf.sqrt(x_var))]
    __str = "["+_str+"] "+x.name
    print(__str)
    return tf.print(x, stats, __str)
//  [python]
def _update_value(self, observ, reward, length):
    """Perform multiple update steps of the value baseline.

    We need to decide for the summary of one iteration, and thus choose the one
    after half of the iterations.

    Args:
      observ: Sequences of observations.
      reward: Sequences of reward.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    with tf.name_scope('update_value'):
      loss, summary = tf.scan(lambda _1, _2: self._update_value_step(observ, reward, length),
                              tf.range(self._config.update_epochs_value), [0., ''],
                              parallel_iterations=1)
      print_loss = tf.Print(0, [tf.reduce_mean(loss)], 'value loss: ')
      with tf.control_dependencies([loss, print_loss]):
        return summary[self._config.update_epochs_value // 2]
//  [python]
def _update_value(self, observ, reward, length):
    """Perform multiple update steps of the value baseline.

    We need to decide for the summary of one iteration, and thus choose the one
    after half of the iterations.

    Args:
      observ: Sequences of observations.
      reward: Sequences of reward.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    with tf.name_scope('update_value'):
      loss, summary = tf.scan(lambda _1, _2: self._update_value_step(observ, reward, length),
                              tf.range(self._config.update_epochs_value), [0., ''],
                              parallel_iterations=1)
      print_loss = tf.print(0, [tf.reduce_mean(loss)], 'value loss: ')
      with tf.control_dependencies([loss, print_loss]):
        return summary[self._config.update_epochs_value // 2]
//  [python]
def log_deferred(op, log_id, every_n=1, first_n=None):
  """Helper method inserting compliance logging ops.

  Note: This helper is not guaranteed to be efficient, as it will insert ops
        and control dependencies. If this proves to be a bottleneck, submitters
        may wish to consider other methods such as extracting values from an
        .events file.

  Args:
    op: A tf op to be printed.
    log_id: a uuid provided by the logger in mlperf_log.py
    every_n: If repeat is True, with what frequency should the input op be '
             logged. If repeat is False, this argument is ignored.
    first_n: Only log this many values. This arg does not interact with every_n.
             The first_n refers to the first n that would have been logged.
  """

  prefix = ":::MLPv0.5.0 [{}]".format(log_id)
  if first_n is not None and first_n == 1:
    return tf.compat.v1.Print(op, [tf.timestamp(), op], message=prefix, first_n=1)

  counter = tf.Variable(tf.zeros(shape=(), dtype=tf.int32) - 1,
                        aggregation=tf.VariableAggregation.MEAN)
  increment = tf.compat.v1.assign_add(counter, 1, use_locking=True)
  return tf.cond(
      pred=tf.equal(tf.math.mod(increment, every_n), 0),
      true_fn=lambda :tf.compat.v1.Print(op, [tf.timestamp(), op], message=prefix,
                       first_n=first_n),
      false_fn=lambda :op
  )
//  [python]
def log_deferred(op, log_id, every_n=1, first_n=None):
  """Helper method inserting compliance logging ops.

  Note: This helper is not guaranteed to be efficient, as it will insert ops
        and control dependencies. If this proves to be a bottleneck, submitters
        may wish to consider other methods such as extracting values from an
        .events file.

  Args:
    op: A tf op to be printed.
    log_id: a uuid provided by the logger in mlperf_log.py
    every_n: If repeat is True, with what frequency should the input op be '
             logged. If repeat is False, this argument is ignored.
    first_n: Only log this many values. This arg does not interact with every_n.
             The first_n refers to the first n that would have been logged.
  """

  prefix = ":::MLPv0.5.0 [{}]".format(log_id)
  if first_n is not None and first_n == 1:
    return tf.print(op, [tf.timestamp(), op], message=prefix, first_n=1)

  counter = tf.Variable(tf.zeros(shape=(), dtype=tf.int32) - 1,
                        aggregation=tf.VariableAggregation.MEAN)
  increment = tf.compat.v1.assign_add(counter, 1, use_locking=True)
  return tf.cond(
      pred=tf.equal(tf.math.mod(increment, every_n), 0),
      true_fn=lambda :tf.print(op, [tf.timestamp(), op], message=prefix,
                       first_n=first_n),
      false_fn=lambda :op
  )
//  [python]
def print_tensor(x, message=''):
    """Prints `message` and the tensor value when evaluated.

    # Arguments
        x: Tensor to print.
        message: Message to print jointly with the tensor.

    # Returns
        The same tensor `x`, unchanged.
    """
    return tf.Print(x, [x], message)
//  [python]
def print_tensor(x, message=''):
    """Prints `message` and the tensor value when evaluated.

    # Arguments
        x: Tensor to print.
        message: Message to print jointly with the tensor.

    # Returns
        The same tensor `x`, unchanged.
    """
    return tf.print(x, message)
//  [python]
def _update_policy(self, observ, action, old_mean, old_logstd, reward, length):
    """Perform multiple update steps of the policy.

    The advantage is computed once at the beginning and shared across
    iterations. We need to decide for the summary of one iteration, and thus
    choose the one after half of the iterations.

    Args:
      observ: Sequences of observations.
      action: Sequences of actions.
      old_mean: Sequences of action means of the behavioral policy.
      old_logstd: Sequences of action log stddevs of the behavioral policy.
      reward: Sequences of rewards.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    with tf.name_scope('update_policy'):
      return_ = utility.discounted_return(reward, length, self._config.discount)
      value = self._network(observ, length).value
      if self._config.gae_lambda:
        advantage = utility.lambda_return(reward, value, length, self._config.discount,
                                          self._config.gae_lambda)
      else:
        advantage = return_ - value
      mean, variance = tf.nn.moments(advantage, axes=[0, 1], keep_dims=True)
      advantage = (advantage - mean) / (tf.sqrt(variance) + 1e-8)
      advantage = tf.Print(
          advantage, [tf.reduce_mean(return_), tf.reduce_mean(value)], 'return and value: ')
      advantage = tf.Print(advantage, [tf.reduce_mean(advantage)], 'normalized advantage: ')
      # pylint: disable=g-long-lambda
      loss, summary = tf.scan(lambda _1, _2: self._update_policy_step(
          observ, action, old_mean, old_logstd, advantage, length),
                              tf.range(self._config.update_epochs_policy), [0., ''],
                              parallel_iterations=1)
      print_loss = tf.Print(0, [tf.reduce_mean(loss)], 'policy loss: ')
      with tf.control_dependencies([loss, print_loss]):
        return summary[self._config.update_epochs_policy // 2]
//  [python]
def _update_policy(self, observ, action, old_mean, old_logstd, reward, length):
    """Perform multiple update steps of the policy.

    The advantage is computed once at the beginning and shared across
    iterations. We need to decide for the summary of one iteration, and thus
    choose the one after half of the iterations.

    Args:
      observ: Sequences of observations.
      action: Sequences of actions.
      old_mean: Sequences of action means of the behavioral policy.
      old_logstd: Sequences of action log stddevs of the behavioral policy.
      reward: Sequences of rewards.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    with tf.name_scope('update_policy'):
      return_ = utility.discounted_return(reward, length, self._config.discount)
      value = self._network(observ, length).value
      if self._config.gae_lambda:
        advantage = utility.lambda_return(reward, value, length, self._config.discount,
                                          self._config.gae_lambda)
      else:
        advantage = return_ - value
      mean, variance = tf.nn.moments(advantage, axes=[0, 1], keep_dims=True)
      advantage = (advantage - mean) / (tf.sqrt(variance) + 1e-8)
      advantage = tf.print(
          advantage, [tf.reduce_mean(return_), tf.reduce_mean(value)], 'return and value: ')
      advantage = tf.print(advantage, [tf.reduce_mean(advantage)], 'normalized advantage: ')
      # pylint: disable=g-long-lambda
      loss, summary = tf.scan(lambda _1, _2: self._update_policy_step(
          observ, action, old_mean, old_logstd, advantage, length),
                              tf.range(self._config.update_epochs_policy), [0., ''],
                              parallel_iterations=1)
      print_loss = tf.print(0, [tf.reduce_mean(loss)], 'policy loss: ')
      with tf.control_dependencies([loss, print_loss]):
        return summary[self._config.update_epochs_policy // 2]
//  [python]
def simple_tf_print(tensor, places=100):
    return tf.Print(tensor, [tensor, tf.shape(tensor)], summarize=places)
//  [python]
def simple_tf_print(tensor, places=100):
    return tf.print(tensor, [tensor, tf.shape(tensor)], summarize=places)
//  [python]
def _adjust_penalty(self, observ, old_mean, old_logstd, length):
    """Adjust the KL policy between the behavioral and current policy.

    Compute how much the policy actually changed during the multiple
    update steps. Adjust the penalty strength for the next training phase if we
    overshot or undershot the target divergence too much.

    Args:
      observ: Sequences of observations.
      old_mean: Sequences of action means of the behavioral policy.
      old_logstd: Sequences of action log stddevs of the behavioral policy.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    with tf.name_scope('adjust_penalty'):
      network = self._network(observ, length)
      assert_change = tf.assert_equal(tf.reduce_all(tf.equal(network.mean, old_mean)),
                                      False,
                                      message='policy should change')
      print_penalty = tf.Print(0, [self._penalty], 'current penalty: ')
      with tf.control_dependencies([assert_change, print_penalty]):
        kl_change = tf.reduce_mean(
            self._mask(utility.diag_normal_kl(old_mean, old_logstd, network.mean, network.logstd),
                       length))
        kl_change = tf.Print(kl_change, [kl_change], 'kl change: ')
        maybe_increase = tf.cond(
            kl_change > 1.3 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.Print(self._penalty.assign(self._penalty * 1.5), [0], 'increase penalty '),
            float)
        maybe_decrease = tf.cond(
            kl_change < 0.7 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.Print(self._penalty.assign(self._penalty / 1.5), [0], 'decrease penalty '),
            float)
      with tf.control_dependencies([maybe_increase, maybe_decrease]):
        return tf.summary.merge([
            tf.summary.scalar('kl_change', kl_change),
            tf.summary.scalar('penalty', self._penalty)
        ])
//  [python]
def _adjust_penalty(self, observ, old_mean, old_logstd, length):
    """Adjust the KL policy between the behavioral and current policy.

    Compute how much the policy actually changed during the multiple
    update steps. Adjust the penalty strength for the next training phase if we
    overshot or undershot the target divergence too much.

    Args:
      observ: Sequences of observations.
      old_mean: Sequences of action means of the behavioral policy.
      old_logstd: Sequences of action log stddevs of the behavioral policy.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    with tf.name_scope('adjust_penalty'):
      network = self._network(observ, length)
      assert_change = tf.assert_equal(tf.reduce_all(tf.equal(network.mean, old_mean)),
                                      False,
                                      message='policy should change')
      print_penalty = tf.print(self._penalty, 'current penalty: ')
      with tf.control_dependencies([assert_change, print_penalty]):
        kl_change = tf.reduce_mean(
            self._mask(utility.diag_normal_kl(old_mean, old_logstd, network.mean, network.logstd),
                       length))
        kl_change = tf.print(kl_change, 'kl change: ')
        maybe_increase = tf.cond(
            kl_change > 1.3 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.print(self._penalty.assign(self._penalty * 1.5), 'increase penalty '),
            float)
        maybe_decrease = tf.cond(
            kl_change < 0.7 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.print(self._penalty.assign(self._penalty / 1.5), 'decrease penalty '),
            float)
      with tf.control_dependencies([maybe_increase, maybe_decrease]):
        return tf.summary.merge([
            tf.summary.scalar('kl_change', kl_change),
            tf.summary.scalar('penalty', self._penalty)
        ])
//  [python]
def tfprint(tensor, fun=None, prefix=""):
    if fun is None:
        fun = lambda x: x
    return tf.Print(tensor, [fun(tensor)], prefix)
//  [python]
def tfprint(tensor, fun=None, prefix=""):
    if fun is None:
        fun = lambda x: x
    return tf.print(tensor, [fun(tensor)], prefix)
//  [python]
def tfprints(tensors, fun=None, prefix=""):
    if fun is None:
        fun = lambda x: x
    prints = []
    for i in range(0, len(tensors)):
        prints.append(tf.Print(tensors[i], [fun(tensors[i])], prefix))
    return prints
//  [python]
def tfprints(tensors, fun=None, prefix=""):
    if fun is None:
        fun = lambda x: x
    prints = []
    for i in range(0, len(tensors)):
        prints.append(tf.print(tensors[i], [fun(tensors[i])], prefix))
    return prints