我安装了带有Tensorflow后端和CUDA的Keras。我有时会按需强制Keras使用CPU。如果不在虚拟环境中安装单独的CPU-only Tensorflow,是否可以做到这一点?如果是这样的话,是怎么做的?如果后端是Theano,可以设置标志,但我还没有听说过可以通过Keras访问的Tensorflow标志。
发布于 2017-08-19 23:58:49
这对我来说很有效(win10),在导入keras之前放置:
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
发布于 2017-11-12 02:46:01
只需导入tensortflow并使用keras,就这么简单。
import tensorflow as tf
# your code here
with tf.device('/gpu:0'):
model.fit(X, y, epochs=20, batch_size=128, callbacks=callbacks_list)
发布于 2016-11-19 16:20:27
根据keras tutorial,您可以简单地使用与常规tensorflow相同的tf.device
作用域:
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
https://stackoverflow.com/questions/40690598
复制相似问题