How to print the value of a Tensor object in TensorFlow?

前端 未结 21 1592
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 07:44

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

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
produ         


        
21条回答
  •  鱼传尺愫
    2020-11-22 07:52

    I think you need to get some fundamentals right. With the examples above you have created tensors (multi dimensional array). But for tensor flow to really work you have to initiate a "session" and run your "operation" in the session. Notice the word "session" and "operation". You need to know 4 things to work with tensorflow:

    1. tensors
    2. Operations
    3. Sessions
    4. Graphs

    Now from what you wrote out you have given the tensor, and the operation but you have no session running nor a graph. Tensor (edges of the graph) flow through graphs and are manipulated by operations (nodes of the graph). There is default graph but you can initiate yours in a session.

    When you say print , you only access the shape of the variable or constant you defined.

    So you can see what you are missing :

     with tf.Session() as sess:     
               print(sess.run(product))
               print (product.eval())
    

    Hope it helps!

提交回复
热议问题