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
Python 在while循环中读取并分配给TensorArray_Python_Tensorflow - Fatal编程技术网

Python 在while循环中读取并分配给TensorArray

Python 在while循环中读取并分配给TensorArray,python,tensorflow,Python,Tensorflow,在TensorFlow中,我有一个tf.while\u循环,它涉及使用TensorArray。我写了一个小玩具的例子来说明我遇到的问题 对于每个循环,我想读取数组中一个元素的值,将其添加到张量,然后将结果分配给数组中的另一个元素。while循环的body参数定义为以下函数: def loop_body(i, x, y): x = x.write(i, y + x.gather(indices=[i-1]))) return i, x i、x和y初始化为: i = tf.cons

在TensorFlow中,我有一个
tf.while\u循环
,它涉及使用
TensorArray
。我写了一个小玩具的例子来说明我遇到的问题

对于每个循环,我想读取数组中一个元素的值,将其添加到张量,然后将结果分配给数组中的另一个元素。while循环的
body
参数定义为以下函数:

def loop_body(i, x, y):
    x = x.write(i, y + x.gather(indices=[i-1])))
    return i, x
i
x
y
初始化为:

i = tf.constant(1, dtype=tf.int32)
x = tf.TensorArray(dtype=tf.float32, size=10)
x = x.write(0, [0, 0, 0])
y = tf.constant([1, 2, 3], dtype=tf.float32)
现在,当我运行代码并执行while循环时,出现以下错误:

ValueError: Inconsistent shapes: saw (?, 3) but expected (3,) (and infer_shape=True)

为什么
x.gather()
不给我一个(3,)的形状?我应该怎么做呢?

文档已经描述了
tf.tensorray.gather()
将以压缩的张量返回tensorray中的选定值

返回:

在由索引选择的张量数组中,压缩为一个张量

所以你会得到(?,3)的形状。您可以更改它:

x = x.write(i, y + x.gather(indices=[i-1])[0])
# or
x = x.write(i, y + x.read(i-1))
此外,代码中还有一些错误。下面我将给出一个完整的例子

import tensorflow as tf

def condition(i, x,y):
    return tf.less(i, 10)

def loop_body(i, x,y):
    x = x.write(i, y + x.gather(indices=[i - 1])[0])
    #or
    # x = x.write(i, y + x.read(i-1))
    return i+1, x, y

i = tf.constant(1)
x = tf.TensorArray(dtype=tf.float32,size=1, dynamic_size=True,clear_after_read=False)
x = x.write(0, [0., 0., 0.])
y = tf.constant([1, 2, 3], dtype=tf.float32)

i, x, y = tf.while_loop(condition, loop_body, loop_vars=[i,x,y])
x = x.stack()

with tf.Session():
    print(i.eval())
    print(x.eval())

#print
10
[[ 0.  0.  0.]
 [ 1.  2.  3.]
 [ 2.  4.  6.]
 [ 3.  6.  9.]
 [ 4.  8. 12.]
 [ 5. 10. 15.]
 [ 6. 12. 18.]
 [ 7. 14. 21.]
 [ 8. 16. 24.]
 [ 9. 18. 27.]]

该文档已经描述了
tf.tensorray.gather()
将以压缩张量的形式返回tensorray中的选定值

返回:

在由索引选择的张量数组中,压缩为一个张量

所以你会得到(?,3)的形状。您可以更改它:

x = x.write(i, y + x.gather(indices=[i-1])[0])
# or
x = x.write(i, y + x.read(i-1))
此外,代码中还有一些错误。下面我将给出一个完整的例子

import tensorflow as tf

def condition(i, x,y):
    return tf.less(i, 10)

def loop_body(i, x,y):
    x = x.write(i, y + x.gather(indices=[i - 1])[0])
    #or
    # x = x.write(i, y + x.read(i-1))
    return i+1, x, y

i = tf.constant(1)
x = tf.TensorArray(dtype=tf.float32,size=1, dynamic_size=True,clear_after_read=False)
x = x.write(0, [0., 0., 0.])
y = tf.constant([1, 2, 3], dtype=tf.float32)

i, x, y = tf.while_loop(condition, loop_body, loop_vars=[i,x,y])
x = x.stack()

with tf.Session():
    print(i.eval())
    print(x.eval())

#print
10
[[ 0.  0.  0.]
 [ 1.  2.  3.]
 [ 2.  4.  6.]
 [ 3.  6.  9.]
 [ 4.  8. 12.]
 [ 5. 10. 15.]
 [ 6. 12. 18.]
 [ 7. 14. 21.]
 [ 8. 16. 24.]
 [ 9. 18. 27.]]