2017-07-02 266 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 
相关问题