TensorFlow model Save and load _ neural network

Source: Internet
Author: User
Tags vars

http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/
What is a TF model:

After training a neural network model, you will save the model for future use or deployment to the product. So, what is the TF model. The TF model basically contains network design or graph, and train the network parameters and variables. Therefore, the TF model has two main files:
a) Meta chart
This is a proposed cache that contains the complete information for this TF diagram, such as all variables and so on. The file ends with. Meta.
b) checkpoint file:
This file is a binary file that contains the values of ownership weights, offsets, gradients, and all other stored variables. This file ends with. ckpy. However, TF is no longer in this form after version 0.11. Instead, the file contains the following files:
mymodel.data-00000-of-00001
Mymodel.index
The. Data file contains training variables.
In addition, TF contains a file called "Checkpoint" that holds the file for the last checkpoint.
So, in summary, the TF model contains the following files: my_test_model.data-00000-of-00001 my_test_model.index my_test_model.meta checkpoint**

2 Save a TF model
Saver = Tf.train.Saver ()
Note that you need to save this model in a session
Python
1saver.save (Sess, ' my-model-name ')
The complete example is:

Import TensorFlow as tf
w1 = tf. Variable (Tf.random_normal (shape=[2]), name= ' W1 ')
w2 = tf. Variable (Tf.random_normal (shape=[5]), name= ' W2 ')
saver = Tf.train.Saver () sess
= tf. Session ()
Sess.run (Tf.global_variables_initializer ())
Saver.save (sess, ' My_test_model ')

If you save this model after 1000 steps in the TF model iteration, you can specify the number of steps
Saver.save (Sess, ' My_test_model ', global_step=1000)

3. Load a pre-trained model
A) Create a network
Use the Tf.train.import () function to load a previously saved network.
Saver = tf.train.import_meta_graph (' My-model-1000.meta ')
Note that Import_meta_graph will be added to the current diagram with the diagrams saved in the. meta file. So, we created a diagram/network, but we used the parameters that needed to load the training into this diagram.

b) Load Parameters

' Restore tensor from model '
w_out= self.graph.get_tensor_by_name (' w:0 ')
b_out = self.graph.get_tensor_by _name (' b:0 ')
_input = Self.graph.get_tensor_by_name (' x:0 ')
_out = Self.graph.get_tensor_by_name (' y:0 ')
y_pre_cls = self.graph.get_tensor_by_name (' output:0 ')

Note Question 1:
If the initial save location is E:, the location is saved in the checkpoint
After modification:
Model_checkpoint_path: "E:\tmp\newModel\crack_capcha.model-8100"
All_model_checkpoint_paths: "E:\tmp\newModel\crack_capcha.model-8100"

The description of the process image
Technically, this are all your need to know to create a class-based neural network that defines the fit (x, Y) and predict (x) Functions.

See StackOverflow explanation
TensorFlow version 0.11.0rc1, can save and restore your model directly by calling Tf.train.export_meta_ Graph and Tf.train.import_meta_graph according tohttps://www.tensorflow.org/programmers_guide/meta_graph
Save Model:

W1 = tf. Variable (Tf.truncated_normal (shape=[10]), name= ' W1 ')
w2 = tf. Variable (Tf.truncated_normal (shape=[20]), name= ' W2 ')
tf.add_to_collection (' VARs ', W1)
Tf.add_to_ Collection (' VARs ', w2)
saver = Tf.train.Saver ()
sess = tf. Session ()
Sess.run (Tf.global_variables_initializer ())
Saver.save (sess, ' My-model ')

**# Save method would call Export_meta_graph implicitly.
You'll get saved graph files:my-model.meta**
Restore Model:

Sess = tf. Session ()
new_saver = tf.train.import_meta_graph (' My-model.meta ')
new_saver.restore (Sess, Tf.train.latest _checkpoint ('./'))
all_vars = tf.get_collection (' VARs ') for V-in
all_vars:
    v_ = Sess.run (v)
    print (V_)

A complete example:
Self.session = tf. Session (Graph=self.graph)

With Self.graph.as_default (): ### #默认图与自定义图的关系 ckpt = tf.train.get_checkpoint_state (self.savefile) if ckpt and CK Pt.model_checkpoint_path:print ('. Join ([Ckpt.model_checkpoint_path, '. Meta ']) Self.saver = Tf.train . Import_meta_graph ('. Join ([Ckpt.model_checkpoint_path, '. Meta ']) Self.saver.restore (self.session,ckpt.model_ch Eckpoint_path) #print all variable to op in Self.graph.get_operations (): Print (Op.name, "", Op.type ) #返回模型中的tensor layers = [op.name for op in Self.graph.get_operations () if op.type== ' conv2d ' and ' import/' I N Op.name] layers = [op.name for op in Self.graph.get_operations ()] feature_nums = [Int (self.graph.get_tenso

     R_by_name (name+ ': 0 '). Get_shape () [-1]) for name in layers] for feature in Feature_nums:print (feature) ' Restore tensor from model ' w_out = self.graph.get_tensor_by_name (' w:0 ') b_out = Self.graph.get_tensor_ By_name (' b:0 ') _inpUT = self.graph.get_tensor_by_name (' x:0 ') _out = Self.graph.get_tensor_by_name (' y:0 ') Y_pre_cls = Self.graph.get _tensor_by_name (' output:0 ') #self. Session.run (Tf.global_variables_initializer ()) ### #非常重要, cannot add this sentence pred = SE Lf.session.run (y_pre_cls,feed_dict={_input:_x}) return pred

There are many pits in the middle, but after a successful load execution, the understanding of the model is deepened

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.