TensorFlow筆記-03-張量,計算圖,會話
阿新 • • 發佈:2018-09-08
result 計算過程 運行 代碼 font fontsize ESS category .html
TensorFlow筆記-03-張量,計算圖,會話
- 搭建你的第一個神經網絡,總結搭建八股
- 基於TensorFlow的NN:用張量表示數據,用計算圖搭建神經網絡,用會話執行計算圖,優化線上的權重(參數),得到模型
- 張量(tensor):多維數組(列表)
- 階:張量的維數
·· 維 數 ···· 階 ········· 名 字 ········· 例 子 ············
·· 0-D ······ 0 ····· 標量 scalar ···· s=1 2 3
·· 1-D ······ 0 ····· 向量 vector ···· s=[1,2,3]
·· 2-D ······ 0 ····· 矩陣 matrix ···· s=[ [1,2,3], [4,5,6],[7,8,9] ]·· n-D ······ 0 ····· 標量 tensor ···· s=[[[[[....n個
- 張量可以表示0階到n階的數組(列表)
- 代碼tf02文件:https://xpwi.github.io/py/TensorFlow/tf02.py
# 兩個張量的加法
import tensorflow as tf
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
result = a+b
print(result)
運行結果:
- 結果分析:
- 計算圖(Graph):搭建神經網絡的計算過程,只搭建,不運算
- 代碼tf03文件:https://xpwi.github.io/py/TensorFlow/tf03.py
# 兩個張量的加法
import tensorflow as tf
# x 是一個一行兩列的張量
x = tf.constant([[1.0, 2.0]])
# x 是一個兩行一列的張量
w = tf.constant([[3.0], [4.0]])
'''
構建計算圖,但不運算
y = XW
= x1*w1 + x2*w2
'''
# 矩陣相乘
y = tf.matmul(x, w)
print(y)
運行結果
Tensor("MatMul:0", shape=(1, 1), dtype=float32)
- 會話(Session):執行計算圖中的結點運算
- 代碼04文件:https://xpwi.github.io/py/TensorFlow/tf04.py
# 兩個張量的加法
import tensorflow as tf
# x 是一個一行兩列的張量
x = tf.constant([[1.0, 2.0]])
# x 是一個兩行一列的張量
w = tf.constant([[3.0], [4.0]])
'''
構建計算圖,但不運算
y = XW
= x1*w1 + x2*w2
'''
# 矩陣相乘
y = tf.matmul(x, w)
print(y)
# 會話:執行節點運算
with tf.Session() as sess:
print(sess.run(y))
運行結果
y = 1.03.0 + 2.04.0 = 11
更多文章:Tensorflow 筆記
- 本筆記不允許任何個人和組織轉載
TensorFlow筆記-03-張量,計算圖,會話