如何在 CPU 上 运行 Tensorflow
How to run Tensorflow on CPU
我已经在 Ubuntu 14.04.
上安装了 GPU 版本的 tensorflow
我在 GPU 服务器上,tensorflow 可以访问可用的 GPU。
我想 运行 CPU 上的 tensorflow。
通常我可以在 GPU 上使用 env CUDA_VISIBLE_DEVICES=0
到 运行。 0.
我该如何在 CPU 之间进行选择?
我对用 with tf.device("/cpu:0"):
重写我的代码不感兴趣
您可以根据 tf.Session
:
应用 device_count
参数
config = tf.ConfigProto(
device_count = {'GPU': 0}
)
sess = tf.Session(config=config)
另请参阅 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_DEVICES
精确设置为 -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
import tensorflow as tf
os.environ['CUDA_VISIBLE_DEVICES'] = ''
if tf.test.gpu_device_name():
print('GPU found')
else:
print("No GPU found")
# GPU found
在某些系统中必须指定:
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="" # or even "-1"
在导入 tensorflow 之前。
您可以使用 tf.config.set_visible_devices
。允许您设置是否使用以及使用哪些 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,您可以使用空列表调用该函数:
set_gpu([])
为了完整起见,如果要避免运行时初始化会分配设备上的所有内存,可以使用tf.config.experimental.set_memory_growth
。
最后,管理使用哪些设备,动态占用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)
安装级别的另一种可能解决方案是寻找 the CPU only variant
就我而言,现在给出:
pip3 install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl
只是 select 正确的版本(在这种情况下,cp38
提示 python 3.8
- 此外,Tensorflow 2.2.0 使用,截至 20 年 7 月 12 日的当前版本)。
使用 venv 的奖励积分,如 this answer.
中所述
环境变量解决方案对我不起作用运行 tensorflow 2.3.1。我根据 github 线程中的评论假设以下解决方案适用于版本 >=2.1.0.
import tensorflow as tf
# Hide GPU from visible devices
tf.config.set_visible_devices([], 'GPU')
确保在导入新的 tensorflow 实例后立即执行此操作(如果您是 运行 jupyter notebook,请重新启动内核)。
并检查您确实 运行 在 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
,否则之前答案的 none 有效
pip install tensorflow-cpu
根据 Tensorflow GPU guide 的推荐。
# 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
1. Fabrizio 的回答对我有用:
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
我必须在导入 tensorflow 之前进行更改。
我正在使用 tensorflow 2.4.0
2. 另一个(低于标准)解决方案可能是重命名 gpu 计算所需的 cusolver64_10.dll 文件.由于tensorflow找不到dll,它会自动使用CPU.
它应该在这样的地方:
C:\Program Files\NVIDIA GPU 计算 Toolkit\CUDA\v11.2\bin
# this works on tensorflow 2.8, windows 10, jupyterlab Version 3.3.2
# this is the very FIRST lines of code
import tensorflow as tf
tf.config.set_visible_devices([], 'GPU')
# if tf.test.gpu_device_name(): # this lies and tells you about all devices
if tf.config.experimental.list_logical_devices('GPU'):
print('GPU found')
else:
print("No GPU found")
我花了太多时间试图解决这个问题。
大多数尝试将进程 运行 部分保留在 CPU 上并仍在加载到 GPU 内存中?奇怪...
运行 上面的代码首先,在其他任何工作之前。
我后来能够将我的隐藏从 6k 增加到 12k。
现在是运行,只用了CPU。
每个 epoch 花费的时间大约是 GPU 上的 10 倍。
从每个 epoch 大约 3 分钟到每个 epoch 超过 35 分钟。
这是可以接受的trade-off。训练时间与模型大小。
我已经在 Ubuntu 14.04.
上安装了 GPU 版本的 tensorflow我在 GPU 服务器上,tensorflow 可以访问可用的 GPU。
我想 运行 CPU 上的 tensorflow。
通常我可以在 GPU 上使用 env CUDA_VISIBLE_DEVICES=0
到 运行。 0.
我该如何在 CPU 之间进行选择?
我对用 with tf.device("/cpu:0"):
您可以根据 tf.Session
:
device_count
参数
config = tf.ConfigProto(
device_count = {'GPU': 0}
)
sess = tf.Session(config=config)
另请参阅 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_DEVICES
精确设置为 -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
import tensorflow as tf
os.environ['CUDA_VISIBLE_DEVICES'] = ''
if tf.test.gpu_device_name():
print('GPU found')
else:
print("No GPU found")
# GPU found
在某些系统中必须指定:
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="" # or even "-1"
在导入 tensorflow 之前。
您可以使用 tf.config.set_visible_devices
。允许您设置是否使用以及使用哪些 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,您可以使用空列表调用该函数:
set_gpu([])
为了完整起见,如果要避免运行时初始化会分配设备上的所有内存,可以使用tf.config.experimental.set_memory_growth
。
最后,管理使用哪些设备,动态占用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)
安装级别的另一种可能解决方案是寻找 the CPU only variant
就我而言,现在给出:
pip3 install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl
只是 select 正确的版本(在这种情况下,cp38
提示 python 3.8
- 此外,Tensorflow 2.2.0 使用,截至 20 年 7 月 12 日的当前版本)。
使用 venv 的奖励积分,如 this answer.
中所述环境变量解决方案对我不起作用运行 tensorflow 2.3.1。我根据 github 线程中的评论假设以下解决方案适用于版本 >=2.1.0.
import tensorflow as tf
# Hide GPU from visible devices
tf.config.set_visible_devices([], 'GPU')
确保在导入新的 tensorflow 实例后立即执行此操作(如果您是 运行 jupyter notebook,请重新启动内核)。
并检查您确实 运行 在 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
pip install tensorflow-cpu
根据 Tensorflow GPU guide 的推荐。
# 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
1. Fabrizio 的回答对我有用:
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
我必须在导入 tensorflow 之前进行更改。 我正在使用 tensorflow 2.4.0
2. 另一个(低于标准)解决方案可能是重命名 gpu 计算所需的 cusolver64_10.dll 文件.由于tensorflow找不到dll,它会自动使用CPU.
它应该在这样的地方: C:\Program Files\NVIDIA GPU 计算 Toolkit\CUDA\v11.2\bin
# this works on tensorflow 2.8, windows 10, jupyterlab Version 3.3.2
# this is the very FIRST lines of code
import tensorflow as tf
tf.config.set_visible_devices([], 'GPU')
# if tf.test.gpu_device_name(): # this lies and tells you about all devices
if tf.config.experimental.list_logical_devices('GPU'):
print('GPU found')
else:
print("No GPU found")
我花了太多时间试图解决这个问题。 大多数尝试将进程 运行 部分保留在 CPU 上并仍在加载到 GPU 内存中?奇怪...
运行 上面的代码首先,在其他任何工作之前。
我后来能够将我的隐藏从 6k 增加到 12k。 现在是运行,只用了CPU。 每个 epoch 花费的时间大约是 GPU 上的 10 倍。 从每个 epoch 大约 3 分钟到每个 epoch 超过 35 分钟。 这是可以接受的trade-off。训练时间与模型大小。