ubuntu下让theano使用GPU

ubuntu下让theano使用GPU

在ubuntu下安装完theano以及cuda后,可以使用如下程序来测试你当前是否使用了GPU:

from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

假设将上述代码存放在test_gpu.py中,运行test_gpu.py,如果输出如下结果:

[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)]
Looping 1000 times took 3.06635117531 seconds
Result is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761
  1.62323284]
Used the cpu

则说明当前使用的是CPU,并没有使用GPU。
若出现类似如下结果:

Using gpu device 0: GeForce GTX 580
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.638810873032 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the gpu

这说明当前使用了GPU,并且告诉了我们当前使用的是哪个GPU。

如果你电脑上有GPU,并且你成功安装了CUDA,但是你的程序却没有使用GPU,那说明你当前的theano配置中默认是不使用GPU的,可以通过以下两个方式来使你的theano使用GPU。
1、在运行test_gpu.py时,在python test_gpu.py前加下语句:

# THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python test_gpu.py

2、配置你的.theanorc文件
在你home下面会有一个.theanorc文件,在这个文件中添加如下语句:

[global]
floatX = float32
device = gpu0

[lib]
cnmem = 1

注意:如果在你的home下没有发现.theanorc文件,按ctrl+h(显示隐藏文件)就可以看到了。

另外,方法一其实是一种覆盖型方式,即在运行当前的.py文件时,用当前的THEANO_FLAGS来覆盖.theanorc中默认的配置。

参考资料:

http://deeplearning.net/software/theano/tutorial/using_gpu.html

http://stackoverflow.com/questions/33519182/testing-theano-code-how-is-using-gpu

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要配置Anaconda以使用Theano GPU,您需要按照以下步骤进行操作: 1. 首先,确保您已经安装了Anaconda。如果还没有安装,请从Anaconda官网下载适合您操作系统的版本并进行安装。 2. 在Anaconda中创建一个新的环境,并激活该环境。您可以使用以下命令在命令行中执行此操作: ``` conda create -n theano python=2.7 source activate theano ``` 3. 安装Theano和Lasagne库。您可以使用以下命令在激活了"Theano"环境的命令行中执行此操作: ``` pip install --upgrade https://github.com/Theano/Theano/archive/master.zip pip install --upgrade https://github.com/Lasagne/Lasagne/archive/master.zip ``` 4. 安装CUDA和cuDNN。请确保您已经正确安装了与您的GPU兼容的CUDA版本,并且已经在系统中配置好了cuDNN。有关如何安装和配置CUDA和cuDNN的详细说明,请参阅官方文档。 5. 配置Theano使用GPU。在"Theano"环境下,您可以创建一个名为".theanorc"的文件,并将以下内容添加到该文件中: ``` [global] device = cuda0 floatX = float32 ``` 6. 确保您已经安装了OpenCV库,您可以使用以下命令在"Theano"环境下进行安装: ``` pip install opencv-python ``` 通过按照上述步骤进行操作,您应该能够成功配置Anaconda以使用Theano GPU。请注意,如果您的GPU不兼容CUDA或您未正确配置cuDNN,可能会出现问题。在配置过程中,最好参考Theano和Anaconda的官方文档以获取更多帮助和支持。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [ubuntu16.04 Anaconda虚拟环境配置theano gpu版本](https://blog.csdn.net/cyc651130/article/details/81412358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Windows Theano GPU 版配置](https://blog.csdn.net/weixin_39801356/article/details/114970605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值