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

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

    tf.keras.backend.eval is useful for evaluating small expressions.

    tf.keras.backend.eval(op)
    

    TF 1.x and TF 2.0 compatible.


    Minimal Verifiable Example

    from tensorflow.keras.backend import eval
    
    m1 = tf.constant([[3., 3.]])
    m2 = tf.constant([[2.],[2.]])
    
    eval(tf.matmul(m1, m2))
    # array([[12.]], dtype=float32)
    

    This is useful because you do not have to explicitly create a Session or InteractiveSession.

提交回复
热议问题