Python 我如何在 GPU 上运行 theano

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33519182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:30:34  来源:igfitidea点击:

How can I run theano on GPU

pythoncudagputheano

提问by babeyh

If I run the following code with python 3.5

如果我使用 python 3.5 运行以下代码

import numpy as np
import time
import theano
A = np.random.rand(1000,10000).astype(theano.config.floatX)
B = np.random.rand(10000,1000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X,Y = theano.tensor.matrices('XY')
mf = theano.function([X,Y],X.dot(Y))
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print ("NP time: %f[s], theano time: %f[s] **(times should be close when run
on CPU!)**" %(np_end-np_start, t_end-t_start))
print ("Result difference: %f" % (np.abs(AB-tAB).max(), ))

I get the output

我得到输出

NP time: 0.161123[s], theano time: 0.167119[s] (times should be close when
run on CPU!)
Result difference: 0.000000

it says if the times are close, it means that I am running on my CPU.

它说如果时间接近,这意味着我正在我的 CPU 上运行。

How can I run this code on my GPU?

如何在我的 GPU 上运行此代码?

NOTE:

笔记:

  • I have a workstation with Nvidia Quadro k4200.
  • I have installed Cuda toolkit
  • I have successfully worked an cuda vectorAdd sample project on VS2012.
  • 我有一个带有 Nvidia Quadro k4200 的工作站。
  • 我已经安装了 Cuda 工具包
  • 我已经成功地在 VS2012 上完成了一个 cuda vectorAdd 示例项目。

采纳答案by Daniel Renshaw

You configure Theano to use a GPU by specifying the device=gpuin Theano's config. There are two principle methods for setting the config: (1) in the THEANO_FLAGSenvironment variable, or (2) via the .theanorc file. Both methods, and all of Theano's configuration flags, are documented.

您可以通过device=gpu在 Theano 的配置中指定 来配置 Theano 以使用 GPU 。设置配置的主要方法有两种:(1)在THEANO_FLAGS环境变量中,或(2)通过 .theanorc 文件。这两种方法以及 Theano 的所有配置标志都记录在案

You will know that Theano is using the GPU if, after calling import theanoyou see a message that looks something like this

如果在调用后import theano看到类似这样的消息,您就会知道 Theano 正在使用 GPU

Using gpu device 0: GeForce GT 640 (CNMeM is disabled)

The details may vary for you but if no message appears at all then Theano is using the CPU only.

详细信息可能因您而异,但如果根本没有出现任何消​​息,则 Theano 仅使用 CPU。

Note also that even if you see the GPU message, your particular computation graph may not run on the GPU. To see which parts of your computation are running on the GPU print its compiled and optimized graph

另请注意,即使您看到 GPU 消息,您的特定计算图也可能无法在 GPU 上运行。要查看计算的哪些部分在 GPU 上运行,请打印其编译和优化的图形

f = theano.function(...)
theano.printing.debugprint(f)

Operations that start with the prefix 'Gpu' will run on the GPU. Operations that do not have that prefix to their name will run on the CPU.

以前缀“Gpu”开头的操作将在 GPU 上运行。名称中没有该前缀的操作将在 CPU 上运行。

回答by Dherath

If you are on Linux, create a .theanorc file in your home folder and add the following to set up theano to run on GPU.

如果您使用的是 Linux,请在您的主文件夹中创建一个 .theanorc 文件并添加以下内容以设置 theano 在 GPU 上运行。

[global]
device = gpu
floatx = float32

回答by Erik Aronesty

Alternatively, if you want to use the GPU programattically:

或者,如果您想以编程方式使用 GPU:

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

You should see a message like this:

您应该会看到如下消息:

Using gpu device 0: Tesla K80

Useful if the environment you are running in isn't easy to configure.

如果您运行的环境不容易配置,则很有用。