コード例 #1
0
 def fn():
   h1 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")
   c1 = gen_data_flow_ops.stack_push_v2(h1, v)
   with ops.control_dependencies([c1]):
     c1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
   h2 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="bar")
   c2 = gen_data_flow_ops.stack_push_v2(h2, 5.0)
   with ops.control_dependencies([c2]):
     c2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)
   return c1 + c2
コード例 #2
0
      def fn():
        h1 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")
        h2 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")

        c1 = gen_data_flow_ops.stack_push_v2(h1, v1)
        with ops.control_dependencies([c1]):
          c2 = gen_data_flow_ops.stack_push_v2(h2, v2)
        with ops.control_dependencies([c2]):
          pop1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
          pop2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)
        return [pop1, pop2]
コード例 #3
0
 def testMultiStack(self):
   with self.test_session(), self.test_scope():
     v = array_ops.placeholder(dtypes.float32)
     h1 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")
     c1 = gen_data_flow_ops.stack_push_v2(h1, v)
     with ops.control_dependencies([c1]):
       c1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
     h2 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="bar")
     c2 = gen_data_flow_ops.stack_push_v2(h2, 5.0)
     with ops.control_dependencies([c2]):
       c2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)
     r = c1 + c2
     self.assertAllClose(9.0, r.eval({v: 4.0}))
コード例 #4
0
 def _testMultiStack(self, use_gpu):
   with self.test_session(use_gpu=use_gpu):
     h1 = gen_data_flow_ops.stack_v2(
         -1, elem_type=dtypes.float32, stack_name="foo")
     c1 = gen_data_flow_ops.stack_push_v2(h1, 4.0)
     with ops.control_dependencies([c1]):
       c1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
     h2 = gen_data_flow_ops.stack_v2(
         -1, elem_type=dtypes.float32, stack_name="bar")
     c2 = gen_data_flow_ops.stack_push_v2(h2, 5.0)
     with ops.control_dependencies([c2]):
       c2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)
     r = c1 + c2
     self.assertAllClose(9.0, r.eval())
コード例 #5
0
 def fn():
     h = gen_data_flow_ops.stack_v2(5,
                                    dtypes.float32,
                                    stack_name="foo")
     c = gen_data_flow_ops.stack_push_v2(h, x, swap_memory=True)
     with ops.control_dependencies([c]):
         return gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
コード例 #6
0
 def _testStackPushPop(self, use_gpu):
   with self.test_session(use_gpu=use_gpu):
     h = gen_data_flow_ops.stack_v2(
         -1, elem_type=dtypes.float32, stack_name="foo")
     c = gen_data_flow_ops.stack_push_v2(h, [[4.0, 5.0]])
     with ops.control_dependencies([c]):
       c1 = gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
     self.assertAllClose([[4.0, 5.0]], c1.eval())
コード例 #7
0
 def testMultiStack(self):
     with self.test_session(), self.test_scope():
         v = array_ops.placeholder(dtypes.float32)
         h1 = gen_data_flow_ops.stack_v2(5,
                                         dtypes.float32,
                                         stack_name="foo")
         c1 = gen_data_flow_ops.stack_push_v2(h1, v)
         with ops.control_dependencies([c1]):
             c1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
         h2 = gen_data_flow_ops.stack_v2(5,
                                         dtypes.float32,
                                         stack_name="bar")
         c2 = gen_data_flow_ops.stack_push_v2(h2, 5.0)
         with ops.control_dependencies([c2]):
             c2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)
         r = c1 + c2
         self.assertAllClose(9.0, r.eval({v: 4.0}))
コード例 #8
0
 def fn():
     h = gen_data_flow_ops.stack_v2(5,
                                    dtypes.float32,
                                    stack_name="foo")
     c = gen_data_flow_ops.stack_push_v2(h, v)
     with ops.control_dependencies([c]):
         c1 = gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
     return c1
コード例 #9
0
  def _testSameNameStacks(self, use_gpu):
    """Different stacks with the same name do not interfere."""
    with self.test_session(use_gpu=use_gpu) as sess:
      h1 = gen_data_flow_ops.stack_v2(
          -1, elem_type=dtypes.float32, stack_name="foo")
      h2 = gen_data_flow_ops.stack_v2(
          -1, elem_type=dtypes.float32, stack_name="foo")

      c1 = gen_data_flow_ops.stack_push_v2(h1, 4.0)
      with ops.control_dependencies([c1]):
        c2 = gen_data_flow_ops.stack_push_v2(h2, 5.0)
      with ops.control_dependencies([c2]):
        pop1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
        pop2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)

      out1, out2 = sess.run([pop1, pop2])
      self.assertAllClose(out1, 4.0)
      self.assertAllClose(out2, 5.0)
コード例 #10
0
 def testStackPushPopSwap(self):
     with self.test_session(), self.test_scope():
         a = np.arange(2000)
         x = array_ops.placeholder(dtypes.float32)
         h = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")
         c = gen_data_flow_ops.stack_push_v2(h, x, swap_memory=True)
         with ops.control_dependencies([c]):
             c1 = gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
         self.assertAllClose(a, c1.eval({x: a}))
コード例 #11
0
  def testSameNameStacks(self):
    """Different stacks with the same name do not interfere."""
    with self.test_session() as sess, self.test_scope():
      v1 = array_ops.placeholder(dtypes.float32)
      v2 = array_ops.placeholder(dtypes.float32)
      h1 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")
      h2 = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")

      c1 = gen_data_flow_ops.stack_push_v2(h1, v1)
      with ops.control_dependencies([c1]):
        c2 = gen_data_flow_ops.stack_push_v2(h2, v2)
      with ops.control_dependencies([c2]):
        pop1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
        pop2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)

      out1, out2 = sess.run([pop1, pop2], {v1: 4.0, v2: 5.0})
      self.assertAllClose(out1, 4.0)
      self.assertAllClose(out2, 5.0)
コード例 #12
0
 def testStackPushPopSwap(self):
   with self.test_session(), self.test_scope():
     a = np.arange(2000)
     x = array_ops.placeholder(dtypes.float32)
     h = gen_data_flow_ops.stack_v2(5, dtypes.float32, stack_name="foo")
     c = gen_data_flow_ops.stack_push_v2(h, x, swap_memory=True)
     with ops.control_dependencies([c]):
       c1 = gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
     self.assertAllClose(a, c1.eval({x: a}))
コード例 #13
0
 def testStackPushPop(self):
   with self.test_session(), self.test_scope():
     size = array_ops.placeholder(dtypes.int32)
     v = array_ops.placeholder(dtypes.float32)
     h = gen_data_flow_ops.stack_v2(size, dtypes.float32, stack_name="foo")
     c = gen_data_flow_ops.stack_push_v2(h, v)
     with ops.control_dependencies([c]):
       c1 = gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
     self.assertAllClose([[4.0, 5.0]], c1.eval({size: 5, v: [[4.0, 5.0]]}))
コード例 #14
0
 def _testStackPushPopSwap(self, use_gpu):
   with self.test_session(use_gpu=use_gpu):
     a = np.arange(2000)
     x = constant_op.constant(a, dtype=dtypes.float32)
     h = gen_data_flow_ops.stack_v2(
         -1, elem_type=dtypes.float32, stack_name="foo")
     c = gen_data_flow_ops.stack_push_v2(h, x, swap_memory=True)
     with ops.control_dependencies([c]):
       c1 = gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
     self.assertAllClose(a, c1.eval())
コード例 #15
0
    def testSameNameStacks(self):
        """Different stacks with the same name do not interfere."""
        with self.test_session() as sess, self.test_scope():
            v1 = array_ops.placeholder(dtypes.float32)
            v2 = array_ops.placeholder(dtypes.float32)
            h1 = gen_data_flow_ops.stack_v2(5,
                                            dtypes.float32,
                                            stack_name="foo")
            h2 = gen_data_flow_ops.stack_v2(5,
                                            dtypes.float32,
                                            stack_name="foo")

            c1 = gen_data_flow_ops.stack_push_v2(h1, v1)
            with ops.control_dependencies([c1]):
                c2 = gen_data_flow_ops.stack_push_v2(h2, v2)
            with ops.control_dependencies([c2]):
                pop1 = gen_data_flow_ops.stack_pop_v2(h1, dtypes.float32)
                pop2 = gen_data_flow_ops.stack_pop_v2(h2, dtypes.float32)

            out1, out2 = sess.run([pop1, pop2], {v1: 4.0, v2: 5.0})
            self.assertAllClose(out1, 4.0)
            self.assertAllClose(out2, 5.0)
コード例 #16
0
    def AddBackpropAccumulatedValue(self,
                                    history_value,
                                    value,
                                    dead_branch=False):
        """Add the getter for an accumulated value in the grad context.

    This is added to the backprop loop. Called in the grad context to
    get the value of an accumulated value. The stack pop op must be guarded
    by the pred of the controlling cond.

    Args:
      history_value: The history (a stack) of a value.
      value: The value that is pushed onto the stack.
      dead_branch: True iff the tensor is on a dead branch of a cond.

    Returns:
      The current value (the top of the stack).
    """
        history_ctxt = history_value.op._get_control_flow_context()
        # Find the cond context that controls history_value if any.
        cond_ctxt = None
        value_ctxt = value.op._get_control_flow_context()
        while value_ctxt and value_ctxt != history_ctxt:
            if isinstance(value_ctxt, control_flow_ops.CondContext):
                cond_ctxt = value_ctxt
                break
            value_ctxt = value_ctxt.outer_context
        with ops.control_dependencies(None):
            self.grad_context.Enter()
            if cond_ctxt:
                # Guard stack pop with a switch if it is controlled by a cond.
                grad_state = self
                pred = None
                while pred is None and grad_state:
                    pred = grad_state.history_map.get(cond_ctxt.pred.name)
                    grad_state = grad_state.outer_grad_state
                if pred is None:
                    pred = cond_ctxt.pred
                branch = (
                    1 - cond_ctxt.branch) if dead_branch else cond_ctxt.branch
                history_value = control_flow_ops._SwitchRefOrTensor(
                    history_value, pred)[branch]
            pop = gen_data_flow_ops.stack_pop_v2(history_value,
                                                 value.dtype.base_dtype)
            pop.set_shape(value.get_shape())
            self.grad_context.Exit()
        parallel_iterations = self.grad_context.parallel_iterations
        if parallel_iterations > 1:
            # All pops are ordered after pivot_for_body and before grad_sync.
            self.grad_sync._add_control_input(pop.op)
        return pop
コード例 #17
0
 def testStackPushPop(self):
     with self.test_session(), self.test_scope():
         size = array_ops.placeholder(dtypes.int32)
         v = array_ops.placeholder(dtypes.float32)
         h = gen_data_flow_ops.stack_v2(size,
                                        dtypes.float32,
                                        stack_name="foo")
         c = gen_data_flow_ops.stack_push_v2(h, v)
         with ops.control_dependencies([c]):
             c1 = gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
         self.assertAllClose([[4.0, 5.0]],
                             c1.eval({
                                 size: 5,
                                 v: [[4.0, 5.0]]
                             }))
コード例 #18
0
 def b1(x, y):
   nx = math_ops.subtract(x, 1)
   ny = y + gen_data_flow_ops.stack_pop_v2(h, dtypes.float32)
   return [nx, ny]