本文爲Lancelod_Liu編譯, 轉載請保留此行.
This Article is translated and edited by
Lancelod_Liu, keep this line if you share it.
原文:戳.
初步 - 基本運算
兩個標量相加
下面的語句展示瞭如何使用Theano進行計算2個數的和.
>>> import theano.tensor as T
>>> from theano import function
>>> x = T.dscalar('x')
>>> y = T.dscalar('y')
>>> z = x + y
>>> f = function([x, y], z)
我們可以使用上面創建的函數進行計算任意的兩個數(就和C語言的函數一樣)
>>> f(2, 3)
array(5.0)
>>> f(16.3, 12.1)
array(28.4)
一步一步地拆分這堆代碼吧. 第一步是定義2個符號(變量), 它們代表了你想要相加的值. 注意從現在開始, 我們會使用"變量"來指代"符號"(x,y,z都是變量對象). 函數f的輸出是一個numpy.ndarray, 只不過是0維的(標量).
當你鍵入這兩行代碼的時候你會發現執行function指令會有一些延遲. 在底層, f被編譯成C代碼.
Step 1
>>> x = T.dscalar('x')
>>> y = T.dscalar('y')
在Theano中, 所有的符號都必須被顯示聲明. 比如, T.dscalar 是我們賦予的0維(scalar)浮點(d)數組. 它是一個 TheanoType.
dscalar 並非一個類. 因此, x和y實際上都不是dscalar的實例. 它們都是TensorVariable的實例. x和y實際上僅僅是在它們的type域中賦了dscalar的值. 如下:
>>> type(x)
<class 'theano.tensor.basic.TensorVariable'>
>>> x.type
TensorType(float64, scalar)
>>> T.dscalar
TensorType(float64, scalar)
>>> x.type is T.dscalar
True
通過帶字符串參數調用T.dscalar, 你就創建了一個代表浮點標量的變量, 其名爲字符串參數. 如果你沒有給出字符串參數, 那麼這個符號變量就沒有名字. 名字並非必須, 但有助於調試. 講到Theano的結構時我們會講解更多. 你也可以在這裏學習更多:Graph Structures.
Step 2
第二步是把x 和 y 相加得到和 z:
>>> z = x + y
z 是另一個變量, 代表着x和y的和.你可以使用pp函數來"優雅"地打印出與z相關的運算.
>>> from theano import pp
>>> print pp(z)
(x + y)
Step 3
最後一步是把 x 和 y 作爲輸入, 把z作爲輸出:
>>> f = function([x, y], z)
function 的第一個參數是一個變量列表, 它們是函數的輸入. 第二個參數可以是變量或變量列表. 兩種情況下, 第二個參數都是我們使用函數時希望看到的輸出. f 此時就可以作爲一個普通Python函數來使用了.
注意
你可以跳過第三步來快速使用這個特性. 只要使用一個變量的eval() 方法.eval() 方法並不如function() 靈活 但它幾乎可以解決任何我們在這個教程中遇到的問題. 它使得你不需要import function(), 下面是eval()的用法:
>>> import theano.tensor as T
>>> x = T.dscalar('x')
>>> y = T.dscalar('y')
>>> z = x + y
>>> z.eval({x : 16.3, y : 12.1})
array(28.4)
我們給 eval() 傳遞了一個字典, 映射了符號變量<->值, 它返回了計算表達式的結果.
eval() 首次調用時會比較慢, 它需要調用function()
來編譯表達式. 之後使用同樣的變量調用eval()將會很快, 因爲變量緩存了編譯好的函數.
Adding two Matrices
你可能已經會了. 實際上, 唯一的改變就是你需要使用矩陣類型初始化x和y:
>>> x = T.dmatrix('x')
>>> y = T.dmatrix('y')
>>> z = x + y
>>> f = function([x, y], z)
dmatrix 是浮點型矩陣. 我們可以對2D arrays使用新的函數 :
>>> f([[1, 2], [3, 4]], [[10, 20], [30, 40]])
array([[ 11., 22.],
[ 33., 44.]])
變量是一個 NumPy 數組(array). 我們也可以使用NumPy數組(array)直接做輸入:
>>> import numpy
>>> f(numpy.array([[1, 2], [3, 4]]), numpy.array([[10, 20], [30, 40]]))
array([[ 11., 22.],
[ 33., 44.]])
矩陣和標量/標量, 向量和標量的相加都是可以的. 具體的變化參考 broadcasting.
共有下面的變量類型:
- byte: bscalar,bvector, bmatrix, brow, bcol, btensor3, btensor4
- 16-bit integers: wscalar,wvector, wmatrix, wrow, wcol, wtensor3, wtensor4
- 32-bit integers: iscalar,ivector, imatrix, irow, icol, itensor3, itensor4
- 64-bit integers: lscalar,lvector, lmatrix, lrow, lcol, ltensor3, ltensor4
- float: fscalar,fvector, fmatrix, frow, fcol, ftensor3, ftensor4
- double: dscalar,dvector, dmatrix, drow, dcol, dtensor3, dtensor4
- complex: cscalar,cvector, cmatrix, crow, ccol, ctensor3, ctensor4
所有的變量類型參考: tensor creation.
注意
使用者--而不是系統架構者--必須選擇你使用32還是64位的整數(i或l前綴)和浮點(f和d前綴).
Exercise
import theano
a = theano.tensor.vector() # declare variable
out = a + a ** 10 # build symbolic expression
f = theano.function([a], out) # compile function
print f([0, 1, 2]) # prints `array([0, 2, 1026])`
Modify and execute this code to compute this expression: a ** 2 + b ** 2 + 2 * a * b.