【问题标题】:CPU as default for tensorflowCPU 作为张量流的默认值
【发布时间】:2022-08-02 21:38:58
【问题描述】:

我想默认使用 CPU。应该只使用tf.device(\"/gpu:0\"): 将一些代码提供给 GPU。

当我使用

import os
os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"\"

tensorflow 仍将使用我的 GPU。

  • Does this help? 确保在调用与 tensorflow 相关的任何内容之前调用它。

标签: python python-3.x tensorflow gpu


【解决方案1】:

您可以同时使用tf.device('/CPU:0')tf.device('/GPU:0')。请在下面找到示例代码。

import tensorflow as tf
tf.debugging.set_log_device_placement(True)

with tf.device('/CPU:0'):
 a=tf.constant([[1.0,2.0,3.0],[4.0,5.0,6.0]])
 b=tf.constant([[1.0,2.0],[3.0,4.0],[5.0,6.0]])
 d=tf.matmul(a,b) 
 print(d)

with tf.device('/GPU:0'):
  c=tf.matmul(a,b)
  print(c)

输出:

Executing op MatMul in device /job:localhost/replica:0/task:0/device:CPU:0
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)
Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2021-07-03
    • 2017-10-05
    • 1970-01-01
    • 2019-02-10
    • 2014-03-03
    • 2013-12-13
    相关资源
    最近更新 更多