-1

示例取自: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)

什么是prior_result?更准确地说,prior_result 定义在哪里?

对于以下给出的许多示例,我有同样的问题:http: //deeplearning.net/software/theano/library/scan.html

例如,

 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)

power 和 free_variables 在哪里定义?

4

1 回答 1

2

这是使用称为“lambda”的 Python 功能。lambda 是 1 行的未命名 python 函数。他们有这种形式:

lambda [param...]: code

在您的示例中,它是:

lambda prior_result, A: prior_result * A

这是一个以prior_result 和A 作为输入的函数。此函数作为 fn 参数传递给 scan() 函数。scan() 将使用 2 个变量调用它。第一个将与 output_info 参数中提供的内容相对应。另一个是 non_sequence 参数中提供的。

于 2014-06-12T14:31:15.513 回答