2017-07-02 267 views
1

keras tensorflow-cpu後端與tensorflow-gpu後端是否有區別(代碼中)?如果我想將tensorflow從cpu更改爲gpu,我需要添加哪些代碼或需要設置哪些環境變量?將keras後端從tensorflow cpu更改爲gpu

keras link我知道我可以使用tf.devices - 就像下面的代碼一樣。但是如果我想要整個代碼,而不是僅僅一部分在GPU上運行呢?

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 

回答

2

只需卸載tensorflow-cpupip uninstall tensorflow),並安裝tensorflow-gpupip install tensorflow-gpu)。現在tensorflow會一直使用你的GPU。

如果您只想在tensorflow-gpu中使用cpu,請設置環境變量CUDA_VISIBLE_DEVICES以使gpus不可見。在加載tensorflow之前,請在您的腳本中執行以下操作:

import os 
os.environ["CUDA_VISIBLE_DEVICES"]="" 
import tensorflow 
相關問題