Shahid khan M
Shahid khan M

Reputation: 41

How to get Value and Index number of tensor?

preds = model([img_feat, ques_feat])
sorted_a = tf.sort(preds, direction='DESCENDING')
print(sorted_a[0][1])

It will print tf.Tensor(0.35625213, shape=(), dtype=float32).

Here I just need the number 0.35625213 and its index.

Upvotes: 0

Views: 1879

Answers (1)

mrazizi
mrazizi

Reputation: 326

To print the value, you can convert the tensor to numpy and then print it:

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

Upvotes: 1

Related Questions