126、TensorFlow Session的執行


# tf.Session.run 方法是一個執行tf.Operation或者計算tf.Tensor的一個主要的機制
# 你可以傳遞一個或者多個tf.Operation或者tf.Tensor對象來給tf.Session.run
# TensorFlow會執行operation操作來計算結果
# tf.Session.run需要你來指定一系列的獲取,這些決定了返回值
# 這些獲取可以是 tf.Operation ,一個tf.Tensor 或者一個tensor-like type 列如tf.Variable
# 這些獲取決定了子的計算圖必須執行的操作來產生結果
import tensorflow as tf
x = tf.constant([[37.0, -23.0], [1.0, 4.0]])
w = tf.Variable(tf.random_uniform([2, 2]))
y = tf.matmul(x, w)
output = tf.nn.softmax(y)
init_op = w.initializer
with tf.Session() as sess:
    # Run the initializer on 'w'
    sess.run(init_op)
    
    # Evaluate 'output' , 'sess.run(output)' will return a NumPy array containing
    # the result of the computation
    print(sess.run(output))
    
    # Evaluate 'y' and 'output' 
    # y will only be computed once,
    # and its result used both to return 
    # y_valu and as an input to the tf.nn.softmax()
    # op .  both y_val and output_val will be NumPy arrays
    y_val, output_val = sess.run([y, output])
    
    # print result
    print(y_val)
    print(output_val)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM