【问题标题】:Using Sacred Module with iPython在 iPython 中使用神圣模块
【发布时间】:2016-03-30 00:27:22
【问题描述】:

我正在尝试为 Python 设置 sacred,我正在通过 tutorial。我可以使用pip install sacred 毫无问题地设置神圣。我在运行基本代码时遇到问题:

from sacred import Experiment

ex = Experiment("hello_world")

运行此代码将返回 ValueError:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-66f549cfb192> in <module>()
      1 from sacred import Experiment
      2 
----> 3 ex = Experiment("hello_world")

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/experiment.pyc in __init__(self, name, ingredients)
     42         super(Experiment, self).__init__(path=name,
     43                                          ingredients=ingredients,
---> 44                                          _caller_globals=caller_globals)
     45         self.default_command = ""
     46         self.command(print_config, unobserved=True)

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/ingredient.pyc in __init__(self, path, ingredients, _caller_globals)
     48         self.doc = _caller_globals.get('__doc__', "")
     49         self.sources, self.dependencies = \
---> 50             gather_sources_and_dependencies(_caller_globals)
     51 
     52     # =========================== Decorators ==================================

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/dependencies.pyc in gather_sources_and_dependencies(globs)
    204 def gather_sources_and_dependencies(globs):
    205     dependencies = set()
--> 206     main = Source.create(globs.get('__file__'))
    207     sources = {main}
    208     experiment_path = os.path.dirname(main.filename)

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/dependencies.pyc in create(filename)
     61         if not filename or not os.path.exists(filename):
     62             raise ValueError('invalid filename or file not found "{}"'
---> 63                              .format(filename))
     64 
     65         mainfile = get_py_file_if_possible(os.path.abspath(filename))

ValueError: invalid filename or file not found "None"

我不确定为什么会返回此错误。该文档没有说明在运行代码之前设置实验文件的任何内容。任何帮助将不胜感激!

【问题讨论】:

  • 尝试将此代码放入文件hello_world.py,如您链接页面上的示例所示
  • @donkopotamus - 是的,有效 - 所以不能从 IPython 笔记本上运行这样的东西?这是我运行它的地方。
  • 看起来我能够通过 IPython/Jupyter 运行它 - 我必须在 .py 中编写代码,因为 @donkopotamus 建议使用 %run 单元魔法命令。如果您想将您的评论移至回答,我很乐意接受。
  • 使用 iPython 中的 %run。它将模块加载到解释器中。
  • 供您参考,sacred 的开发人员正在考虑修改包,以便可以从 ipython 运行它。用户只需要知道他们将失去部分实验的可重复性,因为您无法确定笔记本中的代码是否按照实验中保存的文件的顺序运行。请继续关注 github 页面的神圣。

标签: python python-2.7 ipython python-sacred


【解决方案1】:

给定的回溯表明Experiment 的构造函数搜索其命名空间以找到定义它的文件。

因此,要使示例正常运行,请将示例代码放入一个文件并直接运行该文件。

如果您使用的是ipython,那么您总是可以尝试使用%%python 命令,它会在运行之前有效地将您提供的代码捕获到一个文件中(在一个单独的python 进程中)。

【讨论】:

    【解决方案2】:

    根据文档,如果您使用 IPython/Jupyter,则可以允许实验在不可重现的交互式环境中运行:

    ex = Experiment('jupyter_ex', interactive=True)
    

    https://sacred.readthedocs.io/en/latest/experiment.html#run-the-experiment

    【讨论】:

      【解决方案3】:

      The docs 说得好听(TL;DR:神圣为你检查这个,但失败是为了警告你)

      警告

      默认情况下,如果在交互式环境中运行神圣实验将失败 像 REPL 或 Jupyter Notebook 这样的环境。这是一个有意 安全措施,因为在这些环境中无法重现 保证。如果需要,可以通过以下方式停用此保护措施 interactive=True这样的实验:

      ex = Experiment('jupyter_ex', interactive=True)
      

      【讨论】:

      • 请注意,提问时这个功能和警告是不存在的;见here
      猜你喜欢
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 2017-06-19
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      相关资源
      最近更新 更多