2016-08-11 23 views
1

在使用許多具有相同Tensorflow標誌的Python腳本後,我厭倦了爲每次更改更新多個標誌,因此決定將tf.app.flags重構爲一個單獨的類,我可以在腳本中重複使用該類。重構Tensorflow FLAGS

但是,由於某種奇怪的原因,無論何時我在其他方法中使用self.flags時,都無法識別先前設置的標誌。例如下面的類將工作的優良標誌project_dir2但失敗的標誌project_dir3

`class MyClass(): 
    def __init__(self): 
    self.flags = tf.app.flags 
    self.FLAGS = self.flags.FLAGS 

    #test code that works here 
    self.flags.DEFINE_string("project_dir2", "aValue", "project directory") 
    print("This will print correctly: "+self.FLAGS.project_dir2) 
    self.my_function() 

    def my_function(self): 
    #test code that fails 
    self.flags.DEFINE_string("project_dir3", "aValue", "project directory") 
    print("This will fail: "+self.FLAGS.project_dir3)` 

我得到以下異常:

AttributeError: project_dir2 Exception TypeError: TypeError("'NoneType' object is not callable",) in <function _remove at 0x7fd4c3090668> ignored

有什麼明顯的,我做錯了什麼?或者這是Tensorflow標誌你無法做到的事情?這是否意味着不能在整個腳本中重構常用標誌設置?

回答

0

看起來有一種叫做_parse_flags()的內部方法叫做on first access。你可以手動調用它,你更新

IE

def my_function(self): 
    #test code that fails 
    self.flags.DEFINE_string("project_dir3", "aValue", "project directory") 
    self.flags.FLAGS._parse_flags() 

上tf.flags背景之後 - 這是一個局部重新實現谷歌的gflags庫,所以它缺少的功能/文檔。這是一個更聰明的做法,比如插入官方gflags(issue 1258)。這允許像控制詳細日誌記錄(這需要重新編譯right now

+0

非常感謝! ! !現在完美運作。你剛剛救了我幾個小時的工作後:-)乾杯 – user1400916