TensorFlow saver specifies variable access, tensorflowsaver

Source: Internet
Author: User

TensorFlow saver specifies variable access, tensorflowsaver

Today, I would like to share with you the point of using the saver of TensorFlow to access the trained model.

1. Use saver to access variables;
2. Use saver to access specified variables.

Use saver to access variables.

Let's not talk much about it. first go to the code

# Coding = utf-8import OS import tensorflow as tfimport numpyos. environ ['tf _ CPP_MIN_LOG_LEVEL '] = '2' # Some instruction sets are not installed. Do not display the warning w = TF. variable ([[1, 2, 3], [2, 3], [6, 7, 8], dtype = tf. float32) B = tf. variable ([[4, 5, 6], dtype = tf. float32,) s = tf. variable ([2, 5], [5, 6], dtype = tf. float32) init = tf. global_variables_initializer () saver = tf. train. saver () with tf. session () as sess: sess. run (init) save_path = saver. save (sess, "save_net.ckpt") # set the path to print ("save to path:", save_path)

Here, I define several variables and save them. After running, the variables w, B, and s will be saved. Save to generate the following files:

  • Cheakpoint
  • Save_net.ckpt.data -*
  • Save_net.ckpt.index
  • Save_net.ckpt.meta

The following is the read code.

import tensorflow as tfimport osimport numpy as npos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'w = tf.Variable(np.arange(9).reshape((3,3)),dtype=tf.float32)b = tf.Variable(np.arange(3).reshape((1,3)),dtype=tf.float32)a = tf.Variable(np.arange(4).reshape((2,2)),dtype=tf.float32)saver =tf.train.Saver()with tf.Session() as sess: saver.restore(sess,'save_net.ckpt') print ("weights",sess.run(w)) print ("b",sess.run(b)) print ("s",sess.run(a))

When writing and reading code, you must note that the type, size, quantity, and sequence of the variable definition must be consistent with that of the variable. Otherwise, an error will be reported. The order of data stored is w, B, and s. During storage, w defines the dtype and does not define the name. The same is true for retrieval, because TensorFlow access is accessed based on key-value pairs, so they must be consistent. The variable names, such as w and s, can be different.

The following is the result of my successful reading.

Use saver to access specified variables.

Some variables do not need to be saved during training, but tf. train. Saver () is used directly (). The program will save all the variables. At this time, we can specify to save, only save the variables we need, and discard all the other variables.
In fact, it is very simple. You only need to slightly modify the above Code, just replace tf. train. Saver () with the following code:

program = []program += [w,b]tf.train.Saver(program)

In this way, the program will only store w and B. Similarly, tf. train. Saver () in the read program must be modified as above. Dtype and name must be consistent.

Final code is attached:

# Coding = UTF-8 # saver saves the variable test import OS import tensorflow as tfimport numpyos. environ ['tf _ CPP_MIN_LOG_LEVEL '] = '2' # Some instruction sets are not installed. Do not display the warning w = TF. variable ([[1, 2, 3], [2, 3], [6, 7, 8], dtype = tf. float32) B = tf. variable ([[4, 5, 6], dtype = tf. float32,) s = tf. variable ([2, 5], [5, 6], dtype = tf. float32) init = tf. global_variables_initializer () program = [] program + = [w, B] saver = tf. train. saver (program) with tf. session () as sess: sess. run (init) save_path = saver. save (sess, "save_net.ckpt") # set the path to print ("save to path:", save_path)
# Saver extraction variable test import tensorflow as tfimport osimport numpy as npos. environ ['tf _ CPP_MIN_LOG_LEVEL '] = '2' w = TF. variable (np. arange (9 ). reshape (3, 3), dtype = tf. float32) B = tf. variable (np. arange (3 ). reshape (1, 3), dtype = tf. float32) a = tf. variable (np. arange (4 ). reshape (2, 2), dtype = tf. float32) program = [] program + = [w, B] saver = tf. train. saver (program) with tf. session () as sess: saver. restore (sess, 'Save _ net. ckpt ') print ("weights", sess. run (w) print ("B", sess. run (B) # print ("s", sess. run ())

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.