从纯文本文件中恢复 Tensorflow 变量文件 [英] Restore Tensorflow Variable files from Plaintext files

查看:23
本文介绍了从纯文本文件中恢复 Tensorflow 变量文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我使用此代码将所有变量文件转储为纯文本:

So, I'm using this code to dump all variable files to plaintext:

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()

从这些文件中恢复模型的最佳方法是什么?

What is the best was to restore the model from these files?

保存功能:

def SaveToPlaintext(self, variable_files: Union[str, List[str]]) -> None:
    cwd = os.getcwd()
    var_outdir = os.path.join(cwd,'variables/')
#print(cwd, var_outdir)
    log("saving variable files to '{}'."
        .format(var_outdir))
    if not os.path.exists(var_outdir):
        os.makedirs(var_outdir, 0o777)
    for x in tf.all_variables():
        print(tf.get_variable_scope())
        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 = self.sessions[0]).tofile(file, sep='\t')
        file.close()

恢复功能:

 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})

推荐答案

这里是一个示例,演示如何在创建变量后为变量分配自定义值.

here is an example demonstrating how you can assign a custom value to a variable after creating it.

x = tf.Variable(2.)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(x)

现在你可以看到这个变量的值是2,现在改变它的值

now you can see the value of this variable is 2, now change its value

sess.run("Variable/Assign", feed_dict={"Variable/initial_value:0": 3.})
sess.run(x)

现在这个变量的值更改为 3.

now value of this variable is changed to 3.

回到问题,首先你需要重建图形和一个将变量名映射到它的值的字典,如 {"var1": [1, 2], "var2": [2, 3]},你可以从你保存的文本文件中获取这本词典.然后当你有这个字典时,运行这个命令 sess.run(["var1/Assign", "var2/Assign"], feed_dict={"var1/initial_value:0": [1, 2], "var2/initial_value:0": [2, 3]})

back to question, first you need to rebuild the graph and also an dictionary which maps variable name to its value like {"var1": [1, 2], "var2": [2, 3]}, you can get this dictionary from your saved text file. Then when you have this dictinary, run this command sess.run(["var1/Assign", "var2/Assign"], feed_dict={"var1/initial_value:0": [1, 2], "var2/initial_value:0": [2, 3]})

这篇关于从纯文本文件中恢复 Tensorflow 变量文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆