Tensorflow: Using weights trained in one model inside another, different model

淺唱寂寞╮ 提交于 2019-12-04 09:16:17

What is the proper way to move trained LSTM weights from one graph to another?

You can create your decoding graph first (with a saver object to save the parameters) and create a GraphDef object that you can import in your bigger training graph:

basegraph = tf.Graph()
with basegraph.as_default():
   ***your graph***

traingraph = tf.Graph()
with traingraph.as_default():
     tf.import_graph_def(basegraph.as_graph_def())
     ***your training graph***

make sure you load your variables when you start a session for a new graph.

I don't have experience with this functionality so you may have to look into it a bit more

Is it correct to say that starting a new session "releases resources", but doesn't erase the graph built in memory?

yep, the graph object still hold it

It seems to me like the 'reuse' feature allows Tensorflow to search outside of the current variable scope for variables with the same name (existing in a different scope), and use them in the current scope. Is this correct? If it is, what happens to all of the graph edges from the non-current scope that link to that variable? If it isn't, why does Tensorflow throw an error if you try to have the same variable name within two different scopes? It seems perfectly reasonable to define two variables with identical names in two different scopes, e.g. conv1/sum1 and conv2/sum1.

No, reuse is to determine the behaviour when you use get_variable on an existing name, when it is true it will return the existing variable, otherwise it will return a new one. Normally tensorflow should not throw an error. Are you sure your using tf.get_variable and not just tf.Variable?

In my code I'm working within a new scope but the graph won't run without data to be fed into a placeholder from the initial, default scope. Is the default scope always 'in-scope' for some reason?

I don't really see what you mean. The do not always have to be used. If a placeholder is not required for running an operation you don't have to define it.

If graph edges can span different scopes, and names in different scopes can't be shared unless they refer to the exact same node, then that would seem to defeat the purpose of having different scopes in the first place. What am I misunderstanding here?

I think your understanding or usage of scopes is flawed, see above

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!