Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将keras后端从tensorflow cpu更改为gpu_Python_Tensorflow_Neural Network_Keras - Fatal编程技术网

Python 将keras后端从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))

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))
    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