资讯专栏INFORMATION COLUMN

Windows Theano GPU 版配置

Yu_Huang / 3475人阅读

摘要:因为自己在上的里面第四周的要用到,然后这个似乎是基于后端的。然而版太慢了,跑个马尔科夫蒙特卡洛要个小时,简直不能忍了。为了不把环境搞坏,我在里面新建了一个环境。

因为自己在上Coursera的Advanced Machine Learning, 里面第四周的Assignment要用到PYMC3,然后这个似乎是基于theano后端的。然而CPU版TMD太慢了,跑个马尔科夫蒙特卡洛要10个小时,简直不能忍了。所以妥妥换gpu版。

为了不把环境搞坏,我在Anaconda里面新建了一个环境。(关于Anaconda,可以看我之前翻译的文章)

</>复制代码

  1. Conda Create -n theano-gpu python=3.4

(theano GPU版貌似不支持最新版,保险起见装了旧版)

</>复制代码

  1. conda install theano pygpu

这里面会涉及很多依赖,应该conda会给你搞好,缺什么的话自己按官方文档去装。

然后至于Cuda和Cudnn的安装,可以看我写的关于TF安装的教程

和TF不同的是,Theano不分gpu和cpu版,用哪个看配置文件设置,这一点是翻博客了解到的:
配置好Theano环境之后,只要 C:Users你的用户名 的路径下添加 .theanorc.txt 文件。

.theanorc.txt 文件内容:

</>复制代码

  1. [global]
  2. openmp=False
  3. device = cuda
  4. floatX = float32
  5. base_compiler = C:Program Files (x86)Microsoft Visual Studio 12.0VCin
  6. allow_input_downcast=True
  7. [lib]
  8. cnmem = 0.75
  9. [blas]
  10. ldflags=
  11. [gcc]
  12. cxxflags=-IC:UserslyhAnaconda2MinGW
  13. [nvcc]
  14. fastmath = True
  15. flags = -LC:UserslyhAnaconda2libs
  16. compiler_bindir = C:Program Files (x86)Microsoft Visual Studio 12.0VCin
  17. flags = -arch=sm_30

注意在新版本中,声明用gpu从device=gpu改为device=cuda

然后测试是否成功:

</>复制代码

  1. from theano import function, config, shared, tensor
  2. import numpy
  3. import time
  4. vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
  5. iters = 1000
  6. rng = numpy.random.RandomState(22)
  7. x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
  8. f = function([], tensor.exp(x))
  9. print(f.maker.fgraph.toposort())
  10. t0 = time.time()
  11. for i in range(iters):
  12. r = f()
  13. t1 = time.time()
  14. print("Looping %d times took %f seconds" % (iters, t1 - t0))
  15. print("Result is %s" % (r,))
  16. if numpy.any([isinstance(x.op, tensor.Elemwise) and
  17. ("Gpu" not in type(x.op).__name__)
  18. for x in f.maker.fgraph.toposort()]):
  19. print("Used the cpu")
  20. else:
  21. print("Used the gpu")

输出:

</>复制代码

  1. [GpuElemwise{exp,no_inplace}((float32, vector)>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
  2. Looping 1000 times took 0.377000 seconds
  3. Result is [ 1.23178029 1.61879349 1.52278066 ..., 2.20771813 2.29967761
  4. 1.62323296]
  5. Used the gpu

到这里就算配好了

然后在作业里面,显示Quadro卡启用

但是还是有个warning

</>复制代码

  1. WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.

这个真不知道怎么处理

然后后面运行到:

</>复制代码

  1. with pm.Model() as logistic_model:
  2. # Since it is unlikely that the dependency between the age and salary is linear, we will include age squared
  3. # into features so that we can model dependency that favors certain ages.
  4. # Train Bayesian logistic regression model on the following features: sex, age, age^2, educ, hours
  5. # Use pm.sample to run MCMC to train this model.
  6. # To specify the particular sampler method (Metropolis-Hastings) to pm.sample,
  7. # use `pm.Metropolis`.
  8. # Train your model for 400 samples.
  9. # Save the output of pm.sample to a variable: this is the trace of the sampling procedure and will be used
  10. # to estimate the statistics of the posterior distribution.
  11. #### YOUR CODE HERE ####
  12. pm.glm.GLM.from_formula("income_more_50K ~ sex+age + age_square + educ + hours", data, family=pm.glm.families.Binomial())
  13. with logistic_model:
  14. trace = pm.sample(400, step=[pm.Metropolis()]) #nchains=1 works for gpu model
  15. ### END OF YOUR CODE ###

这里出现的报错:

</>复制代码

  1. GpuArrayException: cuMemcpyDtoHAsync(dst, src->ptr + srcoff, sz, ctx->mem_s): CUDA_ERROR_INVALID_VALUE: invalid argument

这个问题最后github大神解决了:
So njobs will spawn multiple chains to run in parallel. If the model uses the GPU there will be a conflict. We recently added nchains where you can still run multiple chains. So I think running pm.sample(niter, nchains=4, njobs=1) should give you what you want.
我把:

</>复制代码

  1. trace = pm.sample(400, step=[pm.Metropolis()]) #nchains=1 works for gpu model

加上nchains就好了,应该是并行方面的问题

</>复制代码

  1. trace = pm.sample(400, step=[pm.Metropolis()],nchains=1, njobs=1) #nchains=1 works for gpu model

另外

</>复制代码

  1. plot_traces(trace, burnin=200)

出现pm.df_summary报错,把pm.df_summary 换成 pm.summary就好了,也是github搜出来的。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19745.html

相关文章

  • Windows Theano GPU 配置

    摘要:因为自己在上的里面第四周的要用到,然后这个似乎是基于后端的。然而版太慢了,跑个马尔科夫蒙特卡洛要个小时,简直不能忍了。为了不把环境搞坏,我在里面新建了一个环境。 因为自己在上Coursera的Advanced Machine Learning, 里面第四周的Assignment要用到PYMC3,然后这个似乎是基于theano后端的。然而CPU版TMD太慢了,跑个马尔科夫蒙特卡洛要10个...

    thekingisalwaysluc 评论0 收藏0
  • keras环境配置填坑(持续更新)

    摘要:检查目录以及其下的目录是否被添加进环境变量。导入版本时,提示缺少模块,用的函数绘制模型失败八成是没有安装下面两个包里面的无法识别八成是安装了加速版的,此版本支持的核心,把改成进时提示找不到解压直接覆盖目录的文件夹。 L.C.提醒我补上配置的坑 1.配置gpu版本的keras(tensorflow/theano)真xx难!对计算的时间要求不高,就弄个cpu慢吞吞训练算了,怎么安装cpu版...

    VEIGHTZ 评论0 收藏0
  • 最新Github上各DL框架Star数量大PK

    摘要:下图总结了绝大多数上的开源深度学习框架项目,根据项目在的数量来评级,数据采集于年月初。然而,近期宣布将转向作为其推荐深度学习框架因为它支持移动设备开发。该框架可以出色完成图像识别,欺诈检测和自然语言处理任务。 很多神经网络框架已开源多年,支持机器学习和人工智能的专有解决方案也有很多。多年以来,开发人员在Github上发布了一系列的可以支持图像、手写字、视频、语音识别、自然语言处理、物体检测的...

    oogh 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<