接着上篇tensorflow compute graph的理解,其中operation node 需要给运算定义forward 和backward函数。这篇中我们实现一个简单的fully_connected layer的forward 和backward 函数:

class fullyconnect(Operation):
    def __init__(self, x, w, b): 
      super().__init__([x, w, b]) 
      self.x = x 
      self.w = w 
      self.b = b 
    def forward(self, x, w, b): 
      return x.dot(w)+b
    def backward(self, upstream_grad):
      dX = upstream_grad.dot(self.w.T)
      dW = (self.x.T).dot(upstream_grad)
      db = np.sum(upstream_grad)
      return dX, [dW, db]

 

神经网络fully_connected层的forward 和backward实现

具体举一个简单的全连接网络结构来说明,为什么backward的中dX,dW,db的计算:

神经网络fully_connected层的forward 和backward实现

神经网络fully_connected层的forward 和backward实现

相关文章:

  • 2021-11-11
  • 2022-02-09
  • 2021-12-10
  • 2021-07-08
  • 2021-07-30
  • 2021-12-14
  • 2021-07-21
  • 2021-10-06
猜你喜欢
  • 2021-04-06
  • 2021-05-15
  • 2021-12-05
  • 2021-08-17
  • 2022-12-23
  • 2021-12-01
  • 2021-11-30
相关资源
相似解决方案