如何在 Tensorflow 中仅初始化优化器变量?

编程入门 行业动态 更新时间:2024-10-11 03:21:05
本文介绍了如何在 Tensorflow 中仅初始化优化器变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想在 Tensorflow 中使用 MomentumOptimizer.然而,由于这个优化器使用了一些内部变量,尝试在不初始化这个变量的情况下使用它会产生一个错误:

I want to use MomentumOptimizer in Tensorflow. However, since this optimizer uses some internal variable, attempting to use it without initializing this variable yields an error:

FailedPreconditionError(回溯见上文):尝试使用未初始化的值 Variable_2/Momentum

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_2/Momentum

这可以通过初始化所有变量来轻松解决,例如使用

This can be easily solved by initializing all variables, using for example

tf.global_variables_initializer().run()

但是,我不想初始化所有变量 - 只初始化优化器的变量.有没有办法做到这一点?

However, I do not want to initialize all the variables - only those of optimizer. Is there any way to do this?

推荐答案

还有一个更直接的方法:

There is a more straightforward way:

optimizer = tf.train.AdamOptimizer()
session.run(tf.variables_initializer(optimizer.variables()))

这篇关于如何在 Tensorflow 中仅初始化优化器变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

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

发布评论

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

>www.elefans.com

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