【问题标题】:How to get Value and Index number of tensor?如何获取张量的值和索引数?
【发布时间】:2021-09-29 05:03:33
【问题描述】:
preds = model([img_feat, ques_feat])
sorted_a = tf.sort(preds, direction='DESCENDING')
print(sorted_a[0][1])

它将打印tf.Tensor(0.35625213, shape=(), dtype=float32)

这里我只需要数字 0.35625213 及其索引。

【问题讨论】:

  • 添加更多细节和可重现的代码。

标签: tensorflow tensor


【解决方案1】:

要打印值,可以将张量转换为numpy,然后打印:

import tensorflow as tf

# defining a float tensor
a = tf.constant(2.34)

# print tensor
print(a)              # output: tf.Tensor(2.34, shape=(), dtype=float32)

# convert to numpy and then print
print(a.numpy())      # output: 2.34

【讨论】:

猜你喜欢
  • 2022-10-23
  • 1970-01-01
  • 2021-11-25
  • 2019-12-28
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
相关资源
最近更新 更多