Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在CPU上运行Tensorflow_Python_Tensorflow - Fatal编程技术网

Python 如何在CPU上运行Tensorflow

Python 如何在CPU上运行Tensorflow,python,tensorflow,Python,Tensorflow,我已经在Ubuntu 14.04上安装了tensorflow的GPU版本 我在一个GPU服务器上,tensorflow可以访问可用的GPU 我想在CPU上运行tensorflow 通常我可以使用env CUDA_VISIBLE_DEVICES=0在0号GPU上运行 如何在CPU之间进行选择 我对使用tf重写代码不感兴趣。设备/cpu:0:您可以为每个tf应用设备计数参数。会话: 另请参见protobuf配置文件: 也可以将环境变量设置为 CUDA_VISIBLE_DEVICES="" 无需修改

我已经在Ubuntu 14.04上安装了tensorflow的GPU版本

我在一个GPU服务器上,tensorflow可以访问可用的GPU

我想在CPU上运行tensorflow

通常我可以使用env CUDA_VISIBLE_DEVICES=0在0号GPU上运行

如何在CPU之间进行选择

我对使用tf重写代码不感兴趣。设备/cpu:0:

您可以为每个tf应用设备计数参数。会话:

另请参见protobuf配置文件:


也可以将环境变量设置为

CUDA_VISIBLE_DEVICES=""

无需修改源代码。

如果上述答案无效,请尝试:

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

只需使用下面的代码

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
对我来说,只有将CUDA_VISIBLE_设备精确设置为-1才能工作:

作品:

import os
import tensorflow as tf

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

if tf.test.gpu_device_name():
    print('GPU found')
else:
    print("No GPU found")

# No GPU found
不起作用:


在某些系统中,必须指定:

import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]=""  # or even "-1"
导入tensorflow之前。

您可以使用。允许您设置是否以及使用哪个GPU的一个可能功能是:

import tensorflow as tf

def set_gpu(gpu_ids_list):
    gpus = tf.config.list_physical_devices('GPU')
    if gpus:
        try:
            gpus_used = [gpus[i] for i in gpu_ids_list]
            tf.config.set_visible_devices(gpus_used, 'GPU')
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
        except RuntimeError as e:
            # Visible devices must be set before GPUs have been initialized
            print(e)
假设您在一个有4个GPU的系统上,并且只想使用两个GPU,一个id=0,一个id=2,那么在导入库之后,代码的第一个命令将是:

set_gpu([0, 2])
在您的情况下,要仅使用CPU,可以使用空列表调用函数:

为完整起见,如果希望避免运行时初始化将分配设备上的所有内存,可以使用。 最后,管理要使用哪些设备(动态占用GPU内存)的功能变为:

import tensorflow as tf

def set_gpu(gpu_ids_list):
    gpus = tf.config.list_physical_devices('GPU')
    if gpus:
        try:
            gpus_used = [gpus[i] for i in gpu_ids_list]
            tf.config.set_visible_devices(gpus_used, 'GPU')
            for gpu in gpus_used:
                tf.config.experimental.set_memory_growth(gpu, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
        except RuntimeError as e:
            # Visible devices must be set before GPUs have been initialized
            print(e)

安装级别上的另一个可能的解决方案是查找仅CPU的变体:

在我的例子中,现在给出:

pip3 install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl

在本例中,只需选择正确的版本,即cp38提示python 3.8。使用类似venv的奖励积分,如中所述。

运行tensorflow 2.3.1时,环境变量解决方案不适用于我。根据github线程中的注释,我假设以下解决方案适用于>=2.1.0版本

发件人:

请确保在使用fresh tensorflow实例导入后立即执行此操作。如果您正在运行jupyter笔记本,请重新启动内核

要检查您是否确实在CPU上运行:

# To find out which devices your operations and tensors are assigned to
tf.debugging.set_log_device_placement(True)

# Create some tensors and perform an operation
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]])
c = tf.matmul(a, b)

print(c)
预期产出:

2.3.1
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)

在我的例子中,对于tensorflow 2.4.0,除非安装tensorflow cpu,否则前面的答案都不起作用

按照委员会的建议


有人说,在训练阶段之后在CPU上运行神经网络和在GPU上运行神经网络一样高效——也就是说,只有训练阶段真正需要GPU。你知道这是不是真的吗?谢谢这对我来说不适用tf1.1。fabrizioM的解决方案确实如此。使用CUDA_VISIBLE_DEVICES环境变量而不是更改代码中的配置不是更好吗?@Nandeesh我想这取决于您的需要。到目前为止,至少有53人更喜欢环境变量,35人更喜欢在代码中设置设备数量。第一种方法的优点是简单,另一种方法是在python程序内部对多个会话进行更明确的控制。零不需要硬编码,它可以是一个变量。您知道如何将其适应tensorflow 2.0吗,由于没有更多的会话或配置协议?有人说,在训练阶段结束后在CPU上运行神经网络的性能与在GPU上运行神经网络的性能一样——也就是说,只有训练阶段真正需要GPU。你知道这是不是真的吗?谢谢@克拉夏洛特:这不是真的。寻找各种干扰基准,CPU也慢一个数量级。@Thomas谢谢。建议考虑哪些基准?神经网络的工作量和性质也可能不同,对吧?显然google translate应用程序直接在智能手机上运行一些神经网络,可能是在cpu上,而不是在gpu上?@fabrizioM,一个玩具示例会更有用。这对我不起作用。:/设置环境变量,但tensorflow仍然使用GPU,我使用的是conda virtual env,这会造成差异吗?只是为了记录,第一个选项似乎不再有效。在使用tf.keras.Sequential模型时也适用于tf 2.X。有没有一种方法可以在tensorflow不调用错误消息CUDA_error_NO_DEVICE:未检测到支持CUDA的设备的情况下执行此操作?嗨,对我不起作用…我正在使用tensorflow gpu 2.4.1在一个脚本中使用tensorflow和pytorch,这种方法可以帮助我在tensorflow上禁用cuda,但仍然使pytorch使用cuda。我相信这个答案值得更多的选票。这对tensorflow 2.5有效。但是,我认为运行该命令后,tensorflow 2.5的GPU在当前环境中不再可用**我尝试了上面推荐的方法,但不起作用
pip3 install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl
import tensorflow as tf

# Hide GPU from visible devices
tf.config.set_visible_devices([], 'GPU')
# To find out which devices your operations and tensors are assigned to
tf.debugging.set_log_device_placement(True)

# Create some tensors and perform an operation
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]])
c = tf.matmul(a, b)

print(c)
2.3.1
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)
pip install tensorflow-cpu
# Place tensors on the CPU
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]])
  # Any additional tf code placed in this block will be executed on the CPU