tensorflow 入门实例(二)
生活随笔
收集整理的這篇文章主要介紹了
tensorflow 入门实例(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import tensorflow as tf# 創建一個常量 op, 產生一個 1x2 矩陣. 這個 op 被作為一個節點
# 加到默認圖中.
#
# 構造器的返回值代表該常量 op 的返回值.
matrix1 = tf.constant([[3., 3.]])# 創建另外一個常量 op, 產生一個 2x1 矩陣.
matrix2 = tf.constant([[2.],[2.]])# 創建一個矩陣乘法 matmul op , 把 'matrix1' 和 'matrix2' 作為輸入.
# 返回值 'product' 代表矩陣乘法的結果.
product = tf.matmul(matrix1, matrix2)# 啟動默認圖.
sess = tf.Session()# 調用 sess 的 'run()' 方法來執行矩陣乘法 op, 傳入 'product' 作為該方法的參數.
# 上面提到, 'product' 代表了矩陣乘法 op 的輸出, 傳入它是向方法表明, 我們希望取回
# 矩陣乘法 op 的輸出.
#
# 整個執行過程是自動化的, 會話負責傳遞 op 所需的全部輸入. op 通常是并發執行的.
#
# 函數調用 'run(product)' 觸發了圖中三個 op (兩個常量 op 和一個矩陣乘法 op) 的執行.
#
# 返回值 'result' 是一個 numpy `ndarray` 對象.
result = sess.run(product)
print (result)
# ==> [[ 12.]]# 任務完成, 關閉會話.
sess.close()
Session 對象在使用完后需要關閉以釋放資源. 除了顯式調用 close 外, 也可以使用 “with” 代碼塊 來自動完成關閉動作.
import tensorflow as tf matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrix1, matrix2) with tf.Session() as sess:result = sess.run([product])print (result)總結
以上是生活随笔為你收集整理的tensorflow 入门实例(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow 制定 CPU 或G
- 下一篇: tensorflow fetch 取回某