如何在 Tensorflow 中仅初始化优化器变量? [英] How to initialise only optimizer variables in Tensorflow?

查看:30
本文介绍了如何在 Tensorflow 中仅初始化优化器变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆