Theano 简明教程
Theano - Computational Graph
从上述两个示例中,您可能已经注意到,在 Theano 中,我们创建一个表达式,该表达式最终使用 Theano function 进行评估。Theano 使用高级优化技术来优化表达式的执行。为了可视化计算图,Theano 在其库中提供了 printing 软件包。
From the above two examples, you may have noticed that in Theano we create an expression which is eventually evaluated using the Theano function. Theano uses advanced optimization techniques to optimize the execution of an expression. To visualize the computation graph, Theano provides a printing package in its library.
Symbolic Graph for Scalar Addition
若要查看标量加法程序的计算图,请使用打印库如下:
To see the computation graph for our scalar addition program, use the printing library as follows −
theano.printing.pydotprint(f, outfile="scalar_addition.png", var_with_name_simple=True)
执行此语句后,将在您的机器上创建一个名为 scalar_addition.png 的文件。保存的计算图在此处显示,供您快速参考:
When you execute this statement, a file called scalar_addition.png will be created on your machine. The saved computation graph is displayed here for your quick reference −
以下给出生成上述图像的完整程序清单:
The complete program listing to generate the above image is given below −
from theano import *
a = tensor.dscalar()
b = tensor.dscalar()
c = a + b
f = theano.function([a,b], c)
theano.printing.pydotprint(f, outfile="scalar_addition.png", var_with_name_simple=True)
Symbolic Graph for Matrix Multiplier
现在,尝试为我们的矩阵乘法器创建计算图。生成此图的完整清单如下:
Now, try creating the computation graph for our matrix multiplier. The complete listing for generating this graph is given below −
from theano import *
a = tensor.dmatrix()
b = tensor.dmatrix()
c = tensor.dot(a,b)
f = theano.function([a,b], c)
theano.printing.pydotprint(f, outfile="matrix_dot_product.png", var_with_name_simple=True)
生成的图形如下所示:
The generated graph is shown here −
Complex Graphs
在较大表达式中,计算图形可能非常复杂。此处显示了从 Theano 文档中获取的此类图形:
In larger expressions, the computational graphs could be very complex. One such graph taken from Theano documentation is shown here −
为了理解 Theano 的工作原理,首先了解这些计算图的重要性非常重要。有了这种理解,我们就会知道 Theano 的重要性。
To understand the working of Theano, it is important to first know the significance of these computational graphs. With this understanding, we shall know the importance of Theano.
Why Theano?
通过查看计算图形的复杂性,您现在将能够理解开发 Theano 背后的目的。典型的编译器会在程序中提供本地优化,因为它从不会将整个计算视为一个整体。
By looking at the complexity of the computational graphs, you will now be able to understand the purpose behind developing Theano. A typical compiler would provide local optimizations in the program as it never looks at the entire computation as a single unit.
Theano 实施非常高级的优化技术来优化完整的计算图。它将代数方面与优化编译器方面结合在一起。图形的一部分可以编译成 C 语言代码。对于重复计算,评估速度至关重要,Theano 通过生成非常有效的代码来满足此目的。
Theano implements very advanced optimization techniques to optimize the full computational graph. It combines the aspects of Algebra with aspects of an optimizing compiler. A part of the graph may be compiled into C-language code. For repeated calculations, the evaluation speed is critical and Theano meets this purpose by generating a very efficient code.