python - 属性错误 : 'module' object has no attribute 'main' for tf. app.run()

标签 python tensorflow

我正在尝试测试一个小程序,它很简单,如下所示

import numpy as np
import tensorflow as tf    
flags = tf.app.flags
FLAGS = flags.FLAGS    
import tensorvision.train as train
import tensorvision.utils as utils    
flags.DEFINE_string('name', None,
                    'Append a name Tag to run.')

flags.DEFINE_string('hypes', 'hypes/medseg.json',
                    'File storing model parameters.')    
if __name__ == '__main__':
    tf.app.run()

但是,运行程序会出现以下错误信息,

Traceback (most recent call last):
File "train.py", line 43, in <module>
  tf.app.run()
File "/devl/tensorflow/tf_0.12/lib/python3.4/site-  packages/tensorflow/python/platform/app.py", line 39, in run
main = main or sys.modules['__main__'].main
AttributeError: 'module' object has no attribute 'main'

最佳答案

你要么在你的文件中需要一个“def main(args)”函数:

import numpy as np
import tensorflow as tf    
flags = tf.app.flags
FLAGS = flags.FLAGS    
import tensorvision.train as train
import tensorvision.utils as utils    

def main(args):
    flags.DEFINE_string('name', None,
                        'Append a name Tag to run.')

    flags.DEFINE_string('hypes', 'hypes/medseg.json',
                        'File storing model parameters.')    
if __name__ == '__main__':
    tf.app.run()

或者 tf.app.run() 可以调用外部函数

tf.app.run(my_func)

关于python - 属性错误 : 'module' object has no attribute 'main' for tf. app.run(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43004243/

相关文章:

machine-learning - 当 Seq2Seq 网络在输出中一遍又一遍地重复单词时该怎么办?

python - 如何在 python 3.6 中导入我自己的模块?

Python 使用 sys.stdout 打印尾随逗号行为

python - pytest fixture 和测试都依赖于另一个 fixture

python - TensorFlow导入错误: no module named _pywrap_tensorflow

tensorflow - TF/喀拉斯 : ModelCheckpoint "period" and "save_best_only"

machine-learning - 在 Keras 中使用 Google MobileNets?

python - 在 python 上求解复杂的隐式方程

python - Django:带有限制子句的关系的求和注释

python - 我可以将多个不同的图像作为 channel 吗?