简体   繁体   English

如何更改theano使用的设备

[英]How can I change device used of theano

I tried to change the device used in theano-based program. 我试图更改基于theano的程序中使用的设备。

from theano import config
config.device = "gpu1"

However I got error 但是我收到了错误

Exception: Can't change the value of this config parameter after initialization!

I wonder what is the best way of change gpu to gpu1 in code ? 我想知道在代码中改变gpu到gpu1的最佳方法是什么?

Thanks 谢谢

There is no way to change this value in code running in the same process. 在同一进程中运行的代码中无法更改此值。 The best you could do is to have a "parent" process that alters, for example, the THEANO_FLAGS environment variable and spawns children. 您可以做的最好的事情是拥有一个“父”进程,例如,改变THEANO_FLAGS环境变量并生成子进程。 However, the method of spawning will determine which environment the children operate in. 但是,产卵方法将决定儿童在哪个环境中活动。

Note also that there is no way to do this in a way that maintains a process's memory through the change. 另请注意,无法通过更改维护进程内存的方式执行此操作。 You can't start running on CPU, do some work with values stored in memory then change to running on GPU and continue running using the values still in memory from the earlier (CPU) stage of work. 您无法开始在CPU上运行,使用存储在内存中的值执行某些操作然后更改为在GPU上运行并继续使用早期(CPU)工作阶段仍在内存中的值运行。 The process must be shutdown and restarted for a change of device to be applied. 必须关闭并重新启动该过程才能更改要应用的设备。

As soon as you import theano the device is fixed and cannot be changed within the process that did the import. 一旦import theano ,设备就会被修复,并且无法在执行导入的过程中进行更改。

Another possibility which worked for me was setting the environment variable in the process, before importing theano: 另一种对我有用的可能性是在导入theano之前在过程中设置环境变量:

import os    
os.environ['THEANO_FLAGS'] = "device=gpu1"    
import theano

Remove the "device" config in .theanorc, then in your code: 删除.theanorc中的“device”配置,然后在您的代码中:

import theano.sandbox.cuda
theano.sandbox.cuda.use("gpu0")

It works for me. 这个对我有用。

https://groups.google.com/forum/#!msg/theano-users/woPgxXCEMB4/l654PPpd5joJ https://groups.google.com/forum/#!msg/theano-users/woPgxXCEMB4/l654PPpd5joJ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM