2017-10-04 56 views
0

我試着轉換成字符串並儲存,但不能將其轉換回原始類型如何寫列表來自tf.get_collection返回到一個文件和讀取它

我也試着和pickle.dump但它給下面的錯誤

raise TypeError, "can't pickle %s objects" % base.__name__ 
TypeError: can't pickle module objects 

我的代碼:

with tf.Session() as sess: 
    restorer = tf.train.import_meta_graph('abcd.ckpt.meta') 
    restorer.restore(sess,'abcd.ckpt') 
    vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) 
    with open("pickle_target.txt", "wb") as fp: 
     pickle.dump(vars, fp) 

我需要將tf.get_collection保存到一個文件,進行編輯,然後重新讀給一個列表。

+1

您應該包含代碼/示例的相關部分以複製問題 –

+0

對不起!我現在編輯它:)請看看它 –

回答

0

tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)給出了變量列表,而不是存儲在這些變量中的值。要獲得變量的當前值,你必須運行在會話變量列表:

vars_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) 
with tf.Session() as sess: 
    restorer = tf.train.import_meta_graph('abcd.ckpt.meta') 
    restorer.restore(sess,'abcd.ckpt') 
    vars = sess.run(vars_list) 

現在瓦爾是一個正常的Python列表與目前的變量值。

相關問題