2024-09-27 07:36:04 发布
网友
我想要一个for循环,它的迭代次数取决于张量值。例如:
for i in tf.range(input_placeholder[1,1]): # do something
但是,我得到以下错误:
“TypeError:”Tensor“对象不可iterable”
我该怎么办?
为此,需要使用tensorflow while循环(^{}),如下所示:
i = tf.constant(0) while_condition = lambda i: tf.less(i, input_placeholder[1, 1]) def body(i): # do something here which you want to do in your loop # increment i return [tf.add(i, 1)] # do the loop: r = tf.while_loop(while_condition, body, [i])
TensorFlow Python API函数的返回值类型,包括tf.range是^{}。Tensor是表示计算的图中节点的符号句柄。通过调用Tensor上的eval方法或将对象传递给Session的run方法来执行实际计算。在您的例子中,您可能只想遍历numpy的range。
tf.range
Tensor
eval
Session
run
numpy
range
for in in np.range(...): # do something
为此,需要使用tensorflow while循环(^{} ),如下所示:
TensorFlow Python API函数的返回值类型,包括} 。
tf.range
是^{Tensor
是表示计算的图中节点的符号句柄。通过调用Tensor
上的eval
方法或将对象传递给Session
的run
方法来执行实际计算。在您的例子中,您可能只想遍历numpy
的range
。相关问题 更多 >
编程相关推荐