所以,我使用这段代码将所有变量文件转储为纯文本:for x in tf.all_variables():
log("saved variable '{}'.".format(x.name))
newname = x.name.replace("/",".")
current_file = os.path.join(var_outdir, newname)
file = open(current_file, 'w')
x.eval(session = CONFIG.model.tf_manager.sessions[0]).tofile(file, sep='\t')
file.close()
从这些文件恢复模型的最佳方法是什么?在
编辑:
保存功能:
^{pr2}$
恢复功能:def restore_from_text(self, variables_dir, meta_file) -> None:
saver_graph = tf.train.import_meta_graph('{}'.format(meta_file))
for filename in os.listdir(variables_dir):
variable_name = filename.replace(".","/")
variable_files_txt = {}
file_location = os.path.join(variables_dir, filename)
variable_file = open(file_location, 'r')
variable = np.fromfile(variable_file, dtype=float, count=-1, sep='\t')
#init = tf.constant(variable)
variable_name = filename.replace(":0","")
#v = tf.get_variable(variable_name, initializer=init)
variable_files_txt[variable_name] = variable
sess = self.sessions[0]
for var_name, var in variable_files_txt.items():
sess.run([var_name+"/Assign"], feed_dict={var_name+"/initial_value:0": var})