Theano

维基百科,自由的百科全书
跳转到导航 跳转到搜索
Theano
File:Theano logo.svg
原作者蒙特利尔大学蒙特利尔学习算法研究所英语Montreal Institute for Learning Algorithms(MILA)
开发者PyMC开发团队
首次发布2007年,​19年前​(2007
当前版本
    Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
    源代码库
    • {{URL|example.com|可选的显示文本}}
    Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
    编程语言Python, CUDA
    引擎
      Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
      平台Linux, macOS, Windows
      类型机器学习, 函式库
      许可协议3条款BSD许可证

      Theano及其分叉PyTensor,是一个Python库和优化的编译器,用来操纵和求值数学表达式特别是矩阵值表达式[1]。在其中,计算使用NumPy风格的语法来表达并被编译,用来在CPU或者GPU架构上高效的运行。

      历史[编辑]

      Theano是开源项目[2],主要由蒙特利尔大学蒙特利尔学习算法研究所英语Montreal Institute for Learning Algorithms(MILA)开发[3]。软件名字取自古代哲学家Theano英语Theano (philosopher)。在2017年9月28日,Pascal Lamblin发布了来自约书亚·本希奥的一则信息,MILA负责人说:由于更强大的工业参与者的竞争,主要的开发在1.0发行之后将会停止[4]。Theano 1.0.0随后在2017年11月15日发行[5]

      在2018年5月17日,Chris Fonnesbeck代表PyMC开发团队写道:PyMC开发者将在他们退场后取得对Theano维护的控制权[6]。在2021年1月绝大部分的Theano代码基被重新建造,并增加了通过JAXNumba的编译,修订后的这个计算后端以新名字Aesara发行。2022年11月28日,PyMC团队宣布采用从Aesara计划分叉出PyTensor[7]

      样例代码[编辑]

      下列代码以PyTensor用作介绍的例子:

      import pytensor
      from pytensor import tensor as pt
      
      # 声明2个符号浮点标量
      a = pt.dscalar("a")
      b = pt.dscalar("b")
      
      # 建立一个简单的表达式
      c = a + b
      
      # 将这个表达式转换成一个可调用对象,
      # 它接收'(a, b)'值作为输入并计算出一个值给'c'
      f_c = pytensor.function([a, b], c)
      
      assert f_c(1.5, 2.5) == 4.0
      
      # 计算样例表达式关于'a'的梯度
      dc = pytensor.grad(c, a)
      
      f_dc = pytensor.function([a, b], dc)
      
      assert f_dc(1.5, 2.5) == 1.0
      
      >>> import pytensor
      >>> from pytensor import tensor as pt
      >>>
      >>> # 通过'pytensor.function'编译函数还能优化表达式图
      >>> # 它会移除不必要的运算并将特定运算替代为更有效的运算
      >>> 
      >>> v = pt.vector("v")
      >>> M = pt.matrix("M")
      >>> 
      >>> d = a/a + (M + a).dot(v)
      >>> 
      >>> pytensor.dprint(d)
      Add [id A]
       ├─ ExpandDims{axis=0} [id B]
       │  └─ True_div [id C]
       │     ├─ a [id D]
       │     └─ a [id D]
       └─ dot [id E]
          ├─ Add [id F]
          │  ├─ M [id G]
          │  └─ ExpandDims{axes=[0, 1]} [id H]
          │     └─ a [id D]
          └─ v [id I]
      <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
      >>> 
      >>> f_d = pytensor.function([a, v, M], d)
      >>> 
      >>> # 'a/a' -> '1'而点积被替代为BLAS函数(i.e. CGemv)
      >>> pytensor.dprint(f_d)
      Add [id A] 5
       ├─ [1.] [id B]
       └─ CGemv{inplace} [id C] 4
          ├─ AllocEmpty{dtype='float64'} [id D] 3
          │  └─ Shape_i{0} [id E] 2
          │     └─ M [id F]
          ├─ 1.0 [id G]
          ├─ Add [id H] 1
          │  ├─ M [id F]
          │  └─ ExpandDims{axes=[0, 1]} [id I] 0
          │     └─ a [id J]
          ├─ v [id K]
          └─ 0.0 [id L]
      <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
      

      参见[编辑]

      引用[编辑]

      1. ^ Bergstra, J.; O. Breuleux; F. Bastien; P. Lamblin; R. Pascanu; G. Desjardins; J. Turian; D. Warde-Farley; Y. Bengio. Theano: A CPU and GPU Math Expression Compiler (PDF). Proceedings of the Python for Scientific Computing Conference (SciPy) 2010. 30 June 2010 [2020-11-06]. (原始内容存档 (PDF)于2020-11-01). 
      2. ^ Github Repository. [2020-11-06]. (原始内容存档于2020-11-16). 
      3. ^ deeplearning.net. [2020-11-06]. (原始内容存档于2017-12-13). 
      4. ^ Lamblin, Pascal. MILA and the future of Theano. theano-users (邮件列表). 28 September 2017 [28 September 2017]. (原始内容存档于2011-01-22). 
      5. ^ Release Notes – Theano 1.0.0 documentation. [2020-11-06]. (原始内容存档于2020-09-14). 
      6. ^ Developers, PyMC. Theano, TensorFlow and the Future of PyMC. Medium. 2019-06-01 [2019-08-27]. (原始内容存档于2020-08-06) (English). 
      7. ^ PyMC forked Aesara to PyTensor. [2023-08-17]. (原始内容存档于2023-07-18). 

      外部链接[编辑]