在基于tensorflow的keras中,其会自动检测运行的机器中是否有gpu,如果有的话,其会自动在gpu上执行运算。但是keras并没有提供像基于theano如下所示的接口来对要运行的gpu进行选择
# the name 'gpu' might have to be changed depending on your device's identifier (e.g. gpu0, gpu1, etc).
THEANO_FLAGS=device=gpu, floatX=32 python my_keras_script.py
因此我们可以通过指定机器中的哪个gpu可见来实现gpu的选择,如下所示:
CUDA_VISIBLE_DEVICES=1 python test.py
这里指明了我们希望第2个gpu是被可见的
CUDA_VISIBLE_DEVICES=1 Only device 1 will be seen
CUDA_VISIBLE_DEVICES=0,1 Devices 0 and 1 will be visible
CUDA_VISIBLE_DEVICES=”0,1” Same as above, quotation marks are optional
CUDA_VISIBLE_DEVICES=0,2,3 Devices 0, 2, 3 will be visible; device 1 is masked