简体   繁体   中英

Theano scan function

Example taken from: http://deeplearning.net/software/theano/library/scan.html

k = T.iscalar("k")
A = T.vector("A")

 # Symbolic description of the result
 result, updates = theano.scan(fn=lambda prior_result, A: prior_result * A,
                          outputs_info=T.ones_like(A),
                          non_sequences=A,
                          n_steps=k)

 # We only care about A**k, but scan has provided us with A**1 through A**k.
 # Discard the values that we don't care about. Scan is smart enough to
 # notice this and not waste memory saving them.
 final_result = result[-1]

 # compiled function that returns A**k
 power = theano.function(inputs=[A,k], outputs=final_result, updates=updates)

 print power(range(10),2)
 print power(range(10),4)

What is prior_result? More accurately, where is prior_result defined?

I have this same question for lot of the examples given on: http://deeplearning.net/software/theano/library/scan.html

For example,

 components, updates = theano.scan(fn=lambda coefficient, power, free_variable: coefficient * (free_variable ** power),
                              outputs_info=None,
                              sequences=[coefficients, theano.tensor.arange(max_coefficients_supported)],
                              non_sequences=x)

Where is power and free_variables defined?

This is using a Python feature call "lambda". lambda are unnamed python function of 1 line. They have this forme:

lambda [param...]: code

In your example it is:

lambda prior_result, A: prior_result * A

This is a function that take prior_result and A as input. This function, is passed to the scan() function as the fn parameter. scan() will call it with 2 variables. The first one will be the correspondance of what was provided in the output_info parameter. The other is what is provided in the non_sequence parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM