Function to propagate backwards in the graph and mark nodes as used.

  Traverses recursively through the graph from the end tensor, through the op
  that generates the tensor, and then to the input tensors that feed the op.
  Nodes encountered are stored in used_node_names.

  Args:
    output_tensor: A Tensor which we start the propagation.
    used_node_names: A list of strings, stores the name of nodes we've marked as
      visited.

def mark_backward(output_tensor, used_node_names):
  """Function to propagate backwards in the graph and mark nodes as used.

  Traverses recursively through the graph from the end tensor, through the op
  that generates the tensor, and then to the input tensors that feed the op.
  Nodes encountered are stored in used_node_names.

  Args:
    output_tensor: A Tensor which we start the propagation.
    used_node_names: A list of strings, stores the name of nodes we've marked as
      visited.
  """
  op = output_tensor.op
  if op.name in used_node_names:
    return
  used_node_names.add(op.name)
  for input_tensor in op.inputs:
    mark_backward(input_tensor, used_node_names)
  for control_input_op in op.control_inputs:
    used_node_names.add(control_input_op.name)
    for input_tensor in control_input_op.inputs:
      mark_backward(input_tensor, used_node_names)