tf.scan(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, swap_memory=False, infer_shape=True, name=None)
具體參數(shù)定義可以參見官網(wǎng),這里撿最常見的形式來說,常用函數(shù)形式為tf.scan(fn, elems, initializer=None)
函數(shù)說明:scan on the list of tensors unpacked from elems on dimension 0.
f(n)以elems的第一維度的變量list作函數(shù)計算直到遍歷完整個elems
關(guān)于 initializer的說明為:
If no initializer is provided, the output structure and dtypes of fn are assumed to be the same as its input; and in this case, the first argument of fn must match the structure of elems.
If an initializer is provided, then the output of fn must have the same structure as initializer; and the first argument of fn must match this structure.
也就是說當(dāng)initializer給定的時候,fn的輸出結(jié)構(gòu)必須和initializer保持一致,且fn的第一個參變量也必須和該結(jié)構(gòu)一致。而如果該參數(shù)沒有給定的時候初始化默認和x[0]的維度保持一致。
設(shè)函數(shù)為f,
x = [u(0),u(1),...,u(n)]
y = tf.scan(f,x,initializer=v(0))
此時f的參數(shù)類型必須是(v(0),x),f的輸出必須和v(0)保持一致,整個計算過程如下:
v(1)=f(v(0),u(0))
v(2)=f(v(1),u(1))
....
v(n+1)=f(v(n),u(n))
y=v(n+1)