Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带有TensorFlow while\u循环的python列表_Python_Tensorflow - Fatal编程技术网

使用带有TensorFlow while\u循环的python列表

使用带有TensorFlow while\u循环的python列表,python,tensorflow,Python,Tensorflow,在使用control\u flow\u ops.while\u loop时,有没有方法遍历python列表 我正在使用control\u flow\u ops.while\u loop在我的RNN中迭代一个序列。 我还有一个要调用的函数列表 让'j'表示while_循环中序列的当前索引。 迭代时,我想调用L中索引j处的函数 但是调用L[j]不起作用:j是表示序列索引的张量,L是python列表tf.gather也不起作用,因为我也不能将L转换为张量(元素是函数) 如何访问while\u循环中的L

在使用control\u flow\u ops.while\u loop时,有没有方法遍历python列表

我正在使用control\u flow\u ops.while\u loop在我的RNN中迭代一个序列。 我还有一个要调用的函数列表

让'j'表示while_循环中序列的当前索引。 迭代时,我想调用L中索引j处的函数

但是调用L[j]不起作用:j是表示序列索引的张量,L是python列表tf.gather也不起作用,因为我也不能将L转换为张量(元素是函数)

如何访问while\u循环中的L元素?示例如下:

L = [fn1,fn2,fn3]

def body(j,seq):
    seq_elem = seq.read(j)

    # Try to get the function stored in L.
    fn = L[j] # Doesn't work, L is a python list while j is a tensor.

    fn(seq_elem)
    return j+1,seq

_, _ = control_flow_ops.while_loop(
        cond = lambda j, _: j < seq_length,
        body=body,
        loop_vars=(tf.constant(0,dtype=tf.int32), seq)
L=[fn1,fn2,fn3]
def主体(j,序号):
序号=序号读数(j)
#尝试获取存储在L中的函数。
fn=L[j]#不起作用,L是python列表,而j是张量。
fn(如下要素)
返回j+1,序号
_,uu=控制u流量u运行时u循环(
cond=λj,λ:j<序列长度,
身体,
循环变量=(tf.constant(0,dtype=tf.int32),seq)

来自@soloice在该链接上的回答:

“只需运行tf.gather(list,tf\u look\u up[index]),就可以得到所需的内容。”


这有帮助吗?

无法转换为张量的列表将不适用于tf.while\u loop(截至2017年7月)。

不幸的是,列表L无法转换为张量,因此无法传递给tf.gather。