TensorFlow学习笔记之二(使用TensorFlow实现神经网络)
文章目錄
- 全連接網絡結構的前向傳播算法
- 單個神經元
- 全連接網絡結構
- 計算過程舉例
- 代碼實現該神經網絡樣例程序
- 搭建神經網絡的過程:準備、前向傳播、反向傳播、循環迭代
- 準備
- 前向傳播:定義輸入、參數和輸出
- 反向傳播:定義損失函數、反向傳播方法
- 循環迭代:生成會話,訓練STEPS輪
- 總結
全連接網絡結構的前向傳播算法
單個神經元
從上圖可以看出,單個神經元有多個輸入和一個輸出。而神經網絡的結構是不同神經元之間的連接結構。神經元的輸出是所有輸入的加權和。神經元的參數就是輸入的權重ω。神經網絡的優化是優化參數的取值過程。
全連接網絡結構
所謂全連接,指相鄰的兩層之間任意兩個節點之間都有連接。
一個簡單的判斷零件是否合格的三層全連接網絡。該圖展示了這個神經網絡前向傳播過程。
場景描述:
該網絡通過輸入零件長度和零件質量來判斷零件是否合格
該神經網絡的輸入為:
X=[x1x2]X = \begin{gathered} \begin{bmatrix} x_{1} & x_{2} \end{bmatrix} \end{gathered} X=[x1??x2??]?
其中,x1是零件的長度,x2是零件的質量。
神經網絡的參數為:
W(1)=[w1,1(1)w1,2(1)w1,3(1)w2,1(1)w2,2(1)w2,3(1)]W^{(1)}=\begin{gathered} \begin{bmatrix} w^{(1)}_{1,1} & w^{(1)}_{1,2} & w^{(1)}_{1,3} \\ w^{(1)}_{2,1} & w^{(1)}_{2,2} & w^{(1)}_{2,3} \end{bmatrix} \end{gathered} W(1)=[w1,1(1)?w2,1(1)??w1,2(1)?w2,2(1)??w1,3(1)?w2,3(1)??]?
W(2)=[w1,1(2)w2,1(2)w2,1(2)]W^{(2)}=\begin{gathered} \begin{bmatrix} w^{(2)}_{1,1}\\ w^{(2)}_{2,1}\\ w^{(2)}_{2,1} \end{bmatrix} \end{gathered} W(2)=????w1,1(2)?w2,1(2)?w2,1(2)???????
計算過程
[a1,1a1,2a1,3]=[x1x2]?[w1,1(1)w1,2(1)w1,3(1)w2,1(1)w2,2(1)w2,3(1)]\begin{gathered} \begin{bmatrix} a_{1,1} & a_{1,2} & a_{1,3} \end{bmatrix} \end{gathered}=\begin{gathered} \begin{bmatrix} x_{1} & x_{2} \end{bmatrix} \end{gathered} *\begin{gathered} \begin{bmatrix} w^{(1)}_{1,1} & w^{(1)}_{1,2} & w^{(1)}_{1,3} \\ w^{(1)}_{2,1} & w^{(1)}_{2,2} & w^{(1)}_{2,3} \end{bmatrix} \end{gathered} [a1,1??a1,2??a1,3??]?=[x1??x2??]??[w1,1(1)?w2,1(1)??w1,2(1)?w2,2(1)??w1,3(1)?w2,3(1)??]?
[y]=[a1,1a1,2a1,3]?[w1,1(2)w2,1(2)w2,1(2)]\begin{gathered} \begin{bmatrix} y \end{bmatrix} \end{gathered}=\begin{gathered} \begin{bmatrix} a_{1,1} & a_{1,2} & a_{1,3} \end{bmatrix} \end{gathered} *\begin{gathered} \begin{bmatrix} w^{(2)}_{1,1}\\ w^{(2)}_{2,1}\\ w^{(2)}_{2,1} \end{bmatrix} \end{gathered} [y?]?=[a1,1??a1,2??a1,3??]??????w1,1(2)?w2,1(2)?w2,1(2)???????
計算過程舉例
當零件長度=0.7,零件質量=0.9,權重
W(1)=[0.20.10.40.3?0.50.2]W^{(1)}=\begin{gathered} \begin{bmatrix} 0.2 & 0.1 & 0.4 \\ 0.3 & -0.5 & 0.2 \end{bmatrix} \end{gathered} W(1)=[0.20.3?0.1?0.5?0.40.2?]?
W(2)=[0.60.1?0.2]W^{(2)}=\begin{gathered} \begin{bmatrix} 0.6 \\ 0.1 \\ -0.2 \end{bmatrix} \end{gathered} W(2)=???0.60.1?0.2?????
計算中間變量:
[a1,1a1,2a1,3]=[0.70.9]?[0.20.10.40.3?0.50.2]=[0.410.1?0.2]\begin{gathered} \begin{bmatrix} a_{1,1} & a_{1,2} & a_{1,3} \end{bmatrix} \end{gathered}=\begin{gathered} \begin{bmatrix} 0.7& 0.9 \end{bmatrix} \end{gathered} *\begin{gathered} \begin{bmatrix} 0.2 & 0.1 & 0.4 \\ 0.3 & -0.5 & 0.2 \end{bmatrix} \end{gathered}=\begin{gathered} \begin{bmatrix} 0.41 & 0.1 & -0.2 \end{bmatrix} \end{gathered} [a1,1??a1,2??a1,3??]?=[0.7?0.9?]??[0.20.3?0.1?0.5?0.40.2?]?=[0.41?0.1??0.2?]?
得出y值
[y]=[0.410.1?0.2]?[0.60.1?0.2]=[0.116]\begin{gathered} \begin{bmatrix} y \end{bmatrix} \end{gathered}= \begin{bmatrix} 0.41 & 0.1 & -0.2 \end{bmatrix}* \begin{bmatrix} 0.6 \\ 0.1 \\ -0.2 \end{bmatrix} = \begin{bmatrix} 0.116 \end{bmatrix} [y?]?=[0.41?0.1??0.2?]????0.60.1?0.2????=[0.116?]
代碼實現該神經網絡樣例程序
用一個完整的程序來訓練神經網絡來解決二分類問題
數據集:隨機數生成一個
搭建神經網絡的過程:準備、前向傳播、反向傳播、循環迭代
準備
import導入相關庫
常量的定義
數據集生成
前向傳播:定義輸入、參數和輸出
x=
y_=
w1=
w2=
a=
y=
反向傳播:定義損失函數、反向傳播方法
loss=
train_step=
循環迭代:生成會話,訓練STEPS輪
# 3、生成會話,訓練STEPS輪 with tf.Session() as sess:# 3.1、初始化參數值init_op = tf.global_variables_initializer()sess.run(init_op)# 3.2、訓練模型STEPS = 3000for i in range(STEPS):# 3.2.1 每輪確定讀取數據集的游標start = (i*BATCH_SIZE) % 32end = start + BATCH_SIZE# 3.2.2 喂入數據,開始訓練sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})總結
整個神經網絡總共分為四個部分:
總結
以上是生活随笔為你收集整理的TensorFlow学习笔记之二(使用TensorFlow实现神经网络)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow学习笔记之一(Ten
- 下一篇: TensorFlow学习笔记之三(神经网