在优化器期间保持变量不变

编程入门 行业动态 更新时间:2024-10-13 02:19:12
本文介绍了在优化器期间保持变量不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个损失张量 L 的 TensorFlow 计算图,它取决于 2 个 tf.Variables,A 和 B.

I have a TensorFlow computational graph for a loss tensor L that depends on 2 tf.Variables, A and B.

我想在保持 B 固定的同时在变量 A 上运行梯度上升(A+=L 与 A 的梯度),反之亦然 - 在保持 A 固定的同时在 B 上运行梯度上升(B+=L 与 B 的梯度).我该怎么做?

I'd like to run gradient ascent on variable A (A+=gradient of L wrt A) while holding B fixed, and vice versa - running gradient ascent on B (B+=gradient of L wrt B) while holding A fixed. How do I do this?

推荐答案

tf.stop_gradient(tensor) 可能就是你要找的.出于梯度计算的目的,张量将被视为常数.您可以创建两个损失,将不同的部分视为常量.

tf.stop_gradient(tensor) might be what you are looking for. The tensor will be treated as constant for gradient computation purposes. You can create two losses with different parts treated as constants.

另一种选择(通常更好)是创建 2 个优化器,但仅显式优化变量的子集,例如

The other option (and often better) would be to create 2 optimizers but explicitly optimize only subsets of variables, e.g.

train_a = tf.train.GradientDescentOptimizer(0.1).minimize(loss_a, var_list=[A])
train_b = tf.train.GradientDescentOptimizer(0.1).minimize(loss_b, var_list=[B])

并且您可以在它们之间迭代更新.

and you can iterate between them on the updates.

这篇关于在优化器期间保持变量不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 05:31:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!