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

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

    Try this simple code! (it is self explanatory)

    import tensorflow as tf
    sess = tf.InteractiveSession() # see the answers above :)
    x = [[1.,2.,1.],[1.,1.,1.]]    # a 2D matrix as input to softmax
    y = tf.nn.softmax(x)           # this is the softmax function
                                   # you can have anything you like here
    u = y.eval()
    print(u)
    
    0 讨论(0)
  • 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]))
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-11-22 08:14

    No, you can not see the content of the tensor without running the graph (doing session.run()). The only things you can see are:

    • the dimensionality of the tensor (but I assume it is not hard to calculate it for the list of the operations that TF has)
    • type of the operation that will be used to generate the tensor (transpose_1:0, random_uniform:0)
    • type of elements in the tensor (float32)

    I have not found this in documentation, but I believe that the values of the variables (and some of the constants are not calculated at the time of assignment).


    Take a look at this example:

    import tensorflow as tf
    from datetime import datetime
    dim = 7000
    

    The first example where I just initiate a constant Tensor of random numbers run approximately the same time irrespectibly of dim (0:00:00.003261)

    startTime = datetime.now()
    m1 = tf.truncated_normal([dim, dim], mean=0.0, stddev=0.02, dtype=tf.float32, seed=1)
    print datetime.now() - startTime
    

    In the second case, where the constant is actually gets evaluated and the values are assigned, the time clearly depends on dim (0:00:01.244642)

    startTime = datetime.now()
    m1 = tf.truncated_normal([dim, dim], mean=0.0, stddev=0.02, dtype=tf.float32, seed=1)
    sess = tf.Session()
    sess.run(m1)
    print datetime.now() - startTime
    

    And you can make it more clear by calculating something (d = tf.matrix_determinant(m1), keeping in mind that the time will run in O(dim^2.8))

    P.S. I found were it is explained in documentation:

    A Tensor object is a symbolic handle to the result of an operation, but does not actually hold the values of the operation's output.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-22 08:15

    Enable the eager execution which is introduced in tensorflow after version 1.10. It's very easy to use.

    # Initialize session
    import tensorflow as tf
    tf.enable_eager_execution()
    
    
    # Some tensor we want to print the value of
    a = tf.constant([1.0, 3.0])
    
    print(a)
    
    0 讨论(0)
提交回复
热议问题