我如何获得 Op 将放置或放置在其下的设备?

How do I get the device an Op will be, or is, placed under?

这个问题类似于:

但是这个问题是关于设备范围的。我知道它稍微复杂一些,因为设备可能没有明确的范围,并且可能无法提供任何保证。

是否有任何方法来深入了解这一点?

我使用以下实用程序

class _DeviceCaptureOp(object):
  def __init__(self):
    self.device = None
  def _set_device(self, device):
    self.device = device

def get_current_device():
  """Returns device string of current graph context."""

  g = tf.get_default_graph()
  op = _DeviceCaptureOp()
  g._apply_device_functions(op)
  return op.device