将 Sacred Module 与 iPython 一起使用
Using Sacred Module with iPython
我正在尝试设置 sacred
for Python and I am going through the tutorial。我能够使用 pip install sacred
毫无问题地设置神圣。我遇到问题 运行 基本代码:
from sacred import Experiment
ex = Experiment("hello_world")
运行这段代码returns一个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"
我不确定为什么会返回此错误。该文档没有说明在 运行 代码之前设置实验文件。任何帮助将不胜感激!
给出的回溯表明 Experiment
的构造函数搜索其命名空间以查找其定义的文件。
因此,要使示例正常运行,请将示例代码放入一个文件中,然后直接 运行 该文件。
如果您正在使用 ipython
,那么您总是可以尝试使用 %%python
命令,这将在 运行 之前有效地将您提供的代码捕获到文件中(在单独的 python 进程中)。
根据文档,如果您处于 IPython/Jupyter,您可以允许实验在不可重现的交互环境中 运行:
ex = Experiment('jupyter_ex', interactive=True)
https://sacred.readthedocs.io/en/latest/experiment.html#run-the-experiment
The docs 说得好(TL;DR:sacred 会为您检查并失败以警告您)
Warning
By default, Sacred experiments will fail if run in an interactive
environment like a REPL or a Jupyter Notebook. This is an intended
security measure since in these environments reproducibility cannot be
ensured. If needed, this safeguard can be deactivated by passing
interactive=True
to the experiment like this:
ex = Experiment('jupyter_ex', interactive=True)
我正在尝试设置 sacred
for Python and I am going through the tutorial。我能够使用 pip install sacred
毫无问题地设置神圣。我遇到问题 运行 基本代码:
from sacred import Experiment
ex = Experiment("hello_world")
运行这段代码returns一个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"
我不确定为什么会返回此错误。该文档没有说明在 运行 代码之前设置实验文件。任何帮助将不胜感激!
给出的回溯表明 Experiment
的构造函数搜索其命名空间以查找其定义的文件。
因此,要使示例正常运行,请将示例代码放入一个文件中,然后直接 运行 该文件。
如果您正在使用 ipython
,那么您总是可以尝试使用 %%python
命令,这将在 运行 之前有效地将您提供的代码捕获到文件中(在单独的 python 进程中)。
根据文档,如果您处于 IPython/Jupyter,您可以允许实验在不可重现的交互环境中 运行:
ex = Experiment('jupyter_ex', interactive=True)
https://sacred.readthedocs.io/en/latest/experiment.html#run-the-experiment
The docs 说得好(TL;DR:sacred 会为您检查并失败以警告您)
Warning
By default, Sacred experiments will fail if run in an interactive environment like a REPL or a Jupyter Notebook. This is an intended security measure since in these environments reproducibility cannot be ensured. If needed, this safeguard can be deactivated by passing
interactive=True
to the experiment like this:ex = Experiment('jupyter_ex', interactive=True)