AttributeError: ‘tensorflow.python.framework.ops.EagerTensor‘ object has no attribute ‘_in_graph_mod

博客内容讲述了在使用TensorFlow进行深度学习时,遇到的问题是尝试将梯度应用于张量而不是变量对象,而TensorFlow的优化器是设计为对变量对象应用梯度的。由于张量是不可变的,导致优化器无法更新。解决方案是将张量转换为变量,并在梯度计算和应用之外进行优化。此外,建议在梯度带外计算梯度以避免内存占用过高,除非需要高阶导数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The reason for the bug is that the tf.keras optimizers apply gradients to variable objects (of type tf.Variable), while you are trying to apply gradients to tensors (of type tf.Tensor). Tensor objects are not mutable in TensorFlow, thus the optimizer cannot apply gradients to it.

img = tf.Variable(img)
opt = tf.optimizers.Adam(learning_rate=lr, decay = 1e-6)

for _ in range(epoch):
    with tf.GradientTape() as tape:
        tape.watch(img)
        y = model(img.value())[:, :, :, filter]
        loss = -tf.math.reduce_mean(y)

    grads = tape.gradient(loss, img)
    opt.apply_gradients(zip([grads], [img]))

Also, it is recommended to calculate the gradients outside the tape’s context. This is because keeping it in will lead to the tape tracking the gradient calculation itself, leading to higher memory usage. This is only desirable if you want to calculate higher-order gradients. Since you don’t need those, I have kept them outside.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值