上面一篇文章我们主要介绍了MatMul这个ops的正向计算的实现,我们知道模型的最后一步是计算优化模型的参数,而一般采用的方法是梯度下降法,所以每个ops节点不仅要实现一个正向的计算节点,还要实现一个反向的梯度计算节点。
关于反向计算的结点官网有如下一段介绍:
Implement the gradient in Python
Given a graph of ops, TensorFlow uses automatic differentiation (backpropagation) to add new ops representing gradients with respect to the existing ops (seeGradient Computation). To make automatic differentiation work for new ops, you must register a gradient function which computes gradients with respect to the ops' inputs given gradients with respect to the ops' outputs.Mathematically, if an op computes y = f(x) the registered gradient op converts gradients ∂L/∂y