1

所以我一直在研究神圣的东西,它看起来很棒。不幸的是,我没有找到任何像我试图实现的多文件用例示例。

所以我有一个名为 configuration.py 的文件,它旨在包含不同的变量,这些变量将(使用神圣)插入到代码的其余部分(放置在不同的文件中):

from sacred import Experiment
ex = Experiment('Analysis')

@ex.config
def configure_analysis_default():
    """Initializes default  """
    generic_name = "C:\\basic_config.cfg" # configuration filename
    message = "This is my generic name: %s!" % generic_name
    print(message)

@ex.automain #automain function needs to be at the end of the file. Otherwise everything below it is not defined yet
#  when the experiment is run.
def my_main(message):
    print(message)

这本身就很好用。神圣按预期工作。但是,当我尝试引入第二个名为 Analysis.py 的文件时:

import configuration
from sacred import Experiment
ex = Experiment('Analysis')

@ex.capture
def what_is_love(generic_name):
    message = " I don't know"
    print(message)
    print(generic_name)

@ex.automain
def my_main1():
    what_is_love()

运行 Analysis.py 产生:

错误:

TypeError:what_is_love 缺少 ['generic_name'] 的值

我希望“导入配置”语句包含 configuration.py 文件,从而导入其中配置的所有内容,包括 configure_analysis_default() 及其装饰器 @ex.config,然后将其注入 what_is_love(generic_name)。我究竟做错了什么?我怎样才能解决这个问题?

欣赏它!

4

2 回答 2

3

看起来我们应该为这种事情使用成分。

http://sacred.readthedocs.io/en/latest/ingredients.html

但我还没有完全弄清楚。

我在设置中遇到了循环导入问题,所以我使用了一个单独的文件 exp.py,它只声明

from sacred import Experiment
ex = Experiment("default")

在我做的包中的每个文件中

from exp import ex

并且装饰器和配置变量传递似乎有效。我可以使用 --name 在命令行上更改实验的名称:

$> python main.py --name newname

于 2018-02-06T18:42:16.613 回答
1

所以,很愚蠢,但我会把它贴在这里,以支持任何有类似问题的人......

我的问题是我创建了一个不同的实验实例。我只需要从配置文件中导入我的实验。

替换这个:

import configuration
from sacred import Experiment
ex = Experiment('Analysis')

有了这个:

import configuration
ex = configuration.ex
于 2017-12-13T10:26:33.190 回答