在Theano执行期间选择GPU

P. *_*eri 10 python gpu theano lasagne

我正在使用4 GPU机器上的theano和lasagne训练神经网络.我.theanorc包含以下几行:

[global]
device = gpu0
Run Code Online (Sandbox Code Playgroud)

所以当我在python中执行时 import theano,我得到了Using gpu device 0: GRID K520

如果在导入theano后我选择使用say gpu1怎么办?我想动态地这样做,也就是说,没有编辑.theanorc是可能的吗?甚至在运行时选择它?

Eel*_*aak 11

我害怕在导入Theano之后无法更改执行设备.从文档:

config.device

字符串值:'cpu','gpu','gpu0','gpu1','gpu2'或'gpu3'

[...]

在程序执行期间无法修改此标志的值.

Bonus:但是,假设您想要在一个单独的GPU上运行两个Python进程(这就是您想要的吗?),那么您可以执行以下操作:

import os
os.system("THEANO_FLAGS='device=gpu0' python myscript.py")
os.system("THEANO_FLAGS='device=gpu1' python myscript.py")
Run Code Online (Sandbox Code Playgroud)

或者入侵/扩展Python的多处理模块(通过产生子进程来工作)以确保在生成子进程之前设置标志.

  • 这可能是相关的:https://github.com/Theano/Theano/wiki/Using-Multiple-GPUs (2认同)

gid*_*dim 5

编辑:Theano现在基于GPU阵列后端,以下API不再可用.

正如@EelkeSpaak所提到的,在导入theano之后无法更改GPU设备.但是如果你想在不改变环境变量的情况下以编程方式选择它.

  1. 确保您没有在.theanorc文件中选择设备.所以没有:

    device=gpu

  2. 在呼叫之前import theano选择GPU设备如下:

    import theano.sandbox.cuda
    theano.sandbox.cuda.use('gpu1')
    
    #Results in Using gpu device 1: Tesla K80
    
    Run Code Online (Sandbox Code Playgroud)