AttributeError: 'module' 对象没有 tf.app.run() 的属性 'main' [英] AttributeError: 'module' object has no attribute 'main' for tf.app.run()

查看:35
本文介绍了AttributeError: 'module' 对象没有 tf.app.run() 的属性 'main'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to test a short program, which is pretty simple, shown as follows

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()

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

However, running the program gives the following error message,

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)"函数:

You either need a "def main(args)" function in your file:

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() 可以调用外部函数

or tf.app.run() can call an external function

tf.app.run(my_func)

这篇关于AttributeError: 'module' 对象没有 tf.app.run() 的属性 'main'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆