在 tensorflow 中检索未命名的变量 [英] Retrieving an unnamed variable in tensorflow

查看:26
本文介绍了在 tensorflow 中检索未命名的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了一个模型并将其保存在检查点中,但刚刚意识到我忘记命名我在恢复模型时想要检查的变量之一.

I've trained up a model and saved it in a checkpoint, but only just realized that I forgot to name one of the variables I'd like to inspect when I restore the model.

我知道如何从 tensorflow 中检索命名变量(g = tf.get_default_graph() 然后是 g.get_tensor_by_name([name])).在这种情况下,我知道它的范围,但它是未命名的.我试过查看 tf.GraphKeys.GLOBAL_VARIABLES,但由于某种原因它没有出现在那里.

I know how to retrieve named variables from tensorflow, (g = tf.get_default_graph() and then g.get_tensor_by_name([name])). In this case, I know its scope, but it is unnamed. I've tried looking in tf.GraphKeys.GLOBAL_VARIABLES, but it doesn't appear there, for some reason.

以下是它在模型中的定义:

Here's how it's defined in the model:

with tf.name_scope("contrastive_loss") as scope:
    l2_dist = tf.cast(tf.sqrt(1e-4 + tf.reduce_sum(tf.subtract(pred_left, pred_right), 1)), tf.float32) # the variable I want

    # I use it here when calculating another named tensor, if that helps.
    con_loss = contrastive_loss(l2_dist) 
    loss = tf.reduce_sum(con_loss, name="loss")

有没有办法找到没有名字的变量?

Is there any way of finding the variable without a name?

推荐答案

首先,跟进我的第一条评论,tf.get_collection 给定的名称范围不起作用.从 文档 中,如果您提供范围,则只有分配了变量或操作名称将被返回.就这样了.

First of all, following up on my first comment, it makes sense that tf.get_collection given a name scope is not working. From the documentation, if you provide a scope, only variables or operations with assigned names will be returned. So that's out.

您可以尝试的一件事是在您的 中列出每个节点的名称Graph with:

One thing you can try is to list the name of every node in your Graph with:

print([node.name for node in tf.get_default_graph().as_graph_def().node])

或者,从检查点恢复时:

Or possibly, when restoring from a checkpoint:

saver = tf.train.import_meta_graph(/path/to/meta/graph)
sess = tf.Session()
saver.restore(sess, /path/to/checkpoints)
graph = sess.graph
print([node.name for node in graph.as_graph_def().node])

另一种选择是使用 tensorboard 或 Jupyter Notebook 和 show_graph 命令.现在可能有一个内置的 show_graph,但该链接指向一个已定义的 git 存储库.然后,您必须在图中搜索您的操作,然后可能使用以下命令检索它:

Another option is to display the graph using tensorboard or Jupyter Notebook and the show_graph command. There might be a built-in show_graph now, but that link is to a git repository where one is defined. You will then have to search for your operation in the graph and then probably retrieve it with:

my_op = tf.get_collection('full_operation_name')[0]

如果您想在将来设置它以便您可以按名称检索它,您需要使用 tf.add_to_collection:

If you want to set it up in the future so that you can retrieve it by name, you need to add it to a collection using tf.add_to_collection:

my_op = tf.some_operation(stuff, name='my_op')
tf.add_to_collection('my_op_name', my_op)

然后通过恢复图形然后使用:

Then retrieve it by restoring your graph and then using:

my_restored_op = tf.get_collection('my_op_name')[0]

您也可以通过命名它然后在 tf.get_collection 中指定其范围来获取,但我不确定.可以在此处找到更多信息和有用的教程.

You might also be able to get by just naming it and then specifying its scope in tf.get_collection instead, but I am not sure. More information and a helpful tutorial can be found here.

这篇关于在 tensorflow 中检索未命名的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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