jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def testPolicy(self):
    agent = bern_ts_agent.BernoulliThompsonSamplingAgent(
        self._time_step_spec, self._action_spec, batch_size=2
    )
    observations = tf.constant([[1, 1]], dtype=tf.float32)
    time_steps = ts.restart(observations, batch_size=2)
    policy = agent.policy
    action_step = policy.action(time_steps)
    # Batch size 2.
    self.assertAllEqual([2], action_step.action.shape)
    self.evaluate(tf.compat.v1.initialize_all_variables())
    self.assertEqual(action_step.action.shape.as_list(), [2])
    self.assertEqual(action_step.action.dtype, tf.int32)
//  [python]
def testPolicy(self):
    agent = bern_ts_agent.BernoulliThompsonSamplingAgent(
        self._time_step_spec, self._action_spec, batch_size=2
    )
    observations = tf.constant([[1, 1]], dtype=tf.float32)
    time_steps = ts.restart(observations, batch_size=2)
    policy = agent.policy
    action_step = policy.action(time_steps)
    # Batch size 2.
    self.assertAllEqual([2], action_step.action.shape)
    self.evaluate(tf.compat.v1.global_variables_initializer())
    self.assertEqual(action_step.action.shape.as_list(), [2])
    self.assertEqual(action_step.action.dtype, tf.int32)
//  [python]
def testRefIdentity(self):
    with self.test_session():
      v = tf.Variable(7)

      v = control_flow_ops._Identity(v)
      op = tf.assign(v, 9)
      v2 = control_flow_ops.with_dependencies([op], v)

      self.assertTrue(check_op_order(v.graph))
      self.assertTrue(isinstance(v2, tf.Tensor))
      tf.initialize_all_variables().run()
      self.assertEqual(9, v2.eval())
//  [python]
def testRefIdentity(self):
    with self.test_session():
      v = tf.Variable(7)

      v = control_flow_ops._Identity(v)
      op = tf.assign(v, 9)
      v2 = control_flow_ops.with_dependencies([op], v)

      self.assertTrue(check_op_order(v.graph))
      self.assertTrue(isinstance(v2, tf.Tensor))
      tf.global_variables_initializer().run()
      self.assertEqual(9, v2.eval())
//  [python]
def testRefSwitch(self):
    with self.test_session():
      v = tf.Variable(7)

      p = tf.constant(True)
      v1 = control_flow_ops._SwitchRefOrTensor(v.ref(), p)
      v2 = tf.assign(v1[1], 9)
      tf.initialize_all_variables().run()
      self.assertEqual(9, v2.eval())
//  [python]
def testRefSwitch(self):
    with self.test_session():
      v = tf.Variable(7)

      p = tf.constant(True)
      v1 = control_flow_ops._SwitchRefOrTensor(v.ref(), p)
      v2 = tf.assign(v1[1], 9)
      tf.global_variables_initializer().run()
      self.assertEqual(9, v2.eval())
//  [python]
def testRefEnter(self):
    with self.test_session():
      v = tf.Variable(7)

      enter_v = control_flow_ops._Enter(v, "foo_1", is_constant=True)
      nine = tf.constant(9)
      enter_nine = control_flow_ops.enter(nine, "foo_1")
      op = tf.assign(enter_v, enter_nine)
      v2 = control_flow_ops.with_dependencies([op], enter_v)
      v3 = control_flow_ops.exit(v2)
      tf.initialize_all_variables().run()
      self.assertEqual(9, v3.eval())
//  [python]
def testRefEnter(self):
    with self.test_session():
      v = tf.Variable(7)

      enter_v = control_flow_ops._Enter(v, "foo_1", is_constant=True)
      nine = tf.constant(9)
      enter_nine = control_flow_ops.enter(nine, "foo_1")
      op = tf.assign(enter_v, enter_nine)
      v2 = control_flow_ops.with_dependencies([op], enter_v)
      v3 = control_flow_ops.exit(v2)
      tf.global_variables_initializer().run()
      self.assertEqual(9, v3.eval())
//  [python]
def testCond_6(self):
    with self.test_session():
      v1 = tf.Variable([7])

      age = tf.constant(3)
      pred = tf.greater(age, 4)
      fn1 = lambda: age
      fn2 = lambda: v1
      r = tf.cond(pred, fn1, fn2)

      tf.initialize_all_variables().run()
      result = r.eval()
      self.assertAllEqual(np.array([7]), result)
//  [python]
def testCond_6(self):
    with self.test_session():
      v1 = tf.Variable([7])

      age = tf.constant(3)
      pred = tf.greater(age, 4)
      fn1 = lambda: age
      fn2 = lambda: v1
      r = tf.cond(pred, fn1, fn2)

      tf.global_variables_initializer().run()
      result = r.eval()
      self.assertAllEqual(np.array([7]), result)
//  [python]
def testTrainAgent(self):
    observations = np.array([[1, 1]], dtype=np.float32)
    actions = np.array([0, 1], dtype=np.int32)
    rewards = np.array([0.0, 1.0], dtype=np.float32)
    initial_step, final_step = _get_initial_and_final_steps(
        observations, rewards
    )
    action_step = _get_action_step(actions)
    experience = _get_experience(initial_step, action_step, final_step)

    agent = bern_ts_agent.BernoulliThompsonSamplingAgent(
        self._time_step_spec, self._action_spec, batch_size=2
    )
    init_op = agent.initialize()
    if not tf.executing_eagerly():
      with self.cached_session() as sess:
        common.initialize_uninitialized_variables(sess)
        self.assertIsNone(sess.run(init_op))
    loss, _ = agent._train(experience, weights=None)
    self.evaluate(tf.compat.v1.initialize_all_variables())
    # The loss is -sum(rewards).
    self.assertAllClose(self.evaluate(loss), -1.0)
//  [python]
def testTrainAgent(self):
    observations = np.array([[1, 1]], dtype=np.float32)
    actions = np.array([0, 1], dtype=np.int32)
    rewards = np.array([0.0, 1.0], dtype=np.float32)
    initial_step, final_step = _get_initial_and_final_steps(
        observations, rewards
    )
    action_step = _get_action_step(actions)
    experience = _get_experience(initial_step, action_step, final_step)

    agent = bern_ts_agent.BernoulliThompsonSamplingAgent(
        self._time_step_spec, self._action_spec, batch_size=2
    )
    init_op = agent.initialize()
    if not tf.executing_eagerly():
      with self.cached_session() as sess:
        common.initialize_uninitialized_variables(sess)
        self.assertIsNone(sess.run(init_op))
    loss, _ = agent._train(experience, weights=None)
    self.evaluate(tf.compat.v1.global_variables_initializer())
    # The loss is -sum(rewards).
    self.assertAllClose(self.evaluate(loss), -1.0)
//  [python]
def testTrainAgentWithMask(self):
    time_step_spec = ts.time_step_spec((
        tensor_spec.TensorSpec([], tf.float32),
        tensor_spec.TensorSpec([3], tf.int32),
    ))
    agent = bern_ts_agent.BernoulliThompsonSamplingAgent(
        time_step_spec, self._action_spec, batch_size=2
    )
    observations = (
        np.array([1, 1], dtype=np.float32),
        np.array([[0, 0, 1], [0, 0, 1]], dtype=np.int32),
    )
    actions = np.array([0, 1], dtype=np.int32)
    rewards = np.array([1.0, 0.0], dtype=np.float32)
    initial_step, final_step = _get_initial_and_final_steps_with_action_mask(
        observations, rewards
    )
    action_step = _get_action_step(actions)
    experience = _get_experience(initial_step, action_step, final_step)
    loss, _ = agent.train(experience, None)
    self.evaluate(tf.compat.v1.initialize_all_variables())
    self.assertAllClose(self.evaluate(loss), -1.0)
//  [python]
def testTrainAgentWithMask(self):
    time_step_spec = ts.time_step_spec((
        tensor_spec.TensorSpec([], tf.float32),
        tensor_spec.TensorSpec([3], tf.int32),
    ))
    agent = bern_ts_agent.BernoulliThompsonSamplingAgent(
        time_step_spec, self._action_spec, batch_size=2
    )
    observations = (
        np.array([1, 1], dtype=np.float32),
        np.array([[0, 0, 1], [0, 0, 1]], dtype=np.int32),
    )
    actions = np.array([0, 1], dtype=np.int32)
    rewards = np.array([1.0, 0.0], dtype=np.float32)
    initial_step, final_step = _get_initial_and_final_steps_with_action_mask(
        observations, rewards
    )
    action_step = _get_action_step(actions)
    experience = _get_experience(initial_step, action_step, final_step)
    loss, _ = agent.train(experience, None)
    self.evaluate(tf.compat.v1.global_variables_initializer())
    self.assertAllClose(self.evaluate(loss), -1.0)
//  [python]
def testCond_4(self):
    with self.test_session():
      v1 = tf.Variable(7)
      v2 = tf.Variable(7)
      v3 = tf.Variable(7)

      age = tf.constant(3)
      max_age = tf.constant(2)
      pred = tf.greater(age, max_age)
      fn1 = lambda: [tf.assign(v1, 1).op, tf.assign(v2, 2).op]
      fn2 = lambda: [tf.assign(v3, 3).op, tf.constant(10).op]
      r = tf.cond(pred, fn1, fn2)

      tf.initialize_all_variables().run()
      self.assertEqual(len(r), 2)
      result = r[1].eval()
      self.assertTrue(check_op_order(age.graph))
      self.assertAllEqual(True, result)
      self.assertAllEqual(7, v1.eval())
      self.assertAllEqual(2, v2.eval())
      self.assertAllEqual(7, v3.eval())
//  [python]
def testCond_4(self):
    with self.test_session():
      v1 = tf.Variable(7)
      v2 = tf.Variable(7)
      v3 = tf.Variable(7)

      age = tf.constant(3)
      max_age = tf.constant(2)
      pred = tf.greater(age, max_age)
      fn1 = lambda: [tf.assign(v1, 1).op, tf.assign(v2, 2).op]
      fn2 = lambda: [tf.assign(v3, 3).op, tf.constant(10).op]
      r = tf.cond(pred, fn1, fn2)

      tf.global_variables_initializer().run()
      self.assertEqual(len(r), 2)
      result = r[1].eval()
      self.assertTrue(check_op_order(age.graph))
      self.assertAllEqual(True, result)
      self.assertAllEqual(7, v1.eval())
      self.assertAllEqual(2, v2.eval())
      self.assertAllEqual(7, v3.eval())
//  [python]
def testWhileWithRefs_1(self):
    with self.test_session() as sess:
      x = tf.Variable(0).ref()
      i = tf.constant(0)
      c = lambda i, x: tf.less(i, 100)

      self.assertEqual(x.dtype, tf.int32_ref)

      def b(i, x):
        self.assertEqual(x.dtype, tf.int32_ref)
        return (i+1, gen_array_ops._ref_identity(x))

      r = tf.while_loop(c, b, [i, x], parallel_iterations=5)

      tf.initialize_all_variables().run()

      self.assertEqual(r[0].dtype, tf.int32)
      self.assertEqual(r[1].dtype, tf.int32_ref)

      value_i, value_x = sess.run(r)

    self.assertEqual(100, value_i)
    self.assertEqual(0, value_x)
//  [python]
def testWhileWithRefs_1(self):
    with self.test_session() as sess:
      x = tf.Variable(0).ref()
      i = tf.constant(0)
      c = lambda i, x: tf.less(i, 100)

      self.assertEqual(x.dtype, tf.int32_ref)

      def b(i, x):
        self.assertEqual(x.dtype, tf.int32_ref)
        return (i+1, gen_array_ops._ref_identity(x))

      r = tf.while_loop(c, b, [i, x], parallel_iterations=5)

      tf.global_variables_initializer().run()

      self.assertEqual(r[0].dtype, tf.int32)
      self.assertEqual(r[1].dtype, tf.int32_ref)

      value_i, value_x = sess.run(r)

    self.assertEqual(100, value_i)
    self.assertEqual(0, value_x)
//  [python]
def testWhileCondExitControl(self):
    with self.test_session():
      v = tf.Variable(1)
      def false_branch():
        cond = lambda i: i < 100
        def body(i):
          x = tf.assign(v, i)
          return x + 1
        loop = tf.while_loop(cond, body, [0])
        # Make sure to handle correctly control edge from Exit to a node.
        with tf.control_dependencies([loop]):
          return tf.constant(6.0)
      r = tf.cond(tf.constant(False),
                  lambda: tf.constant(1.0),
                  false_branch)
      tf.initialize_all_variables().run()
      self.assertEqual(6.0, r.eval())
      self.assertEqual(99, v.eval())
//  [python]
def testWhileCondExitControl(self):
    with self.test_session():
      v = tf.Variable(1)
      def false_branch():
        cond = lambda i: i < 100
        def body(i):
          x = tf.assign(v, i)
          return x + 1
        loop = tf.while_loop(cond, body, [0])
        # Make sure to handle correctly control edge from Exit to a node.
        with tf.control_dependencies([loop]):
          return tf.constant(6.0)
      r = tf.cond(tf.constant(False),
                  lambda: tf.constant(1.0),
                  false_branch)
      tf.global_variables_initializer().run()
      self.assertEqual(6.0, r.eval())
      self.assertEqual(99, v.eval())
//  [python]
def testWhileUpdateVariable_1(self):
    with self.test_session():
      select = tf.Variable([3.0, 4.0, 5.0])
      n = tf.constant(0)

      def loop_iterator(j):
        return tf.less(j, 3)

      def loop_body(j):
        ns = tf.scatter_update(select, j, 10.0)
        nj = tf.add(j, 1)
        op = control_flow_ops.group(ns)
        nj = control_flow_ops.with_dependencies([op], nj)
        return [nj]

      r = tf.while_loop(loop_iterator, loop_body, [n],
                        parallel_iterations=1)
      self.assertTrue(check_op_order(n.graph))
      tf.initialize_all_variables().run()
      self.assertEqual(3, r.eval())
      result = select.eval()
      self.assertAllClose(np.array([10.0, 10.0, 10.0]), result)
//  [python]
def testWhileUpdateVariable_1(self):
    with self.test_session():
      select = tf.Variable([3.0, 4.0, 5.0])
      n = tf.constant(0)

      def loop_iterator(j):
        return tf.less(j, 3)

      def loop_body(j):
        ns = tf.scatter_update(select, j, 10.0)
        nj = tf.add(j, 1)
        op = control_flow_ops.group(ns)
        nj = control_flow_ops.with_dependencies([op], nj)
        return [nj]

      r = tf.while_loop(loop_iterator, loop_body, [n],
                        parallel_iterations=1)
      self.assertTrue(check_op_order(n.graph))
      tf.global_variables_initializer().run()
      self.assertEqual(3, r.eval())
      result = select.eval()
      self.assertAllClose(np.array([10.0, 10.0, 10.0]), result)
//  [python]
def testWhileUpdateVariable_3(self):
    with self.test_session():
      select = tf.Variable([3.0, 4.0, 5.0])
      n = tf.constant(0)

      def loop_iterator(j, _):
        return tf.less(j, 3)

      def loop_body(j, _):
        ns = tf.scatter_update(select, j, 10.0)
        nj = tf.add(j, 1)
        return [nj, ns]

      r = tf.while_loop(loop_iterator, loop_body,
                        [n, tf.identity(select)],
                        parallel_iterations=1)
      tf.initialize_all_variables().run()
      result = r[1].eval()
    self.assertTrue(check_op_order(n.graph))
    self.assertAllClose(np.array([10.0, 10.0, 10.0]), result)
//  [python]
def testWhileUpdateVariable_3(self):
    with self.test_session():
      select = tf.Variable([3.0, 4.0, 5.0])
      n = tf.constant(0)

      def loop_iterator(j, _):
        return tf.less(j, 3)

      def loop_body(j, _):
        ns = tf.scatter_update(select, j, 10.0)
        nj = tf.add(j, 1)
        return [nj, ns]

      r = tf.while_loop(loop_iterator, loop_body,
                        [n, tf.identity(select)],
                        parallel_iterations=1)
      tf.global_variables_initializer().run()
      result = r[1].eval()
    self.assertTrue(check_op_order(n.graph))
    self.assertAllClose(np.array([10.0, 10.0, 10.0]), result)
//  [python]
def testWhileUpdateVariable_4(self):
    with self.test_session():
      var_a = tf.Variable(0, name="a")
      var_b = tf.Variable(0, name="b")
      tf.initialize_all_variables().run()

      c = tf.constant(0, name="c")
      asn1 = tf.assign_add(var_a, 1, name="a_add")
      # Loop condition
      def pred(i):
        return tf.less(i, 10)
      # Loop body
      def loop_body(i):
        asn2 = tf.assign_add(var_b, asn1, name="b_add")
        with tf.control_dependencies([asn2]):
          ni = tf.add(i, 1, name="i_add")
        return ni

      lpa = tf.while_loop(pred, loop_body, [c],
                          parallel_iterations=1)

      self.assertEqual(0, var_b.eval())
      lpa.eval()  # Run the loop
      self.assertEqual(10, var_b.eval())
//  [python]
def testWhileUpdateVariable_4(self):
    with self.test_session():
      var_a = tf.Variable(0, name="a")
      var_b = tf.Variable(0, name="b")
      tf.global_variables_initializer().run()

      c = tf.constant(0, name="c")
      asn1 = tf.assign_add(var_a, 1, name="a_add")
      # Loop condition
      def pred(i):
        return tf.less(i, 10)
      # Loop body
      def loop_body(i):
        asn2 = tf.assign_add(var_b, asn1, name="b_add")
        with tf.control_dependencies([asn2]):
          ni = tf.add(i, 1, name="i_add")
        return ni

      lpa = tf.while_loop(pred, loop_body, [c],
                          parallel_iterations=1)

      self.assertEqual(0, var_b.eval())
      lpa.eval()  # Run the loop
      self.assertEqual(10, var_b.eval())
//  [python]
def testWhileUpdateVariable_5(self):
    with self.test_session():
      # Create some variables.
      var_a = tf.Variable(0, name="a")
      var_b = tf.Variable(0, name="b")
      tf.initialize_all_variables().run()

      # Change condition to check var_b
      def pred(_):
        return tf.less(var_b, 10)

      # Change body to increment var_b
      def loop_body(i):
        asn1 = tf.assign_add(var_a, tf.constant(1), name="a_add")
        asn2 = tf.assign_add(var_b, tf.constant(1), name="b_add")
        with tf.control_dependencies([asn1, asn2]):
          inc_b = tf.identity(var_b)
        return inc_b

      lpa = tf.while_loop(pred, loop_body, [var_b], parallel_iterations=1,
                          name="loop")

      self.assertEqual(0, var_b.eval())
      lpa.eval()  # Run the loop
      self.assertEqual(10, var_a.eval())
      self.assertEqual(10, var_b.eval())
//  [python]
def testWhileUpdateVariable_5(self):
    with self.test_session():
      # Create some variables.
      var_a = tf.Variable(0, name="a")
      var_b = tf.Variable(0, name="b")
      tf.global_variables_initializer().run()

      # Change condition to check var_b
      def pred(_):
        return tf.less(var_b, 10)

      # Change body to increment var_b
      def loop_body(i):
        asn1 = tf.assign_add(var_a, tf.constant(1), name="a_add")
        asn2 = tf.assign_add(var_b, tf.constant(1), name="b_add")
        with tf.control_dependencies([asn1, asn2]):
          inc_b = tf.identity(var_b)
        return inc_b

      lpa = tf.while_loop(pred, loop_body, [var_b], parallel_iterations=1,
                          name="loop")

      self.assertEqual(0, var_b.eval())
      lpa.eval()  # Run the loop
      self.assertEqual(10, var_a.eval())
      self.assertEqual(10, var_b.eval())
//  [python]
def testWhileUpdateVariable_2(self):
    with self.test_session():
      select1 = tf.Variable([3.0, 4.0, 5.0])
      select2 = tf.Variable([3.0, 4.0, 5.0])
      n = tf.constant(0)

      def loop_iterator(j):
        return tf.less(j, 3)

      def loop_body(j):
        ns1 = tf.scatter_update(select1, j, 10.0)
        ns2 = tf.scatter_update(select2, j, 10.0)
        nj = tf.add(j, 1)
        op = control_flow_ops.group(ns1, ns2)
        nj = control_flow_ops.with_dependencies([op], nj)
        return [nj]

      r = tf.while_loop(loop_iterator, loop_body, [n],
                        parallel_iterations=1)
      self.assertTrue(check_op_order(n.graph))
      tf.initialize_all_variables().run()
      self.assertEqual(3, r.eval())
      result1 = select1.eval()
      self.assertAllClose(np.array([10.0, 10.0, 10.0]), result1)
      result2 = select2.eval()
      self.assertAllClose(np.array([10.0, 10.0, 10.0]), result2)
//  [python]
def testWhileUpdateVariable_2(self):
    with self.test_session():
      select1 = tf.Variable([3.0, 4.0, 5.0])
      select2 = tf.Variable([3.0, 4.0, 5.0])
      n = tf.constant(0)

      def loop_iterator(j):
        return tf.less(j, 3)

      def loop_body(j):
        ns1 = tf.scatter_update(select1, j, 10.0)
        ns2 = tf.scatter_update(select2, j, 10.0)
        nj = tf.add(j, 1)
        op = control_flow_ops.group(ns1, ns2)
        nj = control_flow_ops.with_dependencies([op], nj)
        return [nj]

      r = tf.while_loop(loop_iterator, loop_body, [n],
                        parallel_iterations=1)
      self.assertTrue(check_op_order(n.graph))
      tf.global_variables_initializer().run()
      self.assertEqual(3, r.eval())
      result1 = select1.eval()
      self.assertAllClose(np.array([10.0, 10.0, 10.0]), result1)
      result2 = select2.eval()
      self.assertAllClose(np.array([10.0, 10.0, 10.0]), result2)
//  [python]
def testWhileGrad_Variable(self):
    with self.test_session():
      a = tf.Variable(3.0)
      v = tf.constant(2.0, name="v")
      c = lambda v: tf.less(v, 100.0)
      b = lambda v: tf.mul(v, a)
      r = tf.while_loop(c, b, [v], parallel_iterations=1)

      r = tf.gradients(r, a)
      tf.initialize_all_variables().run()
      self.assertAllClose(216.0, r[0].eval())
//  [python]
def testWhileGrad_Variable(self):
    with self.test_session():
      a = tf.Variable(3.0)
      v = tf.constant(2.0, name="v")
      c = lambda v: tf.less(v, 100.0)
      b = lambda v: tf.mul(v, a)
      r = tf.while_loop(c, b, [v], parallel_iterations=1)

      r = tf.gradients(r, a)
      tf.global_variables_initializer().run()
      self.assertAllClose(216.0, r[0].eval())
//  [python]
def testWhileUpdateVariable_6(self):
    with self.test_session():
      # Create some variables.
      var_a = tf.Variable(0, name="a")
      var_b = tf.Variable(0, name="b")
      c = tf.constant(0)
      tf.initialize_all_variables().run()

      # Loop condition
      def pred(i):
        return tf.less(i, 10)

      # Loop body
      def loop_body(i):
        asn1 = tf.assign_add(var_a, 1, name="a_add")
        with tf.control_dependencies([asn1]):
          asn2 = tf.assign_add(var_b, var_a, name="b_add")
        with tf.control_dependencies([asn2]):
          ni = tf.add(i, 1, name="i_add")
          return ni

      lpa = tf.while_loop(pred, loop_body, [c], parallel_iterations=1,
                          name="loop")

      self.assertEqual(0, var_b.eval())
      lpa.eval()  # Run the loop
      self.assertEqual(55, var_b.eval())
      self.assertEqual(10, var_a.eval())
//  [python]
def testWhileUpdateVariable_6(self):
    with self.test_session():
      # Create some variables.
      var_a = tf.Variable(0, name="a")
      var_b = tf.Variable(0, name="b")
      c = tf.constant(0)
      tf.global_variables_initializer().run()

      # Loop condition
      def pred(i):
        return tf.less(i, 10)

      # Loop body
      def loop_body(i):
        asn1 = tf.assign_add(var_a, 1, name="a_add")
        with tf.control_dependencies([asn1]):
          asn2 = tf.assign_add(var_b, var_a, name="b_add")
        with tf.control_dependencies([asn2]):
          ni = tf.add(i, 1, name="i_add")
          return ni

      lpa = tf.while_loop(pred, loop_body, [c], parallel_iterations=1,
                          name="loop")

      self.assertEqual(0, var_b.eval())
      lpa.eval()  # Run the loop
      self.assertEqual(55, var_b.eval())
      self.assertEqual(10, var_a.eval())
//  [python]
def testWhileGrad_NoDependency(self):
    with self.test_session() as sess:
      variable = tf.Variable(tf.ones([2, 3]))
      time = tf.zeros([], dtype=tf.int32)
      def cond(time, tensor, _):
        return time < 10
      def body(time, tensor, _):
        return (time+1, tensor, tensor)
      loop_vars = [time, variable, variable]
      tensors = tf.while_loop(cond=cond, body=body, loop_vars=loop_vars)
      cost = tf.reduce_sum(tensors[2])
      grad = tf.gradients(cost, [variable])
      tf.initialize_all_variables().run()
      self.assertAllClose(np.ones([2, 3]), sess.run(grad[0]))
//  [python]
def testWhileGrad_NoDependency(self):
    with self.test_session() as sess:
      variable = tf.Variable(tf.ones([2, 3]))
      time = tf.zeros([], dtype=tf.int32)
      def cond(time, tensor, _):
        return time < 10
      def body(time, tensor, _):
        return (time+1, tensor, tensor)
      loop_vars = [time, variable, variable]
      tensors = tf.while_loop(cond=cond, body=body, loop_vars=loop_vars)
      cost = tf.reduce_sum(tensors[2])
      grad = tf.gradients(cost, [variable])
      tf.global_variables_initializer().run()
      self.assertAllClose(np.ones([2, 3]), sess.run(grad[0]))
//  [python]
def testNestedWhileGrad_ParallelIterations(self):
    # Make sure the stack pushes and pops of an inner loop are executed in
    # the sequential order of the iterations of its outer loop.
    with self.test_session() as sess:
      def inner_loop(t):
        fn = lambda n: n + tf.square(var)
        return tf.map_fn(fn=fn, elems=t, parallel_iterations=10)

      def outer_loop(inp):
        return tf.map_fn(fn=inner_loop, elems=inp, parallel_iterations=10)

      var = tf.Variable(tf.constant(3.0))
      inp = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
      res = outer_loop(inp)
      optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
      train_op = optimizer.minimize(tf.reduce_mean(tf.square(res)))
      sess.run(tf.initialize_all_variables())
      sess.run(train_op)
      self.assertAllClose(2.999, var.eval())
//  [python]
def testNestedWhileGrad_ParallelIterations(self):
    # Make sure the stack pushes and pops of an inner loop are executed in
    # the sequential order of the iterations of its outer loop.
    with self.test_session() as sess:
      def inner_loop(t):
        fn = lambda n: n + tf.square(var)
        return tf.map_fn(fn=fn, elems=t, parallel_iterations=10)

      def outer_loop(inp):
        return tf.map_fn(fn=inner_loop, elems=inp, parallel_iterations=10)

      var = tf.Variable(tf.constant(3.0))
      inp = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
      res = outer_loop(inp)
      optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
      train_op = optimizer.minimize(tf.reduce_mean(tf.square(res)))
      sess.run(tf.global_variables_initializer())
      sess.run(train_op)
      self.assertAllClose(2.999, var.eval())
//  [python]
def testWhileGrad_Concat(self):
    with self.test_session() as sess:
      x = tf.get_variable("x", initializer=[[1., 2.]])
      i0 = tf.constant(0)
      h0 = tf.zeros([0, 2])

      def condition(i, _):
        return i < 2

      def body(i, h):
        return i+1, tf.concat(0, [h, x])

      _, h = tf.while_loop(
          condition, body, [i0, h0],
          [i0.get_shape(), tensor_shape.TensorShape([None, 2])])
      s = tf.reduce_sum(h)

      sess.run(tf.initialize_all_variables())
      optimizer = tf.train.GradientDescentOptimizer(0.01)
      op = optimizer.minimize(s)
      sess.run(op)
      self.assertAllClose([[0.98000002, 1.98000002]], sess.run(x))
//  [python]
def testWhileGrad_Concat(self):
    with self.test_session() as sess:
      x = tf.get_variable("x", initializer=[[1., 2.]])
      i0 = tf.constant(0)
      h0 = tf.zeros([0, 2])

      def condition(i, _):
        return i < 2

      def body(i, h):
        return i+1, tf.concat(0, [h, x])

      _, h = tf.while_loop(
          condition, body, [i0, h0],
          [i0.get_shape(), tensor_shape.TensorShape([None, 2])])
      s = tf.reduce_sum(h)

      sess.run(tf.global_variables_initializer())
      optimizer = tf.train.GradientDescentOptimizer(0.01)
      op = optimizer.minimize(s)
      sess.run(op)
      self.assertAllClose([[0.98000002, 1.98000002]], sess.run(x))
//  [python]
def testOneOpCond(self):
    with self.test_session():
      v = tf.Variable(0)
      c = tf.convert_to_tensor(0)
      one = tf.convert_to_tensor(1)
      two = tf.convert_to_tensor(2)
      p = tf.greater_equal(c, 1)

      def a():
        return tf.assign(v, one)

      def b():
        return tf.assign(v, two)

      i = tf.cond(p, a, b)
      self.assertTrue(isinstance(i, tf.Tensor))
      tf.initialize_all_variables().run()

      self.assertEqual(0, v.eval())

      # True case: c = 2 is >= 1, v is set to 1.
      self.assertEqual(1, i.eval(feed_dict={c.name: 2}))
      self.assertEqual(1, v.eval())

      # False case: c = 0 is not >= 1, v is set to 2.
      self.assertEqual(2, i.eval(feed_dict={c.name: 0}))
      self.assertEqual(2, v.eval())
//  [python]
def testOneOpCond(self):
    with self.test_session():
      v = tf.Variable(0)
      c = tf.convert_to_tensor(0)
      one = tf.convert_to_tensor(1)
      two = tf.convert_to_tensor(2)
      p = tf.greater_equal(c, 1)

      def a():
        return tf.assign(v, one)

      def b():
        return tf.assign(v, two)

      i = tf.cond(p, a, b)
      self.assertTrue(isinstance(i, tf.Tensor))
      tf.global_variables_initializer().run()

      self.assertEqual(0, v.eval())

      # True case: c = 2 is >= 1, v is set to 1.
      self.assertEqual(1, i.eval(feed_dict={c.name: 2}))
      self.assertEqual(1, v.eval())

      # False case: c = 0 is not >= 1, v is set to 2.
      self.assertEqual(2, i.eval(feed_dict={c.name: 0}))
      self.assertEqual(2, v.eval())
//  [python]
def testWhileWithRefsWithGradients_1(self):
    with self.test_session() as sess:
      x = tf.Variable(0).ref()
      i = tf.constant(0)
      c = lambda i, x: tf.less(i, 10)

      self.assertEqual(x.dtype, tf.int32_ref)

      # pylint: disable=protected-access
      def body(i, x):
        self.assertEqual(x.dtype, tf.int32_ref)
        return [i+1, gen_array_ops._ref_identity(x)]
      # pylint: enable=protected-access

      r = tf.while_loop(c, body, [i, x], parallel_iterations=5)

      grad_ys = [tf.Variable(73).ref()]
      grad = tf.gradients([r[1]], [x], grad_ys=grad_ys)

      tf.initialize_all_variables().run()

      self.assertEqual(r[0].dtype, tf.int32)
      self.assertEqual(r[1].dtype, tf.int32_ref)

      value_i, value_x, value_x_grad = sess.run(r + grad)

    self.assertEqual(10, value_i)
    self.assertEqual(0, value_x)
    self.assertEqual(73, value_x_grad)
//  [python]
def testWhileWithRefsWithGradients_1(self):
    with self.test_session() as sess:
      x = tf.Variable(0).ref()
      i = tf.constant(0)
      c = lambda i, x: tf.less(i, 10)

      self.assertEqual(x.dtype, tf.int32_ref)

      # pylint: disable=protected-access
      def body(i, x):
        self.assertEqual(x.dtype, tf.int32_ref)
        return [i+1, gen_array_ops._ref_identity(x)]
      # pylint: enable=protected-access

      r = tf.while_loop(c, body, [i, x], parallel_iterations=5)

      grad_ys = [tf.Variable(73).ref()]
      grad = tf.gradients([r[1]], [x], grad_ys=grad_ys)

      tf.global_variables_initializer().run()

      self.assertEqual(r[0].dtype, tf.int32)
      self.assertEqual(r[1].dtype, tf.int32_ref)

      value_i, value_x, value_x_grad = sess.run(r + grad)

    self.assertEqual(10, value_i)
    self.assertEqual(0, value_x)
    self.assertEqual(73, value_x_grad)
//  [python]
def cnn_3layer(input_kspace,w1,b1,w2,b2,w3,b3,w4,w5,Rx,Ry,Rt,sess):

    input_phdr = tf.placeholder(tf.float32, input_kspace.shape)
    w1_phdr = tf.placeholder(tf.float32, w1.shape)
    w2_phdr = tf.placeholder(tf.float32, w2.shape)
    w3_phdr = tf.placeholder(tf.float32, w3.shape)
    w4_phdr = tf.placeholder(tf.float32, w4.shape)
    w5_phdr = tf.placeholder(tf.float32, w5.shape)


    #h_conv1 = tf.nn.relu(conv3d_dilate(input_kspace, w1,Rx,Ry))
    h_conv1 = conv3d_dilate(input_phdr, w1_phdr, strides=[Rx,Ry,Rt])
    h_conv2 = conv3d_dilate(h_conv1, w2_phdr)
    #h_conv2 = tf.nn.relu(conv3d_dilate(h_conv1, w2_phdr))
    h_conv3 = tf.nn.relu(conv3d_dilate(h_conv2, w3_phdr))
    h_conv4 = conv3d_dilate(h_conv3, w4_phdr)
    h_conv5 = conv3d_dilate(h_conv4, w5_phdr)

    if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
        init = tf.initialize_all_variables()
    else:
        init = tf.global_variables_initializer()
    sess.run(init)

    return sess.run(h_conv5, feed_dict={input_phdr: input_kspace, w1_phdr: w1, w2_phdr: w2, w3_phdr: w3, w4_phdr: w4, w5_phdr: w5})
//  [python]
def cnn_3layer(input_kspace,w1,b1,w2,b2,w3,b3,w4,w5,Rx,Ry,Rt,sess):

    input_phdr = tf.placeholder(tf.float32, input_kspace.shape)
    w1_phdr = tf.placeholder(tf.float32, w1.shape)
    w2_phdr = tf.placeholder(tf.float32, w2.shape)
    w3_phdr = tf.placeholder(tf.float32, w3.shape)
    w4_phdr = tf.placeholder(tf.float32, w4.shape)
    w5_phdr = tf.placeholder(tf.float32, w5.shape)


    #h_conv1 = tf.nn.relu(conv3d_dilate(input_kspace, w1,Rx,Ry))
    h_conv1 = conv3d_dilate(input_phdr, w1_phdr, strides=[Rx,Ry,Rt])
    h_conv2 = conv3d_dilate(h_conv1, w2_phdr)
    #h_conv2 = tf.nn.relu(conv3d_dilate(h_conv1, w2_phdr))
    h_conv3 = tf.nn.relu(conv3d_dilate(h_conv2, w3_phdr))
    h_conv4 = conv3d_dilate(h_conv3, w4_phdr)
    h_conv5 = conv3d_dilate(h_conv4, w5_phdr)

    init = tf.global_variables_initializer()
    sess.run(init)

    return sess.run(h_conv5, feed_dict={input_phdr: input_kspace, w1_phdr: w1, w2_phdr: w2, w3_phdr: w3, w4_phdr: w4, w5_phdr: w5})
//  [python]
def testCaseSideEffects(self):
    with self.test_session() as sess:
      v0 = tf.Variable(-1)
      v1 = tf.Variable(-1)
      v2 = tf.Variable(-1)

      a = lambda: control_flow_ops.with_dependencies([tf.assign(v0, 0)], 0)
      b = lambda: control_flow_ops.with_dependencies([tf.assign(v1, 1)], 1)
      c = lambda: control_flow_ops.with_dependencies([tf.assign(v2, 2)], 2)

      x = tf.constant(1)
      y = tf.constant(2)

      r0 = tf.case(((x < y, a), (x > y, b)), default=c, exclusive=True)
      r1 = tf.case(((x > y, a), (x < y, b)), default=c, exclusive=True)
      r2 = tf.case(((x > y, a), (x > y, b)), default=c, exclusive=True)

      tf.initialize_all_variables().run()
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1] * 3)
      self.assertEqual(2, r2.eval())
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1, -1, 2])

      tf.initialize_all_variables().run()
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1] * 3)
      self.assertEqual(1, r1.eval())
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1, 1, -1])

      tf.initialize_all_variables().run()
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1] * 3)
      self.assertEqual(0, r0.eval())
      self.assertAllEqual(sess.run([v0, v1, v2]), [0, -1, -1])
//  [python]
def testCaseSideEffects(self):
    with self.test_session() as sess:
      v0 = tf.Variable(-1)
      v1 = tf.Variable(-1)
      v2 = tf.Variable(-1)

      a = lambda: control_flow_ops.with_dependencies([tf.assign(v0, 0)], 0)
      b = lambda: control_flow_ops.with_dependencies([tf.assign(v1, 1)], 1)
      c = lambda: control_flow_ops.with_dependencies([tf.assign(v2, 2)], 2)

      x = tf.constant(1)
      y = tf.constant(2)

      r0 = tf.case(((x < y, a), (x > y, b)), default=c, exclusive=True)
      r1 = tf.case(((x > y, a), (x < y, b)), default=c, exclusive=True)
      r2 = tf.case(((x > y, a), (x > y, b)), default=c, exclusive=True)

      tf.global_variables_initializer().run()
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1] * 3)
      self.assertEqual(2, r2.eval())
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1, -1, 2])

      tf.global_variables_initializer().run()
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1] * 3)
      self.assertEqual(1, r1.eval())
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1, 1, -1])

      tf.global_variables_initializer().run()
      self.assertAllEqual(sess.run([v0, v1, v2]), [-1] * 3)
      self.assertEqual(0, r0.eval())
      self.assertAllEqual(sess.run([v0, v1, v2]), [0, -1, -1])
//  [python]
def testTrainWithNonLegacyActorNetwork(self, feed_transition):
    if not tf.executing_eagerly():
      self.skipTest('Skipping test: sequential networks not supported in TF1')

    num_epochs = 5
    counter = common.create_variable('test_train_counter')
    action_spec = {
        'my_action': tensor_spec.BoundedTensorSpec([1], tf.float32, -1, 1)
    }

    agent = ppo_agent.PPOAgent(
        self._time_step_spec,
        action_spec,
        tf.compat.v1.train.AdamOptimizer(),
        # action_spec == TensorSpec([1], tf.float32)
        actor_net=create_sequential_actor_net(ndims=1),
        value_net=DummyValueNet(self._obs_spec),
        normalize_observations=False,
        num_epochs=num_epochs,
        initial_adaptive_kl_beta=1.0,
        adaptive_kl_target=10.0,
        adaptive_kl_tolerance=0.5,
        check_numerics=True,
        compute_value_and_advantage_in_train=False,
        train_step_counter=counter,
    )
    agent.initialize()

    experience = _create_experience_trajectory_my_action()
    experience = agent._preprocess(experience)

    if feed_transition:
      experience = trajectory.to_transition(experience)

    loss = lambda: agent.train(experience)

    # Assert that counter starts out at zero.
    self.evaluate(tf.compat.v1.initialize_all_variables())
    self.assertEqual(0, self.evaluate(counter))
    loss_type = self.evaluate(loss)
    loss_numpy = loss_type.loss

    # Assert that loss is not zero as we are training in a non-episodic env.
    self.assertNotEqual(
        loss_numpy,
        0.0,
        msg=(
            'Loss is exactly zero, looks like no training '
            'was performed due to incomplete episodes.'
        ),
    )

    # Assert that train_op ran increment_counter num_epochs times.
    self.assertEqual(num_epochs, self.evaluate(counter))
//  [python]
def testTrainWithNonLegacyActorNetwork(self, feed_transition):
    if not tf.executing_eagerly():
      self.skipTest('Skipping test: sequential networks not supported in TF1')

    num_epochs = 5
    counter = common.create_variable('test_train_counter')
    action_spec = {
        'my_action': tensor_spec.BoundedTensorSpec([1], tf.float32, -1, 1)
    }

    agent = ppo_agent.PPOAgent(
        self._time_step_spec,
        action_spec,
        tf.compat.v1.train.AdamOptimizer(),
        # action_spec == TensorSpec([1], tf.float32)
        actor_net=create_sequential_actor_net(ndims=1),
        value_net=DummyValueNet(self._obs_spec),
        normalize_observations=False,
        num_epochs=num_epochs,
        initial_adaptive_kl_beta=1.0,
        adaptive_kl_target=10.0,
        adaptive_kl_tolerance=0.5,
        check_numerics=True,
        compute_value_and_advantage_in_train=False,
        train_step_counter=counter,
    )
    agent.initialize()

    experience = _create_experience_trajectory_my_action()
    experience = agent._preprocess(experience)

    if feed_transition:
      experience = trajectory.to_transition(experience)

    loss = lambda: agent.train(experience)

    # Assert that counter starts out at zero.
    self.evaluate(tf.compat.v1.global_variables_initializer())
    self.assertEqual(0, self.evaluate(counter))
    loss_type = self.evaluate(loss)
    loss_numpy = loss_type.loss

    # Assert that loss is not zero as we are training in a non-episodic env.
    self.assertNotEqual(
        loss_numpy,
        0.0,
        msg=(
            'Loss is exactly zero, looks like no training '
            'was performed due to incomplete episodes.'
        ),
    )

    # Assert that train_op ran increment_counter num_epochs times.
    self.assertEqual(num_epochs, self.evaluate(counter))
//  [python]
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
                                        reduction_indices=[1]))

with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

sess = tf.Session()

# tf.train.SummaryWriter soon be deprecated, use following
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:  # tensorflow version < 0.12
    writer = tf.train.SummaryWriter('logs/', sess.graph)
else: # tensorflow version >= 0.12
    writer = tf.summary.FileWriter("logs/", sess.graph)

# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()
//  [python]
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2)+b_fc2)

# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                              reduction_indices=[1]))       # loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

sess = tf.Session()
# important step
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()
//  [python]
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2)+b_fc2)

# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                              reduction_indices=[1]))       # loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

sess = tf.Session()
# important step
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.global_variables_initializer()
else:
    init = tf.global_variables_initializer()
//  [python]
def GenCap(path,videos):
    captions=[]
    with tf.Graph().as_default():
        learning_rate = 0.00001
        video,caption,caption_mask,output_logits,loss,dropout_prob = build_model()
        optim = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(loss)
        # ckpt_file = 'S2VT_Dyn_10_0.0001_300_46000.ckpt.meta'        #######################
        ckpt_file = 'S2VT_temp_10_0.0001_300_47279.ckpt.meta'
        saver = tf.train.Saver()  ########################
        with tf.Session() as sess:
            if ckpt_file:
                # saver_ = tf.train.import_meta_graph(ckpt_file)
                # saver_.restore(sess,'./S2VT_Dyn_10_0.0001_300_46000.ckpt')  ##################
                saver.restore(sess,'./caption/S2VT_temp_10_0.0001_300_47279.ckpt')
                print ("Restored model")
            else:
                sess.run(tf.initialize_all_variables())
            for vid in videos:
                vid=path+vid
                while(1):
                    feat_vid = extract_feats(vid,1)
                    vid = fetch_data_batch_user(feat_vid)   ################
                    #vid,caption_GT,_,video_urls = fetch_data_batch(1)
                    caps,caps_mask = convert_caption(['<BOS>'],word2id,80)
                    for i in range(n_steps):
                        o_l = sess.run(output_logits,feed_dict={video:vid,
                                                                caption:caps,
                                                                caption_mask:caps_mask,
                                                                dropout_prob:1.0})
                        out_logits = o_l.reshape([batch_size,n_steps-1,vocab_size])
                        output_captions = np.argmax(out_logits,2)
                        caps[0][i+1] = output_captions[0][i]
                        #print_in_english(caps)
                        if id2word[output_captions[0][i]] == '<EOS>':
                            break
                    cap=print_in_english(caps)
                    break
                    # print ('............................\nGT Caption:\n')
                    # print_in_english(caption_GT)
                captions.append(cap)
    return captions
//  [python]
def GenCap(path,videos):
    captions=[]
    with tf.Graph().as_default():
        learning_rate = 0.00001
        video,caption,caption_mask,output_logits,loss,dropout_prob = build_model()
        optim = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(loss)
        # ckpt_file = 'S2VT_Dyn_10_0.0001_300_46000.ckpt.meta'        #######################
        ckpt_file = 'S2VT_temp_10_0.0001_300_47279.ckpt.meta'
        saver = tf.train.Saver()  ########################
        with tf.Session() as sess:
            if ckpt_file:
                # saver_ = tf.train.import_meta_graph(ckpt_file)
                # saver_.restore(sess,'./S2VT_Dyn_10_0.0001_300_46000.ckpt')  ##################
                saver.restore(sess,'./caption/S2VT_temp_10_0.0001_300_47279.ckpt')
                print ("Restored model")
            else:
                sess.run(tf.compat.v1.global_variables_initializer())
            for vid in videos:
                vid=path+vid
                while(1):
                    feat_vid = extract_feats(vid,1)
                    vid = fetch_data_batch_user(feat_vid)   ################
                    #vid,caption_GT,_,video_urls = fetch_data_batch(1)
                    caps,caps_mask = convert_caption(['<BOS>'],word2id,80)
                    for i in range(n_steps):
                        o_l = sess.run(output_logits,feed_dict={video:vid,
                                                                caption:caps,
                                                                caption_mask:caps_mask,
                                                                dropout_prob:1.0})
                        out_logits = o_l.reshape([batch_size,n_steps-1,vocab_size])
                        output_captions = np.argmax(out_logits,2)
                        caps[0][i+1] = output_captions[0][i]
                        #print_in_english(caps)
                        if id2word[output_captions[0][i]] == '<EOS>':
                            break
                    cap=print_in_english(caps)
                    break
                    # print ('............................\nGT Caption:\n')
                    # print_in_english(caption_GT)
                captions.append(cap)
    return captions
//  [python]
print(tf.__version__)

a=tf.constant(10)
b=tf.constant(32)
c=tf.add(a,b)

init = tf.initialize_all_variables()
//  [python]
print(tf.__version__)

a=tf.constant(10)
b=tf.constant(32)
c=tf.add(a,b)

init = tf.compat.v1.global_variables_initializer()
//  [python]
def evaluate():
    """Evaluated current agent, and records a video with it's performance"""
    envwrap = GymWrapperFactory.make(FLAGS.env,
                                     actrep=FLAGS.action_repeat,
                                     memlen=FLAGS.memory_len,
                                     w=FLAGS.width,
                                     h=FLAGS.height)
    with tf.Session() as sess:
        agent = QlearningAgent(session=sess,
                               action_size=envwrap.action_size,
                               h=FLAGS.height,
                               w=FLAGS.width,
                               channels=FLAGS.memory_len,
                               opt=tf.train.AdamOptimizer(FLAGS.lr))
        sess.run(tf.initialize_all_variables())
        if not os.path.exists(FLAGS.logdir):
            print('ERROR! No', FLAGS.logdir, 'folder found!')
            return
        ckpt = tf.train.latest_checkpoint(FLAGS.logdir)
        if ckpt is not None:
            tf.train.Saver().restore(sess, ckpt)
            agent.update_target()
            print('Session was restored from %s' % ckpt)
        else:
            print('ERROR! No checkpoint found at', FLAGS.logdir)
            return
        envwrap.env.monitor.start(os.path.join(FLAGS.evaldir, FLAGS.env))
        total_reward = 0
        for _ in range(FLAGS.eval_iter):
            s = envwrap.reset()
            terminal = False
            while not terminal:
                reward_per_action = agent.predict_rewards(s)
                s, r, terminal, info = envwrap.step(np.argmax(reward_per_action), test=True)
                total_reward += r
                envwrap.render()
        envwrap.env.monitor.close()
        print('Evaluation finished.')
        print('Average reward per episode: %0.4f' % (total_reward / FLAGS.eval_iter))