Ubuntu14.04安装Theano并配置GPU环境

需积分: 50 16 下载量 47 浏览量 更新于2024-09-11 收藏 1005B TXT 举报
"这篇教程主要介绍了在Ubuntu 14.04上安装Theano,包括环境变量的设置,以及一个简单的测试脚本来验证安装是否成功。Theano是一个Python库,用于数值计算,尤其在深度学习领域广泛应用。" 在安装Theano之前,首先确保系统已安装了必要的依赖项。在Ubuntu 14.04上,你需要安装以下软件包: 1. `python-numpy`:提供高效的多维数组对象和相关数学函数。 2. `python-scipy`:科学计算工具包,包含很多数学、科学和工程计算所需的函数。 3. `python-dev`:Python开发头文件和库,用于编译和链接Python扩展模块。 4. `python-pip`:Python包管理器,用于安装和管理Python软件包。 5. `python-nose`:Python测试工具,用于自动化测试。 6. `g++`:GCC编译器,用于编译C++代码。 安装这些依赖项的命令如下: ```bash sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ ``` 接下来,使用pip安装Theano: ```bash sudo pip install Theano ``` 安装完成后,为了充分利用Theano的性能,需要配置环境变量。打开或创建`~/.bashrc`文件,并添加以下内容: ```bash export THEANO_FLAGS="mode=FAST_RUN,device=gpu,floatX=float32" ``` 这将指定Theano运行在GPU上,并且数据类型为float32,以提高计算速度。然后,重启终端或运行`source ~/.bashrc`使更改生效。 此外,还可以创建一个`.theaonrc`文件来保存Theano的配置,这样每次启动时都会自动应用这些设置。在`~/.theaonrc`文件中写入: ```ini [global] device = gpu floatX = float32 [nvcc] fastmath = True ``` `nvcc`部分的`fastmath=True`启用了一些快速但可能不精确的数学运算,这有助于提升计算速度。 为了验证Theano是否正确安装并能使用GPU,可以编写并运行一个简单的测试脚本`test.py`: ```python from theano import function, config, shared, sandbox import theano.tensor as T import numpy import time vlen = 10 * 30 * 768 # 10x#coresx#threadspercore 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' % iters, t1 - t0, 'seconds') print('Result is', 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') ``` 这个脚本创建了一个共享变量,定义了一个函数,然后执行该函数多次以测量执行时间。最后,它会输出是CPU还是GPU被用于计算。 运行这个脚本,如果看到"Used the gpu",那么恭喜你,Theano已经成功安装并且能在GPU上运行了。如果你遇到任何问题,可能需要检查NVIDIA驱动、CUDA工具包和cuDNN是否已经正确安装并配置。
2017-08-16 上传
Deep Learning with Theano by Christopher Bourez English | 31 July 2017 | ISBN: 1786465825 | ASIN: B073ZFBQQJ | 300 Pages | AZW3 | 6.98 MB Develop deep neural networks in Theano with practical code examples for image classification, machine translation, reinforcement agents, or generative models. About This Book Learn Theano basics and evaluate your mathematical expressions faster and in an efficient manner Learn the design patterns of deep neural architectures to build efficient and powerful networks on your datasets Apply your knowledge to concrete fields such as image classification, object detection, chatbots, machine translation, reinforcement agents, or generative models. Who This Book Is For This book is indented to provide a full overview of deep learning. From the beginner in deep learning and artificial intelligence, to the data scientist who wants to become familiar with Theano and its supporting libraries, or have an extended understanding of deep neural nets. Some basic skills in Python programming and computer science will help, as well as skills in elementary algebra and calculus. What You Will Learn Get familiar with Theano and deep learning Provide examples in supervised, unsupervised, generative, or reinforcement learning. Discover the main principles for designing efficient deep learning nets: convolutions, residual connections, and recurrent connections. Use Theano on real-world computer vision datasets, such as for digit classification and image classification. Extend the use of Theano to natural language processing tasks, for chatbots or machine translation Cover artificial intelligence-driven strategies to enable a robot to solve games or learn from an environment Generate synthetic data that looks real with generative modeling Become familiar with Lasagne and Keras, two frameworks built on top of Theano In Detail This book offers a complete overview of Deep Learning with Theano, a Python-based library that makes optimizing numerical expressions and deep learning models easy on CPU or GPU. The book provides some practical code examples that help the beginner understand how easy it is to build complex neural networks, while more experimented data scientists will appreciate the reach of the book, addressing supervised and unsupervised learning, generative models, reinforcement learning in the fields of image recognition, natural language processing, or game strategy. The book also discusses image recognition tasks that range from simple digit recognition, image classification, object localization, image segmentation, to image captioning. Natural language processing examples include text generation, chatbots, machine translation, and question answering. The last example deals with generating random data that looks real and solving games such as in the Open-AI gym. At the end, this book sums up the best -performing nets for each task. While early research results were based on deep stacks of neural layers, in particular, convolutional layers, the book presents the principles that improved the efficiency of these architectures, in order to help the reader build new custom nets. Style and approach It is an easy-to-follow example book that teaches you how to perform fast, efficient computations in Python. Starting with the very basics-NumPy, installing Theano, this book will take you to the smooth journey of implementing Theano for advanced computations for machine learning and deep learning.