• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python context.device函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tensorflow.python.eager.context.device函数的典型用法代码示例。如果您正苦于以下问题:Python device函数的具体用法?Python device怎么用?Python device使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了device函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: disabled_testSmallIntegerOpsForcedToCPU

  def disabled_testSmallIntegerOpsForcedToCPU(self):
    if not context.context().num_gpus():
      self.skipTest('No GPUs found')

    a = constant_op.constant((1, 2, 3, 4, 5), dtype=dtypes.int64)
    b = constant_op.constant((2, 3, 4, 5, 6), dtype=dtypes.int64)
    with context.device('gpu:0'):
      c = a + b

    # Op forced to CPU since all constants are integers and small.
    self.assertEqual(c.device, '/job:localhost/replica:0/task:0/device:CPU:0')

    a = array_ops.zeros((8, 10), dtype=dtypes.int64)
    b = array_ops.ones((8, 10), dtype=dtypes.int64)

    with context.device('gpu:0'):
      c = a + b

    # Op not forced to CPU since the tensors are larger than 64 elements.
    self.assertEqual(c.device, '/job:localhost/replica:0/task:0/device:GPU:0')

    a = constant_op.constant((1, 2, 3, 4, 5), dtype=dtypes.float32)
    b = constant_op.constant((2, 3, 4, 5, 6), dtype=dtypes.float32)
    with context.device('gpu:0'):
      c = a + b

    # Op not forced to CPU since the constants are not integers.
    self.assertEqual(c.device, '/job:localhost/replica:0/task:0/device:GPU:0')
开发者ID:kylin9872,项目名称:tensorflow,代码行数:28,代码来源:core_test.py


示例2: testResourceTensorPlacement

 def testResourceTensorPlacement(self):
   with context.device('gpu:0'):
     v = resource_variable_ops.ResourceVariable(1.0)
   with context.device('cpu:0'):
     # Check that even though we specified the cpu device we'll run the read op
     # in the device where the handle is.
     self.assertAllEqual(
         gen_resource_variable_ops.read_variable_op(v.handle, v.dtype), 1.0)
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:8,代码来源:core_test.py


示例3: testGenericSummary

 def testGenericSummary(self):
   with context.device(self._test_device):
     x = constant_op.constant(1337.0)
     with context.device("cpu:0"):
       metadata = constant_op.constant("foo")
     self._writer.generic("x", x, metadata)
     event = self._readLastEvent()
     self.assertEqual("x", event.summary.value[0].tag)
开发者ID:Crazyonxh,项目名称:tensorflow,代码行数:8,代码来源:summary_writer_test.py


示例4: testReEntrant

 def testReEntrant(self):
   cpu = context.device('cpu:0')
   gpu = context.device('gpu:0')
   with cpu:
     with gpu:
       with gpu:
         self.assertEndsWith(current_device(), 'GPU:0')
       self.assertEndsWith(current_device(), 'GPU:0')
     self.assertEndsWith(current_device(), 'CPU:0')
     with gpu:
       self.assertEndsWith(current_device(), 'GPU:0')
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:11,代码来源:core_test.py


示例5: testDevicePlacementEnforcesConsistency

 def testDevicePlacementEnforcesConsistency(self):
   cpu = context.device('cpu:0')
   gpu = context.device('gpu:0')
   cpu.__enter__()
   self.assertEndsWith(current_device(), 'CPU:0')
   gpu.__enter__()
   self.assertEndsWith(current_device(), 'GPU:0')
   with self.assertRaisesRegexp(
       RuntimeError, 'Exiting device scope without proper scope nesting'):
     cpu.__exit__()
     self.assertEndsWith(current_device(), 'GPU:0')
   gpu.__exit__()
   self.assertEndsWith(current_device(), 'CPU:0')
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:13,代码来源:core_test.py


示例6: run_eager_mode

 def run_eager_mode(self, **kwargs):
   if force_gpu:
     gpu_name = gpu_device_name()
     if not gpu_name:
       gpu_name = "/device:GPU:0"
     with context.device(gpu_name):
       f(self)
   elif use_gpu:
     # TODO(xpan): Support softplacement and gpu by default when available.
     f(self, **kwargs)
   else:
     with context.device("/device:CPU:0"):
       f(self, **kwargs)
开发者ID:Lin-jipeng,项目名称:tensorflow,代码行数:13,代码来源:test_util.py


示例7: testReEntrant

  def testReEntrant(self):
    if not context.context().num_gpus():
      self.skipTest('No GPUs found')

    cpu = context.device('cpu:0')
    gpu = context.device('gpu:0')
    with cpu:
      with gpu:
        with gpu:
          self.assertEndsWith(current_device(), 'GPU:0')
        self.assertEndsWith(current_device(), 'GPU:0')
      self.assertEndsWith(current_device(), 'CPU:0')
      with gpu:
        self.assertEndsWith(current_device(), 'GPU:0')
开发者ID:perfmjs,项目名称:tensorflow,代码行数:14,代码来源:core_test.py


示例8: testImageSummary

 def testImageSummary(self):
   with context.device(self._test_device):
     a = constant_op.constant([[10.0, 20.0], [-20.0, -10.0]])
     self._writer.histogram("image1", a)
     event = self._readLastEvent()
     self.assertEqual("image1", event.summary.value[0].tag)
     self.assertTrue(event.summary.value[0].image)
开发者ID:Crazyonxh,项目名称:tensorflow,代码行数:7,代码来源:summary_writer_test.py


示例9: testHistogramSummary

 def testHistogramSummary(self):
   with context.device(self._test_device):
     y = constant_op.constant([1.0, 3.0, 3.0, 7.0])
     self._writer.histogram("y", y)
     event = self._readLastEvent()
     self.assertEqual("y", event.summary.value[0].tag)
     self.assertTrue(event.summary.value[0].histo)
开发者ID:Crazyonxh,项目名称:tensorflow,代码行数:7,代码来源:summary_writer_test.py


示例10: testScalarSummary

 def testScalarSummary(self):
   with context.device(self._test_device):
     x = constant_op.constant(1337.0)
     self._writer.scalar("x", x)
     event = self._readLastEvent()
     self.assertTrue("x", event.summary.value[0].tag)
     self.assertEqual(1337.0, event.summary.value[0].simple_value)
开发者ID:Crazyonxh,项目名称:tensorflow,代码行数:7,代码来源:summary_writer_test.py


示例11: benchmark_defun_matmul_100_by_784_GPU

 def benchmark_defun_matmul_100_by_784_GPU(self):
   if not context.num_gpus():
     return
   with context.device(GPU):
     m = self._m_100_by_784.gpu()
     self._benchmark_defun_matmul(
         m, transpose_b=True, num_iters=self._num_iters_100_by_784)
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:7,代码来源:benchmarks_test.py


示例12: fn

 def fn(x):
   with context.device('/gpu:0'):
     b = constant_op.constant(2.0)
     c = math_ops.add(x.gpu(), b)
     # TODO(apassos): remove cpu below by making TensorVSPace aware
     # of devices.
     return math_ops.add(c, constant_op.constant(3.0)).cpu()
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:7,代码来源:backprop_test.py


示例13: benchmark_read_variable_op_with_tape_2_by_2_GPU

 def benchmark_read_variable_op_with_tape_2_by_2_GPU(self):
   if not context.num_gpus():
     return
   with context.device(GPU):
     m = resource_variable_ops.ResourceVariable(self._m_2_by_2.gpu())
     self._benchmark_read_variable_with_tape(
         m, num_iters=self._num_iters_2_by_2)
开发者ID:becster,项目名称:tensorflow,代码行数:7,代码来源:benchmarks_test.py


示例14: benchmark_defun_matmul_2_by_2_GPU

 def benchmark_defun_matmul_2_by_2_GPU(self):
   if not context.num_gpus():
     return
   with context.device(GPU):
     m = self._m_2_by_2.gpu()
     self._benchmark_defun_matmul(
         m, transpose_b=False, num_iters=self._num_iters_2_by_2)
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:7,代码来源:benchmarks_test.py


示例15: __init__

  def __init__(self,
               logdir,
               max_queue=10,
               flush_secs=120,
               filename_suffix=""):
    """Summary writer for TensorBoard, compatible with eager execution.

    If necessary, multiple instances of `SummaryWriter` can be created, with
    distinct `logdir`s and `name`s. Each `SummaryWriter` instance will retain
    its independent `global_step` counter and data writing destination.

    Example:
    ```python
    writer = tfe.SummaryWriter("my_model")

    # ... Code that sets up the model and data batches ...

    for _ in xrange(train_iters):
      loss = model.train_batch(batch)
      writer.scalar("loss", loss)
      writer.step()
    ```

    Args:
      logdir: Directory in which summary files will be written.
      max_queue: Number of summary items to buffer before flushing to
        filesystem. If 0, summaries will be flushed immediately.
      flush_secs: Number of secondsbetween forced commits to disk.
      filename_suffix: Suffix of the event protobuf files in which the summary
        data are stored.

    Raises:
      ValueError: If this constructor is called not under eager execution.
    """
    # TODO(apassos, ashankar): Make this class and the underlying
    # contrib.summary_ops compatible with graph model and remove this check.
    if not context.in_eager_mode():
      raise ValueError(
          "Use of SummaryWriter is currently supported only with eager "
          "execution enabled. File an issue at "
          "https://github.com/tensorflow/tensorflow/issues/new to express "
          "interest in fixing this.")

    # TODO(cais): Consider adding name keyword argument, which if None or empty,
    # will register the global global_step that training_util.get_global_step()
    # can find.
    with context.device(self._CPU_DEVICE):
      self._name = uuid.uuid4().hex
      self._global_step = 0
      self._global_step_tensor = variable_scope.get_variable(
          "global_step/summary_writer/" + self._name,
          shape=[], dtype=dtypes.int64,
          initializer=init_ops.zeros_initializer())
      self._global_step_dirty = False
      self._resource = gen_summary_ops.summary_writer(shared_name=self._name)
      gen_summary_ops.create_summary_file_writer(
          self._resource, logdir, max_queue, flush_secs, filename_suffix)
      # Delete the resource when this object is deleted
      self._resource_deleter = resource_variable_ops.EagerResourceDeleter(
          handle=self._resource, handle_device=self._CPU_DEVICE)
开发者ID:SylChan,项目名称:tensorflow,代码行数:60,代码来源:summary_writer.py


示例16: _update_global_step_tensor

 def _update_global_step_tensor(self):
   with context.device(self._CPU_DEVICE):
     if self._global_step_dirty:
       self._global_step_dirty = False
       return state_ops.assign(self._global_step_tensor, self._global_step)
     else:
       return self._global_step_tensor
开发者ID:SylChan,项目名称:tensorflow,代码行数:7,代码来源:summary_writer.py


示例17: fn

 def fn(x):
   with context.device('/gpu:0'):
     b = tensor.Tensor(2.0)
     c = math_ops.add(x.as_gpu_tensor(), b)
     # TODO(apassos): remove as_cpu_tensor below by making TensorVSPace aware
     # of devices.
     return math_ops.add(c, tensor.Tensor(3.0)).as_cpu_tensor()
开发者ID:chdinh,项目名称:tensorflow,代码行数:7,代码来源:backprop_test.py


示例18: audio

  def audio(self, name, tensor, sample_rate, max_outputs, family=None):
    """Write an audio summary.

    Args:
      name: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      tensor: A 3-D `float32` `Tensor` of shape `[batch_size, frames, channels]`
        or a 2-D `float32` `Tensor` of shape `[batch_size, frames]`, or
        compatible value type.
      sample_rate: A Scalar `float32` `Tensor` indicating the sample rate of the
        signal in hertz.
      max_outputs: Max number of batch elements to generate audio for.
      family: Optional; if provided, used as the prefix of the summary tag name,
        which controls the tab name used for display on Tensorboard.
    """
    with context.device(self._CPU_DEVICE):
      with summary_op_util.summary_scope(
          name, family, values=[tensor]) as (tag, scope):
        gen_summary_ops.write_audio_summary(
            self._resource, self._update_global_step_tensor(),
            tag,
            _maybe_cpu(tensor),
            sample_rate=_maybe_cpu(sample_rate),
            max_outputs=max_outputs,
            name=scope)
开发者ID:SylChan,项目名称:tensorflow,代码行数:25,代码来源:summary_writer.py


示例19: benchmark_defun_matmul_forward_backward_2_by_2_CPU_async

 def benchmark_defun_matmul_forward_backward_2_by_2_CPU_async(self):
   with context.device(CPU):
     m = self._m_2_by_2.cpu()
     self._benchmark_defun_matmul_forward_backward(
         m,
         transpose_b=False,
         num_iters=self._num_iters_2_by_2,
         execution_mode=context.ASYNC)
开发者ID:becster,项目名称:tensorflow,代码行数:8,代码来源:benchmarks_test.py


示例20: setUp

 def setUp(self):
   super(SummaryWriterTest, self).setUp()
   self._test_device = "gpu:0" if context.num_gpus() else "cpu:0"
   self._tmp_logdir = tempfile.mkdtemp()
   with context.device(self._test_device):
     # Use max_queue=0 so that summaries are immediately flushed to filesystem,
     # making testing easier.
     self._writer = summary_writer.SummaryWriter(self._tmp_logdir, max_queue=0)
开发者ID:Crazyonxh,项目名称:tensorflow,代码行数:8,代码来源:summary_writer_test.py



注:本文中的tensorflow.python.eager.context.device函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python context.eager_mode函数代码示例发布时间:2022-05-27
下一篇:
Python context.context函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap