在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])
Run Code Online (Sandbox Code Playgroud)
Eri*_*lan 12
在 Tensorflow 2.0+ 中,它很简单:
my_tensor.numpy()
Run Code Online (Sandbox Code Playgroud)
gro*_*fte 11
它应该像调用int()你的张量一样简单。
int(tf.random.uniform((), minval=0, maxval=32, dtype=tf.dtypes.int32))
>> 4
Run Code Online (Sandbox Code Playgroud)
我已经在 TF 2.2 和 2.4 中检查过这一点
Sun*_*Kim -7
您需要评估张量才能获得值。
如果你想这样做:
i = # convert idx to int
Run Code Online (Sandbox Code Playgroud)
你可以执行 sess.run() 或 idx.eval():
i = sess.run(idx)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23981 次 |
| 最近记录: |