Tensorflow:跨图形传输变量 [英] Tensorflow: transferring variables across graphs

查看:24
本文介绍了Tensorflow:跨图形传输变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何将学习到的保存模型作为更大模型的一部分导入.

I am trying to figure out how to import a learned saved model as a part of a larger model.

具体来说,我有一个 RNN 和一些嵌入矩阵(模型 A),它们已经在存储在 .ckpt 文件中的大型数据集上进行了训练.我还有另一个模型(模型 B),它使用完全相同的 RNN 和嵌入模型作为子模型,并带有额外的操作来操纵这个 RNN 的输出并将其用于分类.

To be concrete, I have an RNN and some embedding matrices (model A) that have been trained on a large dataset stored in a .ckpt file. I also have another model (model B), that uses this exact same RNN and embedding model as a submodel, with additional ops to manipulate the output of this RNN and use it for classification.

这两个模型的图定义不同,但我想将模型 B 初始化为模型 A 的保存版本.如何做到这一点?

The graph definitions for these two models are different, but I'd like to initialize model B to the saved version of model A. How can this be done?

到目前为止,我的尝试是尝试将模型 A 加载为不同会话和图形下的另一个模型,然后将模型 B 中的相关矩阵分配给 A 中的矩阵,但这不起作用.

My attempt so far is to try and load model A as another model under a different session and graph and then assign the relevant matrices in model B to those from A, but this isn't working.

这是代码的相关部分:

sup = supervised() # spins up a class with an interactive session inside and sets up the graph
g = tf.Graph()
with g.as_default():
    unsup = unsupervised('unsup.ckpt') # loads in model A from file 
                                       # w/ another session (not interactive)
    # get matrix from unsup and assign to sup
    sup._word_embeddings.assign(unsup.session.run(unsup._word_embeddings))
    # do the same for the RNN
    sup._gate_matrix.assign(unsup.session.run(unsup._gate_matrix))
    sup._gate_bias.assign(unsup.session.run(unsup._gate_bias))
    sup._cand_matrix.assign(unsup.session.run(unsup._cand_matrix))
    sup._cand_bias.assign(unsup.session.run(unsup._cand_bias))

请注意,在实例化类时,两个模型都使用 tf.get_variable() 为 RNN 分配了来自 rnn_cell.linear 的矩阵的显式变量.

Note that both models have explicit variables assigned to the matrices from rnn_cell.linear for the RNN using tf.get_variable() when instantiating the class.

提前致谢!

推荐答案

答案原来如此简单.这只是创建分配操作...要实际进行分配,请在适当的会话中运行操作.

The answer turned out to be so easy. This was just creating the assign ops... To actually do the assignment, run the ops in the appropriate session.

这篇关于Tensorflow:跨图形传输变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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