保存和加载多计算图模型
pre-knowledge
- ckpt.meta/index/data文件 以及 checkpoint文件
这个ckpt是我们自己给的前缀赋名
.ckpt文件:是旧版本的输出saver.save(sess),相当于你的.ckpt-data
“checkpoint”:文件仅用于告知某些TF函数,这是最新的检查点文件。
.ckpt-meta:包含元图,即计算图的结构,没有变量的值(基本上你可以在tensorboard / graph中看到)。
.ckpt-data:包含所有变量的值,没有结构。
.ckpt-index:可能是内部需要的某种索引来正确映射前两个文件,它通常不是必需的
使用tf.train.Saver保存
(1). save= tf.train.Saver() 是用来保存tensorflow训练模型的,默认保存全部参数 【用来指定保存的变量范围 以及 一些设置】 saver.save()用来设置存储地址、多久保存一次 之类的
(2). 用来加载参数,
注:只加载存储在data中的权重和偏置项等需要训练的参数,其他一律不加载,
包括meta文件中的图
注意无论是参数变量还是计算图 都调用save保存
- 只想load计算图
tf.train.import_meta_graph(".meta文件")
多计算图的数据隔离
- tf.global_variables()是各计算图之间隔离的,里面的代码demo也不错的
reference
- 再看看这个博客:tf.train.Saver() 与tf.train.import_meta_graph区别
- graph和sess之间的关系
- 一个graph可以定义多个sess(当然,一个sess也可以跑多个graph)
- graph只存储节点(Tensor)和control depency
- sess存储Variable和对应的内存,也就是具体的值。不同sess之间的数据也是相互隔离的(e.g. 对应同一个graph,每个sess运行的时候都要initialize_all_variables一遍,因为变量值都存储在每个sess自身)
这里要搞清楚的是Tensor只是数据操作(e.g. Add, Matmul),而不是具体的变量(Variable, which 是有具体的值的)
多进程使用多计算图训练踩坑
-
TF hangs when initializing variables in a multi process setting
里面阐述了为何多进程会hang住,以及处理的两种方法: -
起子进程的 simple sample codes:
for cur_index in range(self.NODE_NUM): # some preparation father_conn, child_conn = Pipe() sub_proc = Process(target=createAgent, args=( child_conn, cur_index, ..., )) sub_proc.daemon = True # 退出主进程的时候紫禁城也会退出,防止泄漏 sub_proc.start() # collect father and children conns in order for afterwards operations sub_proces.append(sub_proc) self.par_conns[cur_index] = father_conn完整版见仓库 fdb0cc2 版本
-
多计算图之间的隔离 sample codes
class ChildNet(): agent_index = 0 def __init__(self, child_conn, dim_state, dim_action, dim_state_g, dim_action_g, act_ind, learning_rate, tau, num_actor_vars, agent_index): #=== gpu_options = tf.GPUOptions(allow_growth=True) self.__sess = tf.Session(graph=tf.Graph(), config=tf.ConfigProto(gpu_options=gpu_options)) self.__conn = child_conn self.__agent_index = agent_index with self.__sess.as_default() as sess: with self.__sess.graph.as_default(): # 这个是必须的!不写的话,会使用默认计算图,而非这个see绑定的计算图 with tf.variable_scope(f'critic_{self.__agent_index}', reuse=tf.AUTO_REUSE): self.__inputs, self.__action, self.__out = self.buildNetwork(scope='performance_network') def buildNetwork(self, scope=None): inputs = tf.placeholder(tf.float32, [None, self.__dim_s_g]) action = tf.placeholder(tf.float32, [None, self.__dim_a_g]) with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): net = inputs net = tf.contrib.layers.fully_connected(net, 128, activation_fn=tf.nn.leaky_relu) net = tf.contrib.layers.fully_connected(net, 32, activation_fn=tf.nn.leaky_relu) net = tf.contrib.layers.fully_connected(tf.concat([net, action], axis=1), 64, activation_fn=tf.nn.leaky_relu) w_init = tflearn.initializations.uniform(minval=-3e-3, maxval=3e-3) with tf.variable_scope('output', reuse=tf.AUTO_REUSE): out = tf.contrib.layers.fully_connected(net, 1, weights_initializer=w_init, activation_fn=None) return inputs, action, out def train(self, inputs, action, q_predicted, is_weight): with self.__sess.as_default() as sess: with self.__sess.graph.as_default(): return sess.run( ..., ..., )完整版见仓库 fdb0cc2 版本
请注意如下代码的必要性:with self.__sess.as_default() as sess: with self.__sess.graph.as_default():
1042

被折叠的 条评论
为什么被折叠?



