6_1 persistence model and re-loading _ discussion (1) The problem of variable management naming confusion in _import_meta_graph mode load persistence model

Source: Internet
Author: User

The problem description address that I submitted to GitHub is: https://github.com/tensorflow/tensorflow/issues/20140

Problem Description (English version):

Write the code below, and save it to a, ckpt as a model

Import TensorFlow asTF ' v1= TF. Variable (Tf.constant (1.0, shape=[1]), name ="v1") V2= TF. Variable (Tf.constant (2.0, shape=[1]), name ="v2") v3= TF. Variable (Tf.constant (3.0, shape=[1]), name ="v3") Result=v1+V2RESULT2= result +V3init_op=Tf.global_variables_initializer () Saver=Tf.train.Saver () with TF. Session () asSess:sess.run (init_op) writer= Tf.summary.FileWriter ('./graphs/const_add', Sess.graph) saver.save (Sess,"saved_model/model.ckpt")`

Then in another. PY, we restore the model from the Model.ckpt file

ImportTensorFlow as Tfsaver= Tf.train.import_meta_graph ("Saved_model/model.ckpt.meta") with TF. Session () as Sess:saver.restore (Sess,"saved_model/model.ckpt")    Print(Sess.run (Tf.get_default_graph (). Get_tensor_by_name ("add:0")))    #Sess.run (Tf.assign (v1,[10])) #直接这样使用v1, you will be prompted V1 not defined        #with Tf.variable_scope ("", REUSE=TF. Auto_reuse):With Tf.variable_scope ("", reuse=False): v1=tf.get_variable (name="v1", shape=[1])        Print(V1.name) Sess.run (tf.assign (v1,[10]))    """④ Output All the variable names that can be trained, that is, the parameters of the neural network"""Trainable_variables=tf.trainable_variables () variable_list_name= [C.name forCinchtf.trainable_variables ()] Variable_list=Sess.run (variable_list_name) forKvinchZip (variable_list_name,variable_list):Print("Variable Name:", K)Print("Shape:", V.shape)#Print (v)    """④ Output All the variable names that can be trained, that is, the parameters of the neural network"""    Print(Sess.run (Tf.get_default_graph (). Get_tensor_by_name ("v1:0")))    Print(Sess.run (Tf.get_default_graph (). Get_tensor_by_name ("add:0")))    Print(Sess.run (Tf.get_default_graph (). Get_tensor_by_name ("add_1:0")))    Print(Sess.run (Tf.get_default_graph (). Get_tensor_by_name ("v1_1:0")))

The results would be as below:

We'll find that:
If we restore some variables from the already existed model file "Saved_model/model.ckpt.meta") ",
such as v1,v2,v3 in this example.
It'll influence the process of calling Get_variable. Because of these, causes as below:

    1. The variables restored from the model file such as V1,V2 and V3 won't exist in the scope of get_variable, it means you can only use
with tf.variable_scope("",reuse=False):        v1=tf.get_variable(name="v1",shape=[1])

and create a new variable. You can not reuse the restored variable v1 from the model file unless you define a V1, before you restore from the model File. Like below

  v1=tf.get_variable (name= "v1", shape=[1]) saver = Tf.train.Saver () with TF. Session () as Sess:saver.restore (Sess, "Saved_model/model.ckpt") print (Sess.run (result))  

That's, you can not reuse the restored variable v1 which are from restoring the model file unless your define it befor you Restore.
2. Although TensorFlow doesnot allow reusing the restored variable v1 which are from restoring the model file if you don ' t Define V1 Before you restore the model file.
If you are get_varialbe after you restore the model file, it would create a variable whose name is "V1_1" and not as N Ame= ' v1 ' which you specify.
In my opinion, it should be corrected because it's so confusing. How to correct it?
I think get_variable should also reuse the variables which is loaded by restoring some model file.
The last sentence was what I finally want to say.
My 中文版 is to bad, you can run the code I and would find what I want to convey.
Thanks.

Summarize

Suppose a V1 named variable exists in a persisted model, and if the persisted model is loaded using Import_meta_graph mode.

    • First, the variable naming in the persisted model to be loaded using import_meta_graph cannot conflict with the naming of variables in the current code default calculation diagram
    • Second, if you want to create a relationship between a python variable and a calculated graph variable node by using Tf.get_variable, that is:

Let me add, that is, three ways to load the persistence model:

第一,saver.restore:属于当前代码计算图已经定义,我需要将持久化模型中变量的值加载到当前代码计算图中的变量中去。所以,两者只能是持久化模型中的变量是当前代码计算图中变量集合的一个子集,必须是一种严格包含的关系。

Second, Import_meta_graph does not allow the current code to compute a variable node that has already been defined and the node in the persisted model to be loaded has a conflict because it is worth the entire graph. The value of the variable is not simply loaded.

Thirdly, using PB files, even if the variables node in the persistence model conflicts with the variable nodes defined in the current code calculation diagram, it is not a matter of Mao. Because of the way the PB files are loaded into the calculation diagram,

All the import/ prefixes are added. In other words, this conflict is separated from the namespace.

The above three points are very important to the conclusion of their own commissioning and observation. (A conclusion that is not found in the market book.) )

So, the best way to use someone else's model is to use a PB file. Because there is no possibility of conflict! However, one drawback is that the variables under the import namespace cannot participate in the current training!!! Remember, you can't get involved in training!

    With Tf.variable_scope ("", reuse=False):        v1=tf.get_variable (name="v1  ", shape=[1])        print(v1.name)

is impossible to do. and you can only use Reuse=false, as if: after loading the persistence model, there is no V1 variable node in the calculation diagram.

At the same time, you use the v1=tf.get_variable (name= "v1", Shape=[1]) method to generate a new variable node v1_1 in the current code's calculation diagram, not the variable node V1 in the loaded persistence model. In this case, the function function is invalidated . That is, you want to invoke the Get_variable function so that the Python variable v1 and the variable node v1 in the calculation diagram are bound, but this is not the case, the variable node v1_1 is bound.

So to access the V1 node in the calculation diagram, you can only use the Tf.get_default_graph (). Get_tensor_by_name ("v1:0") way.

Many people may not understand this: 0, this is a operation output. That is to say, the output of the variable node is only one, so we use 0 to refer to the output tensor of this variable node in the calculation diagram.

In fact, you can customize the operation in TensorFlow, which means I can output multiple tensor. At that time: 0,:1 can be used up.

Overall, the author submits this issue to GitHub and is still in a welcome state of discussion . That is, TensorFlow developers need more TensorFlow users to answer: There is no need to fix this problem .

6_1 persistence model and re-loading _ discussion (1) The problem of variable management naming confusion in _import_meta_graph mode load persistence model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.