tf.scan


tf.scan(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, swap_memory=False, infer_shape=True, name=None)

fn:计算函数
elems:以elems的第一维度的变量list作函数计算直到遍历完整个elems
initializer:fn计算的初始值,替代elems做第一次计算

举个好理解的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
x = [1,2,3]
z = 10

x = tf.convert_to_tensor(x)
z = tf.convert_to_tensor(z)

def f(x,y):
return x+y

g = tf.scan(fn=f,elems = x,initializer=z)

sess = tf.Session()
sess.run(tf.global_variables_initializer)

sess.run(g)

会得到:

1
2
In [97]: sess.run(g)
Out[97]: array([11, 13, 16], dtype=int32)

详细的计算逻辑如下:
11 = 10(初始值initializer)+ 1(x[0])
13 = 11(上次的计算结果)+2(x[1])
16 = 13(上次的计算结果)+3(x[2])

打赏的大佬可以联系我,赠送超赞的算法资料