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

前端 未结 21 1861
爱一瞬间的悲伤
爱一瞬间的悲伤 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 08:11

    You should think of TensorFlow Core programs as consisting of two discrete sections:

    • Building the computational graph.
    • Running the computational graph.

    So for the code below you just Build the computational graph.

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

    You need also To initialize all the variables in a TensorFlow program , you must explicitly call a special operation as follows:

    init = tf.global_variables_initializer()
    

    Now you build the graph and initialized all variables ,next step is to evaluate the nodes, you must run the computational graph within a session. A session encapsulates the control and state of the TensorFlow runtime.

    The following code creates a Session object and then invokes its run method to run enough of the computational graph to evaluate product :

    sess = tf.Session()
    // run variables initializer
    sess.run(init)
    
    print(sess.run([product]))
    

提交回复
热议问题