Tensorflow 获取范围内的所有变量 [英] Tensorflow get all variables in scope

查看:44
本文介绍了Tensorflow 获取范围内的所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某个范围内创建了一些变量,如下所示:

I have some variables created within a certain scope like this:

with tf.variable_scope("my_scope"):
  createSomeVariables()
  ...

然后我想获取my_scope"中所有变量的列表,以便我可以将其传递给优化器.这样做的正确方法是什么?

I then want to get the list of all the variables in "my_scope" so I can pass it to an optimizer. What is the right way to do this?

推荐答案

我想你想要 tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='my_scope').这将获取范围内的所有变量.

I think you want tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='my_scope'). This will get all variables in a scope.

要传递给优化器,您不需要所有变量,您只需要可训练的变量.这些也保存在默认集合中,即 tf.GraphKeys.TRAINABLE_VARIABLES.

To pass to an optimizer you do not want all variables you would just want the trainable variables. Those are also kept in a default collection, which is tf.GraphKeys.TRAINABLE_VARIABLES.

这篇关于Tensorflow 获取范围内的所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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