代码报错:ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a

自定义Keras层后,代码报错,提示如标题,无法计算gradient。

原因:我是在别人模型的基础上加上自己的代码,修改了代码之后,原来作者使用add_weight初始化的变量矩阵没有删除。导致错误。

如图,没有用到的变量需要及时删除,否则在计算gradient时会出错

代码报错:ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a