1

嗨,我正在浏览谷歌在https://github.com/tensorflow/models提供的 /models-master/tutorials/image/cifar10 示例。

我在虚拟环境中运行 tensorflow-1.0.1 和 python 3.5。

从命令行,并在 virtualenv 中,运行: python3 cifar10_train.py 工作正常。

但是当我尝试时:pudb3 cifar10_train.py

我收到此错误:

回溯(最近一次调用):
文件“~/interpreters/p35/lib/python3.5/site-packages/tensorflow/python/platform/app.py”,第 44 行,运行
_sys.exit(main(_sys. argv[:1] + flags_passthrough))
TypeError: main() 接受 0 个位置参数,但给出了 1 个

检查参数给出:

打印 (_sys.argv[:1])
['cifar10_train.py']
打印 (flags_passthrough)
[]

我知道 pudb 位于从命令行运行代码的同一 virtualenv 中,因为 pudb 很好地通过了 tensorflow 导入,并且 virtualenv 是 tensorflow 包所在的唯一位置。

我假设这是在 pudb 引入的某些层之间传递的一些问题......有没有人有一个快速的建议来解决这个问题......我只想逐步完成代码:)

谢谢,

nt

4

3 回答 3

0

我在使用 pudb 和 TensorFlow 时遇到了同样的问题。它与 tf.flags 有关。我只使用 python 的 Argparse 类而不是 tf.flags。

作为替代方案,我相信您可以保留 tf.flags 原样,并from pudb import set_trace; set_trace()在 tensorflow 导入后添加您的代码。将您的脚本称为python script.pywihtout -m pudb,它不应该吓坏了。

于 2017-04-11T19:02:37.140 回答
0

vega 的建议奏效了……谢谢。我会评价你的评论,但我的代表<15...还有什么新的:)

所以按照维加...

在“import tensorflow as tf”语句之后包含“from pudb import set_trace”。然后添加 set_trace() 如下...

def main(argv=None):  # pylint: disable=unused-argument
  cifar10.maybe_download_and_extract()
  if tf.gfile.Exists(FLAGS.train_dir):
    tf.gfile.DeleteRecursively(FLAGS.train_dir)
  tf.gfile.MakeDirs(FLAGS.train_dir)
  train()


if __name__ == '__main__':
  set_trace()    
  tf.app.run()

从命令行调用脚本:python3 cifar10_train.py

它按预期工作。

PuDB 看起来将成为一个很棒的工具。

于 2017-04-11T20:12:48.857 回答
0

似乎tensorflow调用了错误的主函数。我在使用cProfile和调用脚本时遇到了类似的问题

python -m cProfile train.py

似乎问题在于 tf.app.run 在 cProfile 中调用了 main ,它还没有准备好传递参数。在我的情况下,解决方案是指定 main in tf.app.run()

tf.app.run(main=main)

还需要 main 中的假参数def main(_):

于 2018-12-27T14:55:48.803 回答