将特定的TensorFlow变量还原到特定层(按名称还原) [英] Restoring specific TensorFlow variables to a specific layer (Restore by name)

查看:171
本文介绍了将特定的TensorFlow变量还原到特定层(按名称还原)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我训练了一个TensorFlow模型并保存了它,现在有一个不同的模型,并且我想对模型中的某些层(它们具有相同的形状)使用保存的模型中的一些权重。 p>

现在,我能够找到如何从模型(具有特定名称)中保存特定变量的方法,但是我找不到任何通过名称还原这些变量的示例。



例如,假设在保存的模型中,我保存了一个称为 v1的权重张量(具有某种形状)。现在在我的新模型中,我有一个权重张量称为 v2(其形状与 v1张量相同)。现在,我想将保存的变量 v1加载到我的 v2权重张量中,或者甚至更好地将此 v1权重加载到我的新图中的多个张量中。



那有可能吗?如果是这样,我该怎么办?

解决方案

我找到了解决此问题的解决方法。



您可以做的就是将变量值直接存储到磁盘上,既可以作为值也可以作为具有张量名称和值的键的字典。
例如:

  vars_dict = {} 
中的张量(list_of_tensors_you_want_to_save):
vars_dict [tensor.name] = sess.run(tensor)

然后您可以从中加载任何变量字典到您想要的任何其他变量。



在我的示例中,假设原始张量称为 v1,而我要加载的两个张量为 v2和 v3,则可以完成以下操作:

  tensor_to_load_1 = tf.get_default_graph()。get_tensor_by_name( v2)
tensor_to_load_2 = tf.get_default_graph() .get_tensor_by_name( v3)

Assign_op_1 = tf.assign(tensor_to_load_1,vars_dict [ v1])
Assign_op_1 = tf.assign(tensor_to_load_2,vars_dict [ v1]))

sess.run([assign_op_1,Assign_op_2])

当然,仅受以下事实限制: v1, v2和 v3必须具有相同的形状。



使用此示例代码,您可以保存任何变量,并且将它们加载到所需的任何其他变量,而无需原始图形与您当前的图形匹配。


Suppose I trained a TensorFlow model and saved it, now have a different model, and I want to use some of the weights in the saved model for some of the layers in my model (they have the same shape).

Now, I was able to find how to save specific variables from a model (with specific names) but I wasn't able to find any example for restoring those variables by name.

For example suppose that in my saved model I saved a tensor of weights (with some shape) called "v1". Now in my new model I have a weights tensor called "v2" (which have the same shape of the "v1" tensor). Now I want to load the saved variables of "v1" to my "v2" weights tensor, or even better load this "v1" weights to multiple tensor in my new graph.

Is that even possible? If so, how do I do it?

解决方案

I found a workaround to solve this issue.

What you can do is to save the variable values directly to you disk, either as a value or as a dictionary with keys as tensor names and values. For example:

vars_dict = {}
for tensor in (list_of_tensors_you_want_to_save):
     vars_dict[tensor.name] = sess.run(tensor)

Then you can load any variable from this dictionary to any other variable you want.

In my example, suppose the original tensor is called "v1" and the two tensor I want to load are "v2" and "v3", the following can be done:

tensor_to_load_1 = tf.get_default_graph().get_tensor_by_name("v2")
tensor_to_load_2 = tf.get_default_graph().get_tensor_by_name("v3")

assign_op_1 = tf.assign(tensor_to_load_1, vars_dict["v1"])
assign_op_1 = tf.assign(tensor_to_load_2, vars_dict["v1"])

sess.run([assign_op_1, assign_op_2])

This, of course, is only limited by the fact that "v1", "v2" and "v3" must have the same shape.

Using this sample code you can save any variables and load them to any other variables you want, without the need for the original graph to match your current one.

这篇关于将特定的TensorFlow变量还原到特定层(按名称还原)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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