Python 将keras后端从tensorflow cpu更改为gpu
keras tensorflow cpu后端和tensorflow gpu后端之间(在代码上)有差异吗?如果我想将tensorflow从cpu更改为gpu,我需要添加哪些代码,或者需要设置哪些环境变量 据我所知,我可以使用Python 将keras后端从tensorflow cpu更改为gpu,python,tensorflow,neural-network,keras,Python,Tensorflow,Neural Network,Keras,keras tensorflow cpu后端和tensorflow gpu后端之间(在代码上)有差异吗?如果我想将tensorflow从cpu更改为gpu,我需要添加哪些代码,或者需要设置哪些环境变量 据我所知,我可以使用tf.devices——类似下面的代码。但是如果我想要整个代码,而不仅仅是在GPU上运行的一部分,该怎么办 with tf.device('/gpu:0'): x = tf.placeholder(tf.float32, shape=(None, 20, 64))
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
只需卸载
tensorflow cpu
(pip卸载tensorflow
)并安装tensorflow gpu
(pip安装tensorflow gpu
)。现在tensorflow将始终使用您的gpu
如果您只想在tensorflow gpu中使用cpu,请设置环境变量CUDA_VISIBLE_DEVICES
,使gpu不可见。在加载tensorflow之前,请在脚本中执行以下操作:
import os
os.environ["CUDA_VISIBLE_DEVICES"]=""
import tensorflow