Theano 简明教程
Theano - Installation
Theano 可以安装在 Windows、MacOS 和 Linux 上。在所有情况下,安装都很简单。在安装 Theano 之前,您必须安装其依赖项。以下是依赖项列表:
Theano can be installed on Windows, MacOS, and Linux. The installation in all the cases is trivial. Before you install Theano, you must install its dependencies. The following is the list of dependencies −
-
Python
-
NumPy − Required
-
SciPy − Required only for Sparse Matrix and special functions
-
BLAS − Provides standard building blocks for performing basic vector and matrix operations
您可以根据需要选择安装的可选软件包:
The optional packages that you may choose to install depending on your needs are −
-
nose: To run Theano’s test-suite
-
Sphinx − For building documentation
-
Graphiz and pydot − To handle graphics and images
-
NVIDIA CUDA drivers − Required for GPU code generation/execution
-
libgpuarray − Required for GPU/CPU code generation on CUDA and OpenCL devices
我们将在 MacOS 中讨论安装 Theano 的步骤。
We shall discuss the steps to install Theano in MacOS.
MacOS Installation
要安装 Theano 及其依赖项,请您在命令行中使用 pip ,如下所示。这是本教程中我们将要用到的最小依赖项。
To install Theano and its dependencies, you use pip from the command line as follows. These are the minimal dependencies that we are going to need in this tutorial.
$ pip install Theano
$ pip install numpy
$ pip install scipy
$ pip install pydot
您还需要使用以下命令安装 OSx 命令行开发人员工具:
You also need to install OSx command line developer tool using the following command −
$ xcode-select --install
将会看到以下屏幕。点击 Install 按钮来安装工具。
You will see the following screen. Click on the Install button to install the tool.
在成功安装后,将会在控制台看到成功信息。
On successful installation, you will see the success message on the console.
Testing the Installation
在 Anaconda Jupyter 中成功完成安装后,打开一个新笔记本。在代码单元格中,输入以下 Python 脚本:
After the installation completes successfully, open a new notebook in the Anaconda Jupyter. In the code cell, enter the following Python script −
Example
import theano
from theano import tensor
a = tensor.dscalar()
b = tensor.dscalar()
c = a + b
f = theano.function([a,b], c)
d = f(1.5, 2.5)
print (d)
Output
执行脚本,你应该看到以下输出:
Execute the script and you should see the following output −
4.0
执行屏幕快照在你快速参考时显示在下方:
The screenshot of the execution is shown below for your quick reference −
如果你获取了上面的输出,你的 Theano 安装就成功了。如果没有,请按照 Theano 下载页面上的调试说明来修复问题。
If you get the above output, your Theano installation is successful. If not, follow the debug instructions on Theano download page to fix the issues.
What is Theano?
现在你已经成功安装 Theano,我们首先来了解一下什么是 Theano?Theano 是一个 Python 库。它能让你定义、优化和评估数学表达式,特别是应用于机器学习模型开发的表达式。Theano 本身不包含任何预定义的 ML 模型;它只帮助其开发。它在处理多维数组时特别有用。它与 NumPy 无缝集成,而 NumPy 是 Python 中用于科学计算的基本且广泛使用的软件包。
Now that you have successfully installed Theano, let us first try to understand what is Theano? Theano is a Python library. It lets you define, optimize, and evaluate mathematical expressions, especially the ones which are used in Machine Learning Model development. Theano itself does not contain any pre-defined ML models; it just facilitates its development. It is especially useful while dealing with multi-dimensional arrays. It seamlessly integrates with NumPy, which is a fundamental and widely used package for scientific computations in Python.
Theano 用于定义机器学习开发中使用的数学表达式。此类表达式通常涉及矩阵运算、微分、梯度计算等。
Theano facilitates defining mathematical expressions used in ML development. Such expressions generally involve Matrix Arithmetic, Differentiation, Gradient Computation, and so on.
Theano 首先为模型构建整个计算图。然后通过对图应用多个优化技术将其编译为高效代码。编译后的代码由 Theano 中 function 可用的特殊操作注入到 Theano 运行时。我们重复执行 function 以训练神经网络。与使用纯 Python 编码甚至完整的 C 实现相比,训练时间大大减少。
Theano first builds the entire Computational Graph for your model. It then compiles it into highly efficient code by applying several optimization techniques on the graph. The compiled code is injected into Theano runtime by a special operation called function available in Theano. We execute this function repetitively to train a neural network. The training time is substantially reduced as compared to using pure Python coding or even a full C implementation.
现在我们将了解 Theano 开发过程。让我们从如何在 Theano 中定义数学表达式开始。
We shall now understand the process of Theano development. Let us begin with how to define a mathematical expression in Theano.