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

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

    I didn't find it easy to understand what is required even after reading all the answers until I executed this. TensofFlow is new to me too.

    def printtest():
    x = tf.constant([1.0, 3.0])
    x = tf.Print(x,[x],message="Test")
    init = (tf.global_variables_initializer(), tf.local_variables_initializer())
    b = tf.add(x, x)
    with tf.Session() as sess:
        sess.run(init)
        print(sess.run(b))
        sess.close()
    

    But still you may need the value returned by executing the session.

    def printtest():
        x = tf.constant([100.0])
        x = tf.Print(x,[x],message="Test")
        init = (tf.global_variables_initializer(), tf.local_variables_initializer())
        b = tf.add(x, x)
        with tf.Session() as sess:
            sess.run(init)
            c = sess.run(b)
            print(c)
            sess.close()
    

提交回复
热议问题