簡體   English   中英

張量流中的反向傳播深度神經網絡

[英]about backpropagation deep neural network in tensorflow

我正在閱讀有關反向傳播深度神經網絡的信息,據我了解,我可以將這種神經網絡的算法總結如下:

1-輸入x :設置輸入層的相應激活

2-前饋 :計算正向傳播的誤差

3-輸出誤差:計算輸出誤差

4-反向傳播錯誤 :計算反向傳播的錯誤

5-輸出:使用成本函數的梯度

沒關系,然后在帶有解釋的示例代碼下面檢查了許多此類深層網絡代碼:

### imports
import tensorflow as tf

### constant data
x  = [[0.,0.],[1.,1.],[1.,0.],[0.,1.]]
y_ = [[0.],[0.],[1.],[1.]]

### induction
# 1x2 input -> 2x3 hidden sigmoid -> 3x1 sigmoid output

# Layer 0 = the x2 inputs
x0 = tf.constant( x  , dtype=tf.float32 )
y0 = tf.constant( y_ , dtype=tf.float32 )

# Layer 1 = the 2x3 hidden sigmoid
m1 = tf.Variable( tf.random_uniform( [2,3] , minval=0.1 , maxval=0.9 , dtype=tf.float32  ))
b1 = tf.Variable( tf.random_uniform( [3]   , minval=0.1 , maxval=0.9 , dtype=tf.float32  ))
h1 = tf.sigmoid( tf.matmul( x0,m1 ) + b1 )

# Layer 2 = the 3x1 sigmoid output
m2 = tf.Variable( tf.random_uniform( [3,1] , minval=0.1 , maxval=0.9 , dtype=tf.float32  ))
b2 = tf.Variable( tf.random_uniform( [1]   , minval=0.1 , maxval=0.9 , dtype=tf.float32  ))
y_out = tf.sigmoid( tf.matmul( h1,m2 ) + b2 )


### loss
# loss : sum of the squares of y0 - y_out
loss = tf.reduce_sum( tf.square( y0 - y_out ) )

# training step : gradient decent (1.0) to minimize loss
train = tf.train.GradientDescentOptimizer(1.0).minimize(loss)


### training
# run 500 times using all the X and Y
# print out the loss and any other interesting info
with tf.Session() as sess:
  sess.run( tf.global_variables_initializer() )
  for step in range(500) :
    sess.run(train)

  results = sess.run([m1,b1,m2,b2,y_out,loss])
  labels  = "m1,b1,m2,b2,y_out,loss".split(",")
  for label,result in zip(*(labels,results)) :
    print ""
    print label
    print result

print ""

我的問題是,上面的代碼正在計算正向傳播的誤差,但是我看不到任何計算反向傳播誤差的步驟。 換句話說,按照上面的描述,我可以看到步驟1( 輸入x ),步驟2( 前饋 ),步驟3( 輸出錯誤 )和步驟5( 輸出 ),但是步驟號4( 表示錯誤 )在代碼中! 這是正確的還是代碼中缺少的東西? 我在網上找到的所有代碼都在反向傳播深度神經網絡中遵循相同的步驟! 請,您能描述一下錯誤傳播代碼的步驟是如何發生的,或者我應該在執行該步驟時添加些什么?

謝謝

簡而言之,當您構建TF圖直至計算代碼中的損失時,TF就會知道損失取決於哪個tf.Variable (權重)。 然后,當您創建節點train = tf.train.GradientDescentOptimizer(1.0).minimize(loss) ,然后在tf.Session運行它時,將在后台為您進行反向傳播。 更具體地說, train = tf.train.GradientDescentOptimizer(1.0).minimize(loss)合並了以下步驟:

# 1. Create a GD optimizer with a learning rate of 1.0
optimizer = tf.train.GradientDescentOptimizer(1.0)
# 2. Compute the gradients for each of the variables (weights) with respect to the loss
gradients, variables = zip(*optimizer.compute_gradients(loss))
# 3. Update the variables (weights) based on the computed gradients
train = optimizer.apply_gradients(zip(gradients, variables))

特別地,步驟12總結了反向傳播步驟。 希望這會使您更清楚!


此外,我想重組您的問題中的步驟:

  1. 輸入X:神經網絡的輸入。
  2. 正向傳遞:通過神經網絡傳播輸入,以獲取輸出。 換句話說,將輸入X與代碼中的每個tf.Variable
  3. 損耗 :步驟2中獲得的輸出與預期輸出之間的不匹配。
  4. 計算梯度:計算每個tf.Variable (權重)相對於損耗的梯度。
  5. 更新權重 :根據其相應的梯度更新每個tf.Variable (權重)。

請注意,步驟4和5封裝了反向傳播。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM