Skip to main content
 首页 » 编程设计

python之如何从 tensorflow 检查点文件正确恢复网络训练

2024年12月31日23shihaiming

我正在努力恢复模型一天,但没有成功。我的代码包含一个 class TF_MLPRegressor(),我在其中定义构造函数中的网络架构。然后调用 fit() 函数进行训练。所以这就是我如何在 fit() 函数中保存一个带有 1 个隐藏层的简单感知器模型:

            starting_epoch = 0 
            # Launch the graph 
            tf.set_random_seed(self.random_state)   # fix the random seed before creating the Session in order to take effect! 
            if hasattr(self, 'sess'): 
                self.sess.close() 
                del self.sess   # delete Session to release memory 
                gc.collect() 
            self.sess = tf.Session(config=self.config) # save the session to predict from new data 
            # Create a saver object which will save all the variables 
            saver = tf.train.Saver(max_to_keep=2)  # max_to_keep=2 means to not keep more than 2 checkpoint files 
            self.sess.run(tf.global_variables_initializer()) 
 
# ... (each 100 epochs) 
 
            saver.save(self.sess, self.checkpoint_dir+"/resume", global_step=epoch) 

然后我使用完全相同的输入参数值创建一个新的 TF_MLPRegressor() 实例,并调用 fit() 函数来恢复模型,如下所示:

    self.sess = tf.Session(config=self.config)  # create a new session to load saved variables 
    ckpt = tf.train.latest_checkpoint(self.checkpoint_dir) 
    starting_epoch = int(ckpt.split('-')[-1]) 
    metagraph = ".".join([ckpt, 'meta']) 
    saver = tf.train.import_meta_graph(metagraph) 
    self.sess.run(tf.global_variables_initializer())    # Initialize variables 
    lhl = tf.trainable_variables()[2] 
    lhlA = lhl.eval(session=self.sess) 
    saver.restore(sess=self.sess, save_path=ckpt)   # Restore model weights from previously saved model 
    lhlB = lhl.eval(session=self.sess) 
    print lhlA == lhlB 

lhlA 和 lhlB 是恢复前后最后的隐藏层权重,根据我的代码,它们完全匹配,即保存的模型没有加载到 session 中。我做错了什么?

请您参考如下方法:

我找到了解决方法!奇怪的是,元图不包含我定义或分配给它们新名称的所有变量。对于构造函数中的示例,我定义了将携带输入特征向量和实验值的张量:

self.x = tf.placeholder("float", [None, feat_num], name='x') 
self.y = tf.placeholder("float", [None], name='y') 

但是,当我执行 tf.reset_default_graph() 并加载元图时,我得到以下变量列表:

[ 
<tf.Variable 'Variable:0' shape=(300, 300) dtype=float32_ref>,  
<tf.Variable 'Variable_1:0' shape=(300,) dtype=float32_ref>,  
<tf.Variable 'Variable_2:0' shape=(300, 1) dtype=float32_ref>,  
<tf.Variable 'Variable_3:0' shape=(1,) dtype=float32_ref> 
] 

郑重声明,每个输入特征向量有 300 个特征。无论如何,当我稍后尝试使用以下方法开始训练时:

_, c, p = self.sess.run([self.optimizer, self.cost, self.pred],  
feed_dict={self.x: batch_x, self.y: batch_y, self.isTrain: True}) 

我收到如下错误:

"TypeError: Cannot interpret feed_dict key as Tensor: Tensor 'x' is not an element of this graph." 

因此,由于每次我创建 class TF_MLPRegressor() 的实例时,我都会在构造函数中定义网络架构,所以我决定不加载元图并且它起作用了!我不知道为什么 TF 不将所有变量保存到元图中,可能是因为我明确定义了网络架构(我不使用包装器或默认层),如下例所示:

https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/4_Utils/save_restore_model.py

总而言之,我按照第一条消息中的描述保存我的模型,但要恢复它们,我使用这个:

saver = tf.train.Saver(max_to_keep=2) 
self.sess = tf.Session(config=self.config)  # create a new session to load saved variables 
self.sess.run(tf.global_variables_initializer()) 
ckpt = tf.train.latest_checkpoint(self.checkpoint_dir) 
saver.restore(sess=self.sess, save_path=ckpt)   # Restore model weights from previously saved model