如何通过索引获取张量流TF?

时间:2020-08-19 05:07:59

标签: numpy tensorflow tensorflow2.0

object_for_each_prior = tf.constant([1 for i in range(8732)])
-><tf.Tensor: shape=(8732,), dtype=int32, numpy=array([1, 1, 1, ..., 1, 1, 1], dtype=int32)>

然后,如果我想获得职位1148,1149

prior_for_each_object = tf.constant([1148,1149])
object_for_each_prior[prior_for_each_object]

然后我遇到以下错误

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got <tf.Tensor: shape=(2,), dtype=int32, numpy=array([1148, 1149], dtype=int32)>

如果我想通过索引获取张量数,应该如何处理?

1 个答案:

答案 0 :(得分:0)

使用var correct = 0; var totalQuestions = 3; var finalScore = 0 function question() { var answer1 = prompt('Question 1: what is 1 + 1?') if (+answer1 === 2) { correct += 1 }; var answer2 = prompt('Question 2: what is 2 + 3?') if (+answer2 === 5) { correct += 1 }; var answer3 = prompt('Question 3: what is 3 + 3?') if (+answer3 === 6) { correct += 1 }; finalScore = correct / totalQuestions console.log(`your final score: ${finalScore}`); } question()函数索引张量。 例如:

tf.gather_nd

请参阅this文档以进一步了解>>> object_for_each_prior = tf.constant([1 for i in range(8732)]) >>> prior_for_each_object = tf.gather_nd(object_for_each_prior, indices=[[1148], [1149]]) >>> prior_for_each_object <tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 1])> >>> prior_for_each_object.numpy() array([1, 1])