-1

I struggle to apply answers to similar questions, with Tensorflow 2.6.0.

I would like to inspect the values in my tensor during debugging. If I do a Python print

predicted_ids=tf.random.categorical(predicted_logits, num_samples=1)
predicted_ids=tf.squeeze(predicted_ids, axis=-1)
print(predicted_ids)

I get

Tensor("Squeeze:0", shape=(1,), dtype=int64)

I then try to

(1)

print(tf.Print(predicted_ids, [predicted_ids], message="This is predicted_ids: "))

(2)

with tf.Session() as sess:  print(predicted_ids.eval()) 

(3)

sess = tf.InteractiveSession()
a = tf.Print(predicted_ids, [predicted_ids], message="This is predicted_ids: ")

All of which will throw errors. It seems to me this is a very common question, and there must be an elegant robust simple answer, in TF 2.6.0.

user229044
  • 232,980
  • 40
  • 330
  • 338
kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • 1
    @hafiz031 already gave you the answer. Use the `numpy` **method** (not attribute) as he showed. – ATony Oct 02 '21 at 14:30
  • error is obtained with `predicted_ids.numpy()` unfortunately – kiriloff Oct 04 '21 at 09:40
  • 1
    Something must be off either with `predicted_logits` or your installation because I tried the code above with `[0.3, 0.2, 0.1, 0.4]` in lieu of `predicted_logits` and `.numpy()` worked fine on TF 2.6.0 – ATony Oct 05 '21 at 12:33

2 Answers2

0

It is fairly simple:

For example:

tf.random.categorical(tf.math.log([[0.5, 0.5]]), 5).numpy()

Output:

array([[1, 1, 1, 0, 1]])

numpy()

In your case:

predicted_ids.numpy()

hafiz031
  • 2,236
  • 3
  • 26
  • 48
-1

I think you don't need to create a session as .eval() function is compatible with TensorFlow v1. The code which works for me fine is to use tf.print() function. Here's a quick demo:

c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)
print(tf.print(e))

Jiten Patel
  • 123
  • 9
  • this prints something ugly `name: "PrintV2" op: "PrintV2" input: "StringFormat" attr { key: "end" value { s: "\n" } } attr { key: "output_stream" value { s: "stderr" } }` – kiriloff Oct 02 '21 at 13:33