AttributeError: 'Adam' object has no attribute 'compute_gradients'求解答

AttributeError: 'Adam' object has no attribute 'compute_gradients'
在修改代码的过程中报错,本人使用的是tensorflow2.2,keras2.3.1,源代码为tensorflow1.13 实在是不知道如何修改,求解答

以下是部分代码,后期需要全部代码私我

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.python.keras.optimizers import adam_v2



 #----------------------优化器优化---------------------------(如果后期损失值过大,考虑梯度是否过大)
        #采用梯度截断技术
        #tf.variable_scope用于定义创建变量(层)的操作的上下文管理器。
        with tf.variable_scope('optimizer'):
            opt=tf.keras.optimizers.Adam(self.lr)
            #opt=tf.train.AdamOptimizer(self.lr)为原版的修改
            grad_vars=opt.compute_gradients(self.cost) #计算出所有参数的导数
            clip_grad_vars=[[tf.clip_by_value(g,-5,5),v] for g,v in grad_vars]#按照值进行截断,g、v通过限定值进行截断,clip_grad_vars为截断之后的梯度
            self.train_op=opt.apply_gradients(clip_grad_vars,self.global_step)
            #使用截断后的梯度,进行参数的更新 即clip_grad_vars每次更新,后续计数加1
        self.saver=tf.Train.Saver(tf.global_variables(),max_to_keep=5)#模型保存器   max_to_keep=5只保留5次最新的模型,如果硬盘足够大,可以大一些

曾用这个方法进行修改:
gradients = optimizer.get_gradients(objective, var_list)

但抛出一个值错误:
ValueError: Variable has None for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.


with tf.variable_scope('optimizer'):
            #opt=adam_v2.Adam(self.lr)
            opt=Adam(self.lr)
            with tf.GradientTape() as tape:
                grads=tape.gradient(self.cost,tf.trainable_variables())
            clip_grad_vars = [[tf.clip_by_value(g, -5, 5), v] for g, v in zip(grads, dict)]
            # 使用截断后的梯度,进行参数的更新,即clip_grad_vars每次更新,后续计数加1
            self.train_op = opt.apply_gradients(clip_grad_vars, self.global_step)
        self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)