今天训练网上的代码,目前想基于他们训练好的代码,restore ckpt文件的参数以后,再接着训练。
但是加载完数据以后,报错为
Adam_1 not found in checkpoint
经过查找,应该是他们训练好的模型,没有存这些参数,经过查找方法,在GitHub上找到了答案,具体就是在checkpoint里找到所有的参数,和model文件里面的匹配,匹配上就用,匹配不上就算了。
def optimistic_restore(session, save_file):
reader = tf.train.NewCheckpointReader(save_file)
saved_shapes = reader.get_variable_to_shape_map()
var_names = sorted([(var.name, var.name.split(':')[0]) for var in tf.global_variables()
if var.name.split(':')[0] in saved_shapes])
restore_vars = []
name2var = dict(zip(map(lambda x:x.name.split(':')[0], tf.global_variables()), tf.global_variables()))
with tf.variable_scope('', reuse=True):
for var_name, saved_var_name in var_names:
curr_var = name2var[saved_var_name]
var_shape = curr_var.get_shape().as_list()
if var_shape == saved_shapes[saved_var_name]:
restore_vars.append(curr_var)
saver = tf.train.Saver(restore_vars) #只加载checkpoint有参数的
saver.restore(session, save_file)