code: def traverse(out, x, x_copy, d, visited=None):
      """
      Function used by scan to parse the tree and figure out which nodes
      it needs to replace.
  
      There are two options :
          1) x and x_copy or on host, then you would replace x with x_copy
          2) x is on gpu, x_copy on host, then you need to replace
          host_from_gpu(x) with x_copy
      This happens because initially shared variables are on GPU... which is
      fine for the main computational graph but confuses things a bit for the
      inner graph of scan.
  
      """
      # ``visited`` is a set of nodes that are already known and don't need to be
      # checked again, speeding up the traversal of multiply-connected graphs.
      # if a ``visited`` set is given, it will be updated in-place so the callee
      # knows which nodes we have seen.
      if visited is None:
          visited = set()
      if out in visited:
          return d
      visited.add(out)
      from theano.gpuarray.basic_ops import GpuFromHost, host_from_gpu
      from theano.gpuarray import pygpu_activated
      from theano.gpuarray.type import GpuArrayType
  
      if out == x:
          assert isinstance(x.type, GpuArrayType)
          d[out] = GpuFromHost(x.type.context_name)(x_copy)
          return d
      elif out.owner is None:
          return d
      elif (
          pygpu_activated
          and out.owner.op == host_from_gpu
          and out.owner.inputs == [x]
      ):
          d[out] = tensor.as_tensor_variable(x_copy)
          return d
      else:
          for inp in out.owner.inputs:
              d = traverse(inp, x, x_copy, d, visited)
          return d
  
imports: from __future__ import absolute_import, print_function, division
  import copy
  import logging
  import warnings
  from collections import OrderedDict
  import numpy
  import theano
  from theano.compat import izip
  from six import string_types, iteritems
  from six.moves import xrange
  from theano.compile.pfunc import rebuild_collect_shared
  from theano import gof, compat
  from theano import tensor, scalar
  from theano.tensor.basic import get_scalar_constant_value
label_desc: Use image node_id 2 for calling a global function with example usage: traverse(out, x, x_copy, d, visited) and returns: d, d, d, d, d