Python tensorflow.python.ops.control_flow_ops.IsLoopSwitch() Examples

The following are 10 code examples of tensorflow.python.ops.control_flow_ops.IsLoopSwitch(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.python.ops.control_flow_ops , or try the search function .
Example #1
Source File: gradients_impl.py    From lambda-packs with MIT License 5 votes vote down vote up
def _UpdatePendingAndEnqueueReady(grads, op, queue, pending_count, loop_state):
  """Update pending count for the inputs of op and enqueue ready ops."""
  for x in op.inputs:
    # pylint: disable=protected-access
    pending_count[x.op._id] -= 1
    ready = (pending_count[x.op._id] == 0)
    if loop_state and not ready:
      ready = (pending_count[x.op._id] > 0 and
               control_flow_ops.IsLoopSwitch(x.op))
    # pylint: enable=protected-access
    if ready:
      if control_flow_ops.IsLoopExit(x.op):
        # if x is an exit without real gradient, defer processing them.
        grad_state = loop_state.GetGradState(x.op, before=False)
        grad_state.deferred_exits.append(x)
        grad_state.pending_exits_count -= 1
        if grad_state.pending_exits_count == 0:
          # We now have all the exits so process them.
          has_real_grad = False
          for y in grad_state.deferred_exits:
            if _HasAnyNotNoneGrads(grads, y.op):
              has_real_grad = True
              queue.append(y.op)
            else:
              grad_state.unused_exits.append(y)
          if has_real_grad:
            # For an unused exit, if it has floating-point outputs, backprop
            # a zero gradient. Otherwise, just ignore it.
            for y in grad_state.unused_exits:
              if _IsTrainable(y):
                _SetGrad(grads, y, loop_state.ZerosLikeForExit(y))
              queue.append(y.op)
          else:
            # All exits are "unused" so use None as gradient.
            for y in grad_state.unused_exits:
              queue.append(y.op)
      else:
        queue.append(x.op) 
Example #2
Source File: gradients_impl.py    From lambda-packs with MIT License 5 votes vote down vote up
def _SetGrad(grads, t, grad):
  """Sets gradient "grad" in "grads" for tensor "t"."""
  op = t.op
  op_grads = grads.get(op)
  if not op_grads:
    op_grads = [[] for _ in xrange(len(op.outputs))]
    grads[op] = op_grads
  t_grads = op_grads[t.value_index]
  if isinstance(t_grads, list):
    t_grads.append(grad)
  else:
    assert control_flow_ops.IsLoopSwitch(op)
    op_grads[t.value_index] = grad 
Example #3
Source File: gradients_impl.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def _UpdatePendingAndEnqueueReady(grads, op, queue, pending_count, loop_state):
  """Update pending count for the inputs of op and enqueue ready ops."""
  for x in op.inputs:
    # pylint: disable=protected-access
    pending_count[x.op._id] -= 1
    ready = (pending_count[x.op._id] == 0)
    if loop_state and not ready:
      ready = (pending_count[x.op._id] > 0 and
               control_flow_ops.IsLoopSwitch(x.op))
    # pylint: enable=protected-access
    if ready:
      if control_flow_ops.IsLoopExit(x.op):
        # if x is an exit without real gradient, defer processing them.
        grad_state = loop_state.GetGradState(x.op, before=False)
        grad_state.deferred_exits.append(x)
        grad_state.pending_exits_count -= 1
        if grad_state.pending_exits_count == 0:
          # We now have all the exits so process them.
          has_real_grad = False
          for y in grad_state.deferred_exits:
            if _HasAnyNotNoneGrads(grads, y.op):
              has_real_grad = True
              queue.append(y.op)
            else:
              grad_state.unused_exits.append(y)
          if has_real_grad:
            # For an unused exit, if it has floating-point outputs, backprop
            # a zero gradient. Otherwise, just ignore it.
            for y in grad_state.unused_exits:
              if _IsTrainable(y):
                _SetGrad(grads, y, loop_state.ZerosLikeForExit(y))
              queue.append(y.op)
          else:
            # All exits are "unused" so use None as gradient.
            for y in grad_state.unused_exits:
              queue.append(y.op)
      else:
        queue.append(x.op) 
Example #4
Source File: gradients_impl.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def _SetGrad(grads, t, grad):
  """Sets gradient "grad" in "grads" for tensor "t"."""
  op = t.op
  op_grads = grads.get(op)
  if not op_grads:
    op_grads = [[] for _ in xrange(len(op.outputs))]
    grads[op] = op_grads
  t_grads = op_grads[t.value_index]
  if isinstance(t_grads, list):
    t_grads.append(grad)
  else:
    assert control_flow_ops.IsLoopSwitch(op)
    op_grads[t.value_index] = grad 
Example #5
Source File: gradients_impl.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _UpdatePendingAndEnqueueReady(grads, op, queue, pending_count, loop_state):
  """Update pending count for the inputs of op and enqueue ready ops."""
  for x in op.inputs:
    # pylint: disable=protected-access
    pending_count[x.op._id] -= 1
    ready = (pending_count[x.op._id] == 0)
    if loop_state and not ready:
      ready = (pending_count[x.op._id] > 0 and
               control_flow_ops.IsLoopSwitch(x.op))
    # pylint: enable=protected-access
    if ready:
      if control_flow_ops.IsLoopExit(x.op):
        # if x is an exit without real gradient, defer processing them.
        grad_state = loop_state.GetGradState(x.op, before=False)
        grad_state.deferred_exits.append(x)
        grad_state.pending_exits_count -= 1
        if grad_state.pending_exits_count == 0:
          # We now have all the exits so process them.
          has_real_grad = False
          for y in grad_state.deferred_exits:
            if _HasAnyNotNoneGrads(grads, y.op):
              has_real_grad = True
              queue.append(y.op)
            else:
              grad_state.unused_exits.append(y)
          if has_real_grad:
            # For an unused exit, if it has floating-point outputs, backprop
            # a zero gradient. Otherwise, just ignore it.
            for y in grad_state.unused_exits:
              if _IsTrainable(y):
                _SetGrad(grads, y, loop_state.ZerosLikeForExit(y))
              queue.append(y.op)
          else:
            # All exits are "unused" so use None as gradient.
            for y in grad_state.unused_exits:
              queue.append(y.op)
      else:
        queue.append(x.op) 
Example #6
Source File: gradients_impl.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _SetGrad(grads, t, grad):
  """Sets gradient "grad" in "grads" for tensor "t"."""
  op = t.op
  op_grads = grads.get(op)
  if not op_grads:
    op_grads = [[] for _ in xrange(len(op.outputs))]
    grads[op] = op_grads
  t_grads = op_grads[t.value_index]
  if isinstance(t_grads, list):
    t_grads.append(grad)
  else:
    assert control_flow_ops.IsLoopSwitch(op)
    op_grads[t.value_index] = grad 
Example #7
Source File: gradients_impl.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def _UpdatePendingAndEnqueueReady(grads, op, queue, pending_count, loop_state):
  """Update pending count for the inputs of op and enqueue ready ops."""
  for x in op.inputs:
    # pylint: disable=protected-access
    pending_count[x.op._id] -= 1
    ready = (pending_count[x.op._id] == 0)
    if loop_state and not ready:
      ready = (pending_count[x.op._id] > 0 and
               control_flow_ops.IsLoopSwitch(x.op))
    # pylint: enable=protected-access
    if ready:
      if control_flow_ops.IsLoopExit(x.op):
        # if x is an exit without real gradient, defer processing them.
        grad_state = loop_state.GetGradState(x.op, before=False)
        grad_state.deferred_exits.append(x)
        grad_state.pending_exits_count -= 1
        if grad_state.pending_exits_count == 0:
          # We now have all the exits so process them.
          has_real_grad = False
          for y in grad_state.deferred_exits:
            if _HasAnyNotNoneGrads(grads, y.op):
              has_real_grad = True
              queue.append(y.op)
            else:
              grad_state.unused_exits.append(y)
          if has_real_grad:
            # For an unused exit, if it has floating-point outputs, backprop
            # a zero gradient. Otherwise, just ignore it.
            for y in grad_state.unused_exits:
              if _IsTrainable(y):
                _SetGrad(grads, y, loop_state.ZerosLikeForExit(y))
              queue.append(y.op)
          else:
            # All exits are "unused" so use None as gradient.
            for y in grad_state.unused_exits:
              queue.append(y.op)
      else:
        queue.append(x.op) 
Example #8
Source File: gradients_impl.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def _SetGrad(grads, t, grad):
  """Sets gradient "grad" in "grads" for tensor "t"."""
  op = t.op
  op_grads = grads.get(op)
  if not op_grads:
    op_grads = [[] for _ in xrange(len(op.outputs))]
    grads[op] = op_grads
  t_grads = op_grads[t.value_index]
  if isinstance(t_grads, list):
    t_grads.append(grad)
  else:
    assert control_flow_ops.IsLoopSwitch(op)
    op_grads[t.value_index] = grad 
Example #9
Source File: gradients_impl.py    From keras-lambda with MIT License 5 votes vote down vote up
def _UpdatePendingAndEnqueueReady(grads, op, queue, pending_count, loop_state):
  """Update pending count for the inputs of op and enqueue ready ops."""
  for x in op.inputs:
    # pylint: disable=protected-access
    pending_count[x.op._id] -= 1
    ready = (pending_count[x.op._id] == 0)
    if loop_state and not ready:
      ready = (pending_count[x.op._id] > 0 and
               control_flow_ops.IsLoopSwitch(x.op))
    # pylint: enable=protected-access
    if ready:
      if control_flow_ops.IsLoopExit(x.op):
        # if x is an exit without real gradient, defer processing them.
        grad_state = loop_state.GetGradState(x.op, before=False)
        grad_state.deferred_exits.append(x)
        grad_state.pending_exits_count -= 1
        if grad_state.pending_exits_count == 0:
          # We now have all the exits so process them.
          has_real_grad = False
          for y in grad_state.deferred_exits:
            if _HasAnyNotNoneGrads(grads, y.op):
              has_real_grad = True
              queue.append(y.op)
            else:
              grad_state.unused_exits.append(y)
          if has_real_grad:
            # For an unused exit, if it has floating-point outputs, backprop
            # a zero gradient. Otherwise, just ignore it.
            for y in grad_state.unused_exits:
              if _IsTrainable(y):
                _SetGrad(grads, y, loop_state.ZerosLikeForExit(y))
              queue.append(y.op)
          else:
            # All exits are "unused" so use None as gradient.
            for y in grad_state.unused_exits:
              queue.append(y.op)
      else:
        queue.append(x.op) 
Example #10
Source File: gradients_impl.py    From keras-lambda with MIT License 5 votes vote down vote up
def _SetGrad(grads, t, grad):
  """Sets gradient "grad" in "grads" for tensor "t"."""
  op = t.op
  op_grads = grads.get(op)
  if not op_grads:
    op_grads = [[] for _ in xrange(len(op.outputs))]
    grads[op] = op_grads
  t_grads = op_grads[t.value_index]
  if isinstance(t_grads, list):
    t_grads.append(grad)
  else:
    assert control_flow_ops.IsLoopSwitch(op)
    op_grads[t.value_index] = grad