2017-08-26 134 views
0

我正在用ImageNet訓練我的網絡,因此我可以爲我的項目使用訓練後的權重子集。無法在張量流中保存/恢復變量的子集

保存和恢復孔權是沒有問題的,但是當我嘗試將它們保存而不完全連接層它給了我一個錯誤:NameError:全局名稱「W1」沒有定義。如果有幫助的人,倉庫是在github或代碼片段:

inference.py

... 
def inference(images): 
    w1 = tf.get_variable('w1', shape=[5,5,3,64]) 
    ... 

grasp.py

def run_training(): 
    ... 
    logits = inference(images) 
    ... 
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) 
sess = tf.Session() 
sess.run(init_op) 
saver = tf.train.Saver({'w1': w1}) 

回溯

Traceback (most recent call last): 
    File "./grasp.py", line 130, in <module> 
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) 
    File "/usr/local/lib/python2.7/site- 
    packages/tensorflow/python/platform/app.py", line 44, in run 
    _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 
    File "./grasp.py", line 83, in main 
    run_training() 
    File "./grasp.py", line 52, in run_training 
    saver = tf.train.Saver({'w1': w1}) 
NameError: global name 'w1' is not defined 

如果您有任何建議或需要更多信息,請讓我知道。

回答

0

這裏您需要訪問tf.global_variables()集合中的變量。

w1 = [v for v in tf.global_variables() if v.name == 'w1:0'][0] 
saver = tf.train.Saver({'w1': w1}) 
+0

我發現這個一個太,'W1 = [V對於在tf.get_collection V(tf.GraphKeys.GLOBAL_VARIABLES)如果v.name == 'W1:0'] [0]'。 – prometeu