Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (47.6k points)

I have been using the introductory example of matrix multiplication in TensorFlow.

matrix1 = tf.constant([[3., 3.]]) 

matrix2 = tf.constant([[2.],[2.]]) 

product = tf.matmul(matrix1, matrix2)

When I print the product, it is displaying it as a Tensor object:

<tensorflow.python.framework.ops.Tensor object at 0x10470fcd0>

But how do I know the value of the product?

The following doesn't help:

print product Tensor("MatMul:0", shape=TensorShape([Dimension(1), Dimension(1)]), dtype=float32)

I know that graphs run on Sessions, but isn't there any way I can check the output of a Tensorobject without running the graph in a session?

To Learn what is machine learning and application of machine learning, visit the machine learning course by Intellipaat.

1 Answer

0 votes
by (33.1k points)

To print the actual value of a Tensor object, you have to run some code in a session. When you run some code in a session, it connects your Jupyter notebook with GPU.

Here are some methods to print a Tensor object.

  • Session.run(): When you use a default session within tf.Session() block, then it evaluates the value passed in it. This method will give you the value of the Tensor. 

  • tf.InteractiveSession(): This method lets you open a session at the start of the problem. Then use that session for all Tensor.eval() calls. You can use this method in the shell or on the IPython notebook.

  • tf.Print(): If you are using distributed TensorFlow like Google Collab, you can print its output to the standard output of the task where your program runs. On Jupyter Notebook, you will not see the output of tf.Print() in the notebook without running it in a session.

Browse Categories

...