由于一些复杂的原因,我在 python 代码中同时使用了tensorflow和theano,并且我有2个gpu,我希望它们共享,但如 another question 中所述。有一些问题,我想知道是否有一些技巧可以实现这一点,例如指定tensorflow仅使用1个gpu,而theano使用另一个gpu?
目前我只能通过os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float64'
禁用theano的gpu使用,并让tensorflow使用所有
os.environ['KERAS_BACKEND'] = 'theano'
os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float64'
import tensorflow as tf
import keras as ks
最佳答案
这个我没试过。但是,如果您有多个 GPU,则可以使用以下技巧强制在 GPU 上运行代码:
import tensorflow as tf
with tf.device('/gpu:0'):
# Run the tensorflow code
import tensorflow as tf
with tf.device('/gpu:1'):
# Run the theano code
希望这有帮助!
关于python - 如何让tensorflow和theano同时共享GPU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56182949/