• tf tensor 输出


    在学习TensorFlow的过程中,我们需要知道某个tensor的值是什么,这个很重要,尤其是在debug的时候。也许你会说,这个很容易啊,直接print就可以了。其实不然,print只能打印输出shape的信息,而要打印输出tensor的值,需要借助class tf.Session, class tf.InteractiveSession。因为我们在建立graph的时候,只建立tensor的结构形状信息,并没有执行数据的操作。

    一 class tf.Session 

    运行tensorflow操作的类,其对象封装了执行操作对象和评估tensor数值的环境。这个我们之前介绍过,在定义好所有的数据结构和操作后,其最后运行。

    [python] view plain copy
     
    1. import tensorflow as tf  
    2.   
    3. # Build a graph.  
    4. a = tf.constant(5.0)  
    5. b = tf.constant(6.0)  
    6. c = a * b  
    7. # Launch the graph in a session.  
    8. sess = tf.Session()  
    9. # Evaluate the tensor `c`.  
    10. print(sess.run(c))  
    
    
    

    二 class tf.InteractiveSession

    顾名思义,用于交互上下文的session,便于输出tensor的数值。与上一个Session相比,其有默认的session执行相关操作,比如:Tensor.eval(), Operation.run()。Tensor.eval()是执行这个tensor之前的所有操作,Operation.run()也同理。

    [python] view plain copy
     
    1. import tensorflow as tf  
    2. a = tf.constant(5.0)  
    3. b = tf.constant(6.0)  
    4. c = a * b  
    5. with tf.Session():  
    6.   # We can also use 'c.eval()' here.  
    7.   print(c.eval())  
  • 相关阅读:
    hdu5360 Hiking(水题)
    hdu5348 MZL's endless loop(欧拉回路)
    hdu5351 MZL's Border(规律题,java)
    hdu5347 MZL's chemistry(打表)
    hdu5344 MZL's xor(水题)
    hdu5338 ZZX and Permutations(贪心、线段树)
    hdu 5325 Crazy Bobo (树形dp)
    hdu5323 Solve this interesting problem(爆搜)
    hdu5322 Hope(dp)
    Lightoj1009 Back to Underworld(带权并查集)
  • 原文地址:https://www.cnblogs.com/Alex0111/p/8493590.html
Copyright © 2020-2023  润新知