Theano一览

Theano是一个Python库,它允许你定义、优化和求值数学表达式,特别是具有多维数组(numpy.ndarray)的数学表达式。对于涉及大量数据的问题,使用Theano可以获得与手工编写的C实现不相上下的速度。它还可以通过利用最近的GPU超过CPU上的C多个数量级。

Theano combines aspects of a computer algebra system (CAS) with aspects of an optimizing compiler. It can also generate customized C code for many mathematical operations. CAS与优化编译的这种组合对于复杂数学表达式重复求值并且求值速度很关键的任务特别有用。对于许多不同的表达式每个求值一次的情况,Theano可以最小化编译/分析的开销,但仍然提供诸如自动微分等符号特征。

Theano的编译器对这些符号表达式应用许多不同复杂度的优化。These optimizations include, but are not limited to:

  • use of GPU for computations
  • constant folding
  • merging of similar subgraphs, to avoid redundant calculation
  • 算术简化(例如x*y/x -> y, --x -> x
  • inserting efficient BLAS operations (e.g. GEMM) in a variety of contexts
  • using memory aliasing to avoid calculation
  • 使用就地操作,无论它涉不涉及到别名
  • loop fusion for elementwise sub-expressions
  • 数值稳定性的改进(例如\log(1+\exp(x)) and \log(\sum_i \exp(x[i])))
  • 完整列表请参阅优化

Theano was written at the LISA lab to support rapid development of efficient machine learning algorithms. Theano以希腊数学家命名,她可能是毕达哥拉斯的妻子。Theano is released under a BSD license (link).

先睹为快

Here is an example of how to use Theano. 它没有展示Theano的许多功能,但它具体说明了Theano是什么。

import theano
from theano import tensor

# declare two symbolic floating-point scalars
a = tensor.dscalar()
b = tensor.dscalar()

# create a simple expression
c = a + b

# convert the expression into a callable object that takes (a,b)
# values as input and computes a value for c
f = theano.function([a,b], c)

# bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'
assert 4.0 == f(1.5, 2.5)

Theano不是一个正常意义上的编程语言,因为你在Python中编写一个程序来为Theano构建表达式。在某种程度上它仍然像一个编程语言,因为你必须

  • declare variables (a,b) and give their types
  • 构建表达式来表示如何将这些变量放在一起
  • 将表达式图编译为函数,以便将它们用于计算。

It is good to think of theano.function as the interface to a compiler which builds a callable object from a purely symbolic graph. Theano的最重要的特性之一是theano.function可以优化图,甚至将其中的一些或全部编译为本机机器指令。

相对其他类似库的特色

Theano是一个Python库和优化编译器,用于处理和求值表达式,特别是矩阵表达式。矩阵的操作通常使用numpy包来完成,那么什么是Theano做的而Python和numpy没有做的呢?

  • 执行速度优化:Theano可以使用g++nvcc将表达式图的部分编译成CPU或GPU指令,它们运行起来比纯Python快得多。
  • 符号微分:Theano可以自动构建用于计算梯度的符号图。
  • stability optimizations: Theano can recognize [some] numerically unstable expressions and compute them with more stable algorithms.

The closest Python package to Theano is sympy. Theano比Sympy更注重张量表达,并有更多的机制进行编译。Sympy has more sophisticated algebra rules and can handle a wider variety of mathematical operations (such as series, limits, and integrals).

如果将numpyMATLABsympyMathematica进行比较,Theano是一种试图结合两个世界的最好的部分的东西。

入门

安装Theano
在你的系统上下载并安装Theano的说明。
教程
Getting started with Theano’s basic features. 如果你是新手,去这里!
API文档
Details of what Theano provides. 建议先通读教程

A PDF version of the online documentation may be found here.

Theano的愿景

This is the vision we have for Theano. 这是给人们对Theano未来的一个期望,但我们不能承诺实现所有的。This should also help you to understand where Theano fits in relation to other computational tools.

  • Support tensor and sparse operations
  • Support linear algebra operations
  • 图变换
    • 微分/高阶微分
    • 'R'和'L'微分运算符
    • Speed/memory optimizations
    • Numerical stability optimizations
  • 可以使用多种编译语言、指令集:C/C++、CUDA、OpenCL、PTX、CAL、AVX ...
  • 延迟求值
  • Loop
  • 并行执行(SIMD、多核,集群上的多节点,分布式多节点)
  • 支持NumPy所有功能和SciPy的基本功能
  • 在Theano中轻松封装库函数

注意:短期没有计划支持多节点计算。

Theano愿景的状态

Here is the state of that vision as of December 3th, 2013 (after Theano release 0.6):

  • We support tensors using the numpy.ndarray object and we support many operations on them.
  • 我们通过使用scipy.{csc,csr,bsr} _matrix对象支持稀疏类型,并支持对它们的一些操作。
  • 我们已经开始实现/封装更高级的线性代数运算。
  • 我们有许多图变换,涵盖上面列出的4个类别。
  • 我们可以通过更好的存储优化和指令选择来改进图转换。
    • 类似于在优化阶段的自动调整,但这不适用于只有1个的操作。
    • Example of use: Determine if we should move computation to the GPU or not depending on the input size.
    • 可能实现:允许fgraph中的Theano变量拥有超过1个所有者。
  • We support Python 2 and Python 3.
  • 我们对float32类型的张量有一个CUDA后端。
  • Efforts have begun towards a generic GPU ndarray (GPU tensor) (started in the libgpuarray project)
    • Move GPU backend outside of Theano.
    • Will provide better support for GPU on Windows and support an OpenCL backend on CPU.
  • Loops work, but not all related optimizations are currently done.
  • cvm链接器允许延迟求值。It is the current default linker.
    • How to have DebugMode check it? 目前,DebugMode非延迟地检查计算。
  • SIMD parallelism on the CPU comes from the compiler.
  • Multi-core parallelism support limited. If the external BLAS implementation supports it, many dot are parallelized via gemm, gemv and ger. Also, element-wise operation are supported. See Multi cores support in Theano.
  • No multi-node support.
  • 实现大多数但不是所有NumPy的函数/别名。* https://github.com/Theano/Theano/issues/1080
  • 将现有的Python函数封装的更简单并写成文档。
  • 我们知道如何从对象类型(张量、稀疏矩阵、dtype、broadcast 标志)分离共享变量内存存储位置,但我们需要这样做。

联系我们

Discussion about Theano takes place in the theano-dev and theano-users mailing lists. People interested in development of Theano should check the former, while the latter is reserved for issues that concern the end users.

问题、评论、赞美、批评和错误报告应提交到这些邮件列表。

We welcome all kinds of contributions. 如果你对如何扩展Theano有任何问题,请随时问问theano-dev邮件列表。