SoFunction
Updated on 2024-11-10

Two ways to save/load TensorFlow models

TensorFlow Model Save/Load

When we use an algorithmic model on line, we must first save the model that has been trained. tensorflow saves the model in a way that is not quite the same as sklearn, sklearn is very straightforward, a dump and load method can be saved and loaded for use. But tensorflow due to graph, operation these concepts, save and load the model is slightly troublesome.

I. Basic methodology

Searching online for tensorflow model preservation, most of what you find is the basic approach. Namely

save (a file etc) (computing)

  • define variable
  • Save using the () method

go down (in history)

  • define variable
  • loaded using the () method

as ifsave (a file etc) (computing)The code is as follows

import tensorflow as tf 
import numpy as np 

W = ([[1,1,1],[2,2,2]],dtype = tf.float32,name='w') 
b = ([[0,1,2]],dtype = tf.float32,name='b') 

init = tf.initialize_all_variables() 
saver = () 
with () as sess: 
  (init) 
  save_path = (sess,"save/") 

go down (in history)The code is as follows

import tensorflow as tf 
import numpy as np 

W = (tf.truncated_normal(shape=(2,3)),dtype = tf.float32,name='w') 
b = (tf.truncated_normal(shape=(1,3)),dtype = tf.float32,name='b') 

saver = () 
with () as sess: 
  (sess,"save/") 

The inconvenience of this approach is that when you use the model, you have to redefine the structure of the model and then load the values of the variables with the corresponding names. However, in many cases we would prefer to be able to read a file and use the model directly instead of having to redefine the model. So we need to use another method.

II. Approaches that do not require redefinition of the network structure

.import_meta_graph

import_meta_graph(
 meta_graph_or_file,
 clear_devices=False,
 import_scope=None,
 **kwargs
)

This method loads all the nodes of the saved graph from the file into the current default graph and returns a saver, which means that when we save, we save not only the values of the variables, but also the various nodes in the corresponding graph, so the structure of the model is also saved.

For example, if we want to save the y that computes the final prediction, we should add it to the collection during the training phase. The code is as follows

save (a file etc) (computing)

### Define the model
input_x = (tf.float32, shape=(None, in_dim), name='input_x')
input_y = (tf.float32, shape=(None, out_dim), name='input_y')

w1 = (tf.truncated_normal([in_dim, h1_dim], stddev=0.1), name='w1')
b1 = (([h1_dim]), name='b1')
w2 = (([h1_dim, out_dim]), name='w2')
b2 = (([out_dim]), name='b2')
keep_prob = (tf.float32, name='keep_prob')
hidden1 = ((self.input_x, w1) + b1)
hidden1_drop = (hidden1, self.keep_prob)
### Define forecasting objectives
y = ((hidden1_drop, w2) + b2)
# Create saver
saver = (...variables...)
# If y needs to be saved for use in forecasting
tf.add_to_collection('pred_network', y)
sess = ()
for step in xrange(1000000):
 (train_op)
 if step % 1000 == 0:
  # Save the checkpoint, and also export a meta_graph by default.
  # graph named 'my-model-{global_step}.meta'.
  (sess, 'my-model', global_step=step)

go down (in history)

with () as sess:
 new_saver = .import_meta_graph('my-save-dir/')
 new_saver.restore(sess, 'my-save-dir/my-model-10000')
 # tf.get_collection() returns a list. But only the first parameter is needed here.
 y = tf.get_collection('pred_network')[0]

 graph = tf.get_default_graph()

 # Because there are placeholders in y, the (y) time also needs to populate those placeholders with the actual samples to be predicted and the corresponding parameters, which need to be obtained via the get_operation_by_name method of graph.
 input_x = graph.get_operation_by_name('input_x').outputs[0]
 keep_prob = graph.get_operation_by_name('keep_prob').outputs[0]

 # Predictions using y
 (y, feed_dict={input_x:...., keep_prob:1.0})

There are two things to keep in mind here:

I. The name of the file to fill in when (), because at the time of, each checkpoint will save three files, such as
, , -00000-of-00001
existimport_meta_graphWhen you fill in the name of the meta file, we know that the weights are stored in the-00000-of-00001in this file, but if you fill in this filename in the restore method, you will get an error, and you should fill in the prefix, which can be used with the.latest_checkpoint(checkpoint_dir)This method fetches.

Second, the model's y is used in the placeholder, in the () time must feed the corresponding data, so also according to the name of the specific placeholder, from the graph to use theget_operation_by_namemethod to get it.

This is the whole content of this article.