How to run Tensorflow on CPU

2020-01-24 06:26发布

I have installed the GPU version of tensorflow on an Ubuntu 14.04.

I am on a GPU server where tensorflow can access the available GPUs.

I want to run tensorflow on the CPUs.

Normally I can use env CUDA_VISIBLE_DEVICES=0 to run on GPU no. 0.

How can I pick between the CPUs instead?

I am not intersted in rewritting my code with with tf.device("/cpu:0"):

6条回答
甜甜的少女心
2楼-- · 2020-01-24 06:46

If the above answers don't work, try:

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
查看更多
Juvenile、少年°
3楼-- · 2020-01-24 06:54

You can also set the environment variable to

CUDA_VISIBLE_DEVICES=""

without having to modify the source code.

查看更多
干净又极端
4楼-- · 2020-01-24 06:54

For me, only setting CUDA_VISIBLE_DEVICES to precisely -1 works:

Works:

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

Does not work:

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
查看更多
家丑人穷心不美
5楼-- · 2020-01-24 07:03

Just using the code below.

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
查看更多
三岁会撩人
6楼-- · 2020-01-24 07:04

In some systems one have to specify:

import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]=""  # or even "-1"

BEFORE importing tensorflow.

查看更多
时光不老,我们不散
7楼-- · 2020-01-24 07:06

You can apply device_count parameter per tf.Session:

config = tf.ConfigProto(
        device_count = {'GPU': 0}
    )
sess = tf.Session(config=config)

See also protobuf config file:

tensorflow/core/framework/config.proto

查看更多
登录 后发表回答