在Tensorflow中,我想将标量张量转换为整数。有可能吗?
我需要创建一个循环,循环的索引是标量张量,在循环体内,我想使用索引来访问张量数组中的条目。
例如:
idx = tf.constant(0)
c = lambda i : tf.less(i, 10)
def body(idx) :
i = # convert idx to int
b = weights[i] # access an entry in a tensor array, tensor cannot be used directly
....
return idx+1
tf.while_loop(c, body, [idx])
最佳答案
您需要评估张量以获取值。
如果要这样做:
i = # convert idx to int
您可以执行sess.run()或idx.eval():
i = sess.run(idx)
关于tensorflow - Tensorflow:如何在python中将标量张量转换为标量变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37049411/