Theano及其分叉PyTensor,是一個Python庫和優化的編譯器,用來操縱和求值數學表達式特別是矩陣值表達式[2]。在其中,計算使用NumPy風格的語法來表達並被編譯,用來在CPU或者GPU架構上高效的運行。

Theano
原作者蒙特利爾大學蒙特利爾學習算法研究所英語Montreal Institute for Learning Algorithms(MILA)
開發者PyMC開發團隊
首次發布2007年,​17年前​(2007
當前版本
  • 2.20.0 (2024年3月28日;穩定版本)[1]
編輯維基數據鏈接
源代碼庫 編輯維基數據鏈接
編程語言Python, CUDA
平台Linux, macOS, Windows
類型機器學習, 函式庫
許可協議3條款BSD許可證
網站pytensor.readthedocs.io/en/latest/ 編輯維基數據

歷史 編輯

Theano是開源項目[3],主要由蒙特利爾大學蒙特利爾學習算法研究所英語Montreal Institute for Learning Algorithms(MILA)開發[4]。軟件名字取自古代哲學家Theano英語Theano (philosopher)。在2017年9月28日,Pascal Lamblin發布了來自約書亞·本希奧的一則信息,MILA負責人說:由於更強大的工業參與者的競爭,主要的開發在1.0發行之後將會停止[5]。Theano 1.0.0隨後在2017年11月15日發行[6]

在2018年5月17日,Chris Fonnesbeck代表PyMC開發團隊寫道:PyMC開發者將在他們退場後取得對Theano維護的控制權[7]。在2021年1月絕大部份的Theano代碼基被重新建造,並增加了通過JAXNumba的編譯,修訂後的這個計算後端以新名字Aesara發行。2022年11月28日,PyMC團隊宣布採用從Aesara計劃分叉出PyTensor[8]

樣例代碼 編輯

下列代碼以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. ^ Release 2.20.0. 2024年3月28日 [2024年4月25日]. 
  2. ^ 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). 
  3. ^ Github Repository. [2020-11-06]. (原始內容存檔於2020-11-16). 
  4. ^ deeplearning.net. [2020-11-06]. (原始內容存檔於2017-12-13). 
  5. ^ Lamblin, Pascal. MILA and the future of Theano. theano-users (郵件列表). 28 September 2017 [28 September 2017]. (原始內容存檔於2011-01-22). 
  6. ^ Release Notes – Theano 1.0.0 documentation. [2020-11-06]. (原始內容存檔於2020-09-14). 
  7. ^ Developers, PyMC. Theano, TensorFlow and the Future of PyMC. Medium. 2019-06-01 [2019-08-27]. (原始內容存檔於2020-08-06) (英語). 
  8. ^ PyMC forked Aesara to PyTensor. [2023-08-17]. (原始內容存檔於2023-07-18). 

外部連結 編輯