Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print full (not truncated) tensor in tensorflow?

Whenever I try printing I always get truncated results

import tensorflow as tf
import numpy as np

np.set_printoptions(threshold=np.nan)

tensor = tf.constant(np.ones(999))

tensor = tf.Print(tensor, [tensor])

sess = tf.Session()

sess.run(tensor)

As you can see I've followed a guide I found on Print full value of tensor into console or write to file in tensorflow

But the output is simply

...\core\kernels\logging_ops.cc:79] [1 1 1...]

I want to see the full tensor, thanks.

like image 525
Plezos Avatar asked Mar 22 '18 13:03

Plezos


People also ask

How do I print a tensor without truncation?

To avoid truncation and to control how much of the tensor data is printed use the same API as numpy's numpy. set_printoptions(threshold=10_000) . If your tensor is very large, adjust the threshold value to a higher number. All the available set_printoptions arguments are documented here.

How do I print a TF tensor value?

[A]: To print the value of a tensor without returning it to your Python program, you can use the tf. print() operator, as Andrzej suggests in another answer. According to the official documentation: To make sure the operator runs, users need to pass the produced op to tf.

How do I print a Kerastensor?

print_tensor(y_true, message='y_true = ') y_pred = K. print_tensor(y_pred, message='y_pred = ') ... The function returns an identical tensor. When that tensor is evaluated, it will print its content, preceded by message .


2 Answers

You can do it as follows in TensorFlow 2.x:

import tensorflow as tf

tensor = tf.constant(np.ones(999))
tf.print(tensor, summarize=-1)

From TensorFlow docs -> summarize: The first and last summarize elements within each dimension are recursively printed per Tensor. If set to -1, it will print all elements of every tensor.

https://www.tensorflow.org/api_docs/python/tf/print

like image 162
Lostefra Avatar answered Oct 20 '22 14:10

Lostefra


This is solved easily by checking the Tensorflow API for tf.Print. Pass summarize=n where n is the number of elements you want displayed.

like image 19
xdurch0 Avatar answered Oct 20 '22 14:10

xdurch0