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

前端 未结 21 1999
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 08:12

    Basically, in tensorflow when you create a tensor of any sort they are created and stored inside which can be accessed only when you run a tensorflow session. Say you have created a constant tensor
    c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
    Without running a session, you can get
    - op: An Operation. Operation that computes this tensor.
    - value_index: An int. Index of the operation's endpoint that produces this tensor.
    - dtype: A DType. Type of elements stored in this tensor.

    To get the values you can run a session with the tensor you require as:

    with tf.Session() as sess:
        print(sess.run(c))
        sess.close()
    

    The output will be something like this:

    array([[1., 2., 3.], [4., 5., 6.]], dtype=float32)

提交回复
热议问题