code: def find_tensors(sources, predicate):
      """
      find all the tensors that are used for computing grads and has been
      computed during forward
      :param grads:
      :param forward_ops:
      :return:
      """
      from collections import deque
      import tensorflow as tf
  
      queue = deque([])
      for source in sources:
          queue.append(source)
  
      results = set()
      visited = set()
      while len(queue) > 0:
          node = queue.popleft()
          # this is necessary, because input may not be differentiable
          if node is None:
              continue
          else:
              visited.add(node.name)
              if predicate(node):
                  results.add(node)
              else:
                  if isinstance(node, tf.Tensor):
                      inputs = list(node.op.inputs) + list(
                          node.op.control_inputs
                      )
                  elif isinstance(node, tf.Operation):
                      inputs = list(node.inputs) + list(
                          node.control_inputs
                      )
                  else:
                      invalidInputError(
                          False, "Unrecognized Node: {}".format(node)
                      )
                  for input_tensor in inputs:
                      # this is necessary because there may be a cycle in the graph
                      # such as tf.while_loop
                      if input_tensor.name not in visited:
                          queue.append(input_tensor)
      return list(results)
  
imports: import sys
  import warnings
  from bigdl.dllib.utils.log4Error import *
label_desc: Use image node_id 1 for calling a global function with example usage: find_tensors(sources, predicate) and returns: list