例如,使用size_type
时,必须将实验配置中的所有变量传递到主函数中,例如
Sacred
如您所见,在此实验中,有两个变量ex = Experiment('iris_rbf_svm')
@ex.config
def cfg():
C = 1.0
gamma = 0.7
@ex.automain
def run(C, gamma):
iris = datasets.load_iris()
per = permutation(iris.target.size)
iris.data = iris.data[per]
iris.target = iris.target[per]
clf = svm.SVC(C, 'rbf', gamma=gamma)
clf.fit(iris.data[:90],
iris.target[:90])
return clf.score(iris.data[90:],
iris.target[90:])
和C
,并将它们传递到主函数中。
在实际场景中,有数十个实验变量,将它们全部传递到主函数中确实很混乱。 有没有办法将它们全部作为字典传递?还是作为具有属性的对象?
好的解决方案将导致如下结果:
gamma
答案 0 :(得分:1)
我能找到的最优雅的解决方案
τ1
答案 1 :(得分:1)
是的,您可以为此使用special value _config
值:
ex = Experiment('iris_rbf_svm')
@ex.config
def cfg():
C = 1.0
gamma = 0.7
@ex.automain
def run(_config):
C = _config['C']
gamma = _config['gamma']
答案 2 :(得分:0)
git存储库中有一个相关示例: https://github.com/IDSIA/sacred/blob/master/examples/08_less_magic.py
您可以直接指定字典(无需创建修饰功能)
from sacred import Experiment
from sacred.observers import FileStorageObserver
from sklearn import svm, datasets, model_selection
ex = Experiment("svm")
ex.add_config(
{ # Configuration is explicitly defined as dictionary.
"C": 1.0,
"gamma": 0.7,
"kernel": "rbf",
"seed": 42,
}
)
并按以下方式使用它:
def get_model(C, gamma, kernel):
return svm.SVC(C=C, kernel=kernel, gamma=gamma)
@ex.main # Using main, command-line arguments will not be interpreted in any special way.
def run(_config):
X, y = datasets.load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(
X, y, test_size=0.2
)
clf = get_model(
_config["C"], _config["gamma"], _config["kernel"]
) # Parameters are passed explicitly.
clf.fit(X_train, y_train)
return clf.score(X_test, y_test)
if __name__ == "__main__":
ex.run()