如何在Ipython Notebook上使用GPU? [英] How Can I use my GPU on Ipython Notebook?
问题描述
OS:Ubuntu 14.04LTS
语言:Python Anaconda 2.7(keras,theano)
显卡:GTX980Ti
CUDA:CUDA 7.5
OS : Ubuntu 14.04LTS
Language : Python Anaconda 2.7 (keras, theano)
GPU : GTX980Ti
CUDA : CUDA 7.5
我想使用我的GPU(GTX980Ti)在IPython Notebook上运行keras python代码
但是我找不到它.
I wanna run keras python code on IPython Notebook by using my GPU(GTX980Ti)
But I can't find it.
我想测试以下代码.当我在Ubuntu终端上运行它时, 我的命令如下(它很好地使用了GPU.没有问题)
I want to test below code. When I run it on to Ubuntu terminal, I command as below (It uses GPU well. It doesn't have any problem)
首先,我按如下所示设置路径
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
第二,我按如下方式运行代码
THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath=True' python myscript.py
它运行良好.
And it runs well.
但是当我在pycharm(python IDE)上运行代码或 当我在Ipython Notebook上运行它时,它不使用gpu. 它仅使用CPU
But when i run the code on pycharm(python IDE) or When I run it on Ipython Notebook, It doesn't use gpu. It only uses CPU
myscript.py代码如下.
myscript.py code is as below.
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 xrange(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')
要解决此问题,我强制代码如下所述使用gpu (在myscript.py上再插入两行)
To solve it, I force the code use gpu as below (Insert two lines more on myscript.py)
import theano.sandbox.cuda
theano.sandbox.cuda.use("gpu0")
然后它会生成如下错误
ERROR (theano.sandbox.cuda): nvcc compiler not found on $PATH. Check your nvcc installation and try again.
该怎么做???我花了两天时间. 而且我肯定采用了在主目录中使用'.theanorc'文件的方式.
how to do it??? I spent two days..
And I surely did the way of using '.theanorc' file at home directory.
推荐答案
我正在ipython笔记本上使用theano,该笔记本利用了我系统的GPU.此配置似乎可以在我的系统上正常工作.(带有GTX 750M的Macbook Pro)
I'm using theano on an ipython notebook making use of my system's GPU. This configuration seems to work fine on my system.(Macbook Pro with GTX 750M)
我的〜/.theanorc文件:
My ~/.theanorc file :
[global]
cnmem = True
floatX = float32
device = gpu0
各种环境变量(我使用虚拟环境(macvnev):
Various environment variables (I use a virtual environment(macvnev):
echo $LD_LIBRARY_PATH
/opt/local/lib:
echo $PATH
/Developer/NVIDIA/CUDA-7.5/bin:/opt/local/bin:/opt/local/sbin:/Developer/NVIDIA/CUDA-7.0/bin:/Users/Ramana/projects/macvnev/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
echo $DYLD_LIBRARY_PATH
/Developer/NVIDIA/CUDA-7.5/lib:/Developer/NVIDIA/CUDA-7.0/lib:
我如何运行ipython笔记本(对我来说,设备是gpu0):
How I run ipython notebook (For me, the device is gpu0) :
$THEANO_FLAGS=mode=FAST_RUN,device=gpu0,floatX=float32 ipython notebook
$nvcc -V
的输出:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2015 NVIDIA Corporation
Built on Thu_Sep_24_00:26:39_CDT_2015
Cuda compilation tools, release 7.5, V7.5.19
在您的帖子中,可能您将$ PATH变量设置为错误.
From your post, probably you've set the $PATH variable wrong.
这篇关于如何在Ipython Notebook上使用GPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!