在使用許多具有相同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標誌你無法做到的事情?這是否意味着不能在整個腳本中重構常用標誌設置?
非常感謝! ! !現在完美運作。你剛剛救了我幾個小時的工作後:-)乾杯 – user1400916