練習 Theano 的 Scan Function


In [1]:
import theano
import theano.tensor as T


Using gpu device 0: GeForce GTX 980 Ti (CNMeM is enabled with initial size: 95.0% of memory, cuDNN not available)
  • 撰寫一個 A 的 K 次計算函式
  • [0 1 2 3 4 5 6 7 8 9 ] >>> [ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
  • k 為 integer ,代表多少次方
  • a 為 底

In [33]:
k = T.iscalar('K')
a = T.vector('A')
i = T.vector('A')

In [34]:
result, updates = theano.scan(fn=lambda pre , k : pre*a , 
                    outputs_info = i,
                    non_sequences=a,
                    n_steps = k
                   )
  • result 為用 tensor 來接
  • update 為利用 theano.scan 所產生 funciton description,白話的說就是 "利用函式來產出函式"

In [35]:
print result
print updates


Subtensor{int64::}.0
OrderedUpdates()

In [36]:
final_result = result[-1]

In [37]:
power = theano.function(inputs=[a,k,i],outputs=[final_result],updates=updates)

In [38]:
print(power(range(10),2,[10]*10))


[array([   0.,   10.,   40.,   90.,  160.,  250.,  360.,  490.,  640.,  810.], dtype=float32)]

In [ ]: