求梯度

08 Jun 2020

tf.gradients

如果要求梯度,关键是要显示给出y=f(x)的形式,否则就会报错

AttributeError: ‘NoneType’ object has no attribute ‘op’

所以如果正演算子中有导数,必须显示地给出输入和输出地计算过程,不能用程序定义地Layer;

如果有多阶导数,那么x–>y至少有两个计算算子(如权重与输入相乘和激活函数两个算子要显示给出)

同时,必须关闭动态运行

tf.compat.v1.disable_eager_execution()

如果函数形式被封装在class类中,然后再调用类求梯度的化,一阶导数也求不出。

gradients()把operation加入graph中(所以不能即刻执行),返回一个长度为len(xs)的Tensor列表,列表中每个tensor是 sum(dy/dx) for y in ys and for x in xs.

The approach we take here is as follows: Create a list of all ops in the subgraph between the ys and xs. Visit these ops in reverse order of ids to ensure that when we visit an op the gradients w.r.t its outputs have been collected. Then aggregate these gradients if needed, call the op’s gradient function, and add the generated gradients to the gradients for its input.

tf.GradientTape

两种求导方式是否相同

参考:https://stackoverflow.com/questions/55380219/is-tf-gradienttape-in-tf-2-0-equivalent-to-tf-gradients

tf.custom_graident

自定义求导

参考:https://www.tensorflow.org/guide/autodiff