python - tutorial - tensorflow gpu support
Keras con il backend Tensorflow può essere costretto a usare CPU o GPU a piacimento? (4)
Come per il
tutorial
keras, puoi semplicemente usare lo stesso ambito
tf.device
del normale tensorflow:
with tf.device('/gpu:0'):
x = tf.placeholder(tf.float32, shape=(None, 20, 64))
y = LSTM(32)(x) # all ops in the LSTM layer will live on GPU:0
with tf.device('/cpu:0'):
x = tf.placeholder(tf.float32, shape=(None, 20, 64))
y = LSTM(32)(x) # all ops in the LSTM layer will live on CPU:0
Ho installato Keras con il backend Tensorflow e CUDA. A volte mi piacerebbe forzare Keras a usare la CPU. Questo può essere fatto senza dire l'installazione di un Tensorflow separato solo CPU in un ambiente virtuale? Se é cosi, come? Se il backend fosse Theano, le bandiere potrebbero essere impostate, ma non ho sentito parlare di bandiere Tensorflow accessibili tramite Keras.
Ho appena trascorso un po 'di tempo a capirlo.
La risposta di Thoma non è completa.
test.py
tuo programma sia
test.py
, vuoi usare gpu0 per eseguire questo programma e mantenere libero gpus.
Dovresti scrivere
CUDA_VISIBLE_DEVICES=0 python test.py
Notare che si tratta di
DEVICES
non di
DEVICE
Questo ha funzionato per me (win10), posto prima di importare keras:
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
Se vuoi forzare Keras a usare la CPU
Modo 1
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""
prima dell'importazione di Keras / Tensorflow.
Modo 2
Esegui il tuo script come
$ CUDA_VISIBLE_DEVICES="" ./your_keras_code.py
Guarda anche