{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Sacred\n", "- 모든 실험은 신성하다\n", "- 모든 실험은 훌륭함\n", "- 만약 실험이 낭비되면 신이 매우 화를 낼 것\n", "\n", "- Sacred는 실험을 구성, 기록 복제하는 도구\n", " - 최소한의 오버 헤드를 도입해 모듈화, 실험 구성을 도움\n", "- 기능\n", " - 실험의 모든 파라미터 추적\n", " - 여러 설정에서 실험을 쉽게 할 수 있음\n", " - 파일이나 데이터베이스에 실행 설정을 저장\n", " - 결과 재현\n", " \n", "- 메인 메커니즘\n", " - Config Scope는 `@ex.config` 데코레이터 함수를 사용해 쉽게 설정 가능\n", " - dependency injection을 통해 캡쳐된 기능을 사용할 수 있음. 시스템이 파라미터를 전달해 쉽게 config value를 사용할 수 있음\n", " - Command line interface가 쉽게 파라미터를 바꿀 수 있게 함\n", " - 실험 및 구성에 대한 모든 정보를 기록해 실험을 추적\n", " - Automatic seed는 무작위성을 제어해 재현성을 유지할 때 도움이 됨\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "from numpy.random import permutation\n", "from sklearn import svm, datasets\n", "from sacred import Experiment\n", "ex = Experiment('iris_rbf_svm', interactive=True)\n", "# jupyter notebook일 경우 interactive=True, python 스크립트라면 없어도 됨\n", "\n", "@ex.config\n", "def cfg():\n", " C = 1.0\n", " gamma = 0.7\n", "\n", "# ex.automain은 python 스크립트일 때 사용\n", "@ex.main\n", "def run(C, gamma):\n", " iris = datasets.load_iris()\n", " per = permutation(iris.target.size)\n", " iris.data = iris.data[per]\n", " iris.target = iris.target[per]\n", " clf = svm.SVC(C, 'rbf', gamma=gamma)\n", " clf.fit(iris.data[:90],\n", " iris.target[:90])\n", " return clf.score(iris.data[90:],\n", " iris.target[90:])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING - iris_rbf_svm - No observers have been added to this run\n", "INFO - iris_rbf_svm - Running command 'run'\n", "INFO - iris_rbf_svm - Started\n", "INFO - iris_rbf_svm - Result: 0.9833333333333333\n", "INFO - iris_rbf_svm - Completed after 0:00:00\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex.run()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "from sacred import Experiment\n", "ex = Experiment('my_experiment', interactive=True)\n", "\n", "@ex.config\n", "def my_config():\n", " foo = 42\n", " bar = 'baz'\n", "\n", "@ex.capture\n", "def some_function(a, foo, bar=10):\n", " print(a, foo, bar)\n", "\n", "@ex.main\n", "def my_main():\n", " some_function(1, 2, 3) # 1 2 3\n", " some_function(1) # 1 42 'baz'\n", " some_function(1, bar=12) # 1 42 12\n", " some_function() # TypeError: missing value for 'a'" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING - my_experiment - No observers have been added to this run\n", "INFO - my_experiment - Running command 'my_main'\n", "INFO - my_experiment - Started\n", "ERROR - my_experiment - Failed after 0:00:00!\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1 2 3\n", "1 42 baz\n", "1 42 12\n" ] }, { "ename": "TypeError", "evalue": "some_function is missing value(s) for ['a']", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/local/lib/python3.6/site-packages/sacred/experiment.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, command_name, config_updates, named_configs, meta_info, options)\u001b[0m\n\u001b[1;32m 207\u001b[0m run = self._create_run(command_name, config_updates, named_configs,\n\u001b[1;32m 208\u001b[0m meta_info, options)\n\u001b[0;32m--> 209\u001b[0;31m \u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 210\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mrun\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 211\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.6/site-packages/sacred/run.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 219\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_start_heartbeat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 220\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_execute_pre_run_hooks\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 221\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmain_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 222\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_execute_post_run_hooks\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 223\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.6/site-packages/sacred/config/captured_function.py\u001b[0m in \u001b[0;36mcaptured_function\u001b[0;34m(wrapped, instance, args, kwargs)\u001b[0m\n\u001b[1;32m 44\u001b[0m \u001b[0mstart_time\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;31m# =================== run actual function =================================\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 46\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwrapped\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 47\u001b[0m \u001b[0;31m# =========================================================================\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mwrapped\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mmy_main\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0msome_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 1 42 'baz'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0msome_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbar\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m12\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 1 42 12\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0msome_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# TypeError: missing value for 'a'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/local/lib/python3.6/site-packages/sacred/config/captured_function.py\u001b[0m in \u001b[0;36mcaptured_function\u001b[0;34m(wrapped, instance, args, kwargs)\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0mbound\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0minstance\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 40\u001b[0m args, kwargs = wrapped.signature.construct_arguments(args, kwargs, options,\n\u001b[0;32m---> 41\u001b[0;31m bound)\n\u001b[0m\u001b[1;32m 42\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mwrapped\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[0mwrapped\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Started\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.6/site-packages/sacred/config/signature.py\u001b[0m in \u001b[0;36mconstruct_arguments\u001b[0;34m(self, args, kwargs, options, bound)\u001b[0m\n\u001b[1;32m 100\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fill_in_options\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moptions\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbound\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 101\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 102\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_assert_no_missing_args\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbound\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 103\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.6/site-packages/sacred/config/signature.py\u001b[0m in \u001b[0;36m_assert_no_missing_args\u001b[0;34m(self, args, kwargs, bound)\u001b[0m\n\u001b[1;32m 158\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmissing_args\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 159\u001b[0m raise TypeError(\"{} is missing value(s) for {}\".format(\n\u001b[0;32m--> 160\u001b[0;31m self.name, missing_args))\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: some_function is missing value(s) for ['a']" ] } ], "source": [ "ex.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Observing an Experiment\n", "- The main one is the Mongo Observer which stores all information in a MongoDB.\n", "- The File Storage Observer stores the run information as files in a given directory and will therefore only work locally.\n", "- The TinyDB Observer provides another local way of observing experiments by using tinydb to store run information in a JSON file.\n", "- The SQL Observer connects to any SQL database and will store the relevant information there." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "from sacred.observers import MongoObserver\n", "\n", "ex.observers.append(MongoObserver.create())\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from pymongo import MongoClient" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "import urllib.parse" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 몽고DB 설치\n", "- [홈페이지](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "username = urllib.parse.quote_plus('user')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "password = urllib.parse.quote_plus('password')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "from sacred.observers import MongoObserver\n", "\n", "ex.observers.append(MongoObserver.create(\n", " url='mongodb://user:password@example.com/the_database?authMechanism=SCRAM-SHA-1',\n", " db_name='MY_DB'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filt Storage Observer" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "from sacred.observers import FileStorageObserver\n", "\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "ex = Experiment('iris_rbf_svm', interactive=True)\n", "# jupyter notebook일 경우 interactive=True, python 스크립트라면 없어도 됨\n", "\n", "@ex.config\n", "def cfg():\n", " C = 1.0\n", " gamma = 0.7\n", "\n", "# ex.automain은 python 스크립트일 때 사용\n", "@ex.main\n", "def run(C, gamma):\n", " iris = datasets.load_iris()\n", " per = permutation(iris.target.size)\n", " iris.data = iris.data[per]\n", " iris.target = iris.target[per]\n", " clf = svm.SVC(C, 'rbf', gamma=gamma)\n", " clf.fit(iris.data[:90],\n", " iris.target[:90])\n", " return clf.score(iris.data[90:],\n", " iris.target[90:])\n", "\n", "ex.observers.append(FileStorageObserver.create('my_runs'))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - iris_rbf_svm - Running command 'run'\n", "INFO - iris_rbf_svm - Started run with ID \"1\"\n", "INFO - iris_rbf_svm - Result: 0.95\n", "INFO - iris_rbf_svm - Completed after 0:00:00\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slack Observer\n", "- [링크](https://sacred.readthedocs.io/en/latest/observers.html#slack-observer)\n", "![image](https://sacred.readthedocs.io/en/latest/_images/slack_observer.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 실험과 logging을 합치고 싶은 경우\n", "- `_log` 사용" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "@ex.capture\n", "def some_function(_log):\n", " _log.warning('My warning message!')" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "#!/usr/bin/env python\n", "# coding=utf-8\n", "\"\"\" An example showcasing the logging system of Sacred.\"\"\"\n", "from __future__ import division, print_function, unicode_literals\n", "import logging\n", "from sacred import Experiment\n", "\n", "ex = Experiment('log_example', interactive=True)\n", "\n", "# set up a custom logger\n", "logger = logging.getLogger('mylogger')\n", "logger.handlers = []\n", "ch = logging.StreamHandler()\n", "formatter = logging.Formatter('[%(levelname).1s] %(name)s >> \"%(message)s\"')\n", "ch.setFormatter(formatter)\n", "logger.addHandler(ch)\n", "logger.setLevel('INFO')\n", "\n", "# attach it to the experiment\n", "ex.logger = logger\n", "\n", "\n", "@ex.config\n", "def cfg():\n", " number = 2\n", " got_gizmo = False\n", "\n", "\n", "@ex.capture\n", "def transmogrify(got_gizmo, number, _log):\n", " if got_gizmo:\n", " _log.debug(\"Got gizmo. Performing transmogrification...\")\n", " return number * 42\n", " else:\n", " _log.warning(\"No gizmo. Can't transmogrify!\")\n", " return 0\n", "\n", "\n", "@ex.main\n", "def main(number, _log):\n", " _log.info('Attempting to transmogrify %d...', number)\n", " result = transmogrify()\n", " _log.info('Transmogrification complete: %d', result)\n", " return result" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[W] mylogger.log_example >> \"No observers have been added to this run\"\n", "WARNING - mylogger.log_example - No observers have been added to this run\n", "[I] mylogger.log_example >> \"Running command 'main'\"\n", "INFO - mylogger.log_example - Running command 'main'\n", "[I] mylogger.log_example >> \"Started\"\n", "INFO - mylogger.log_example - Started\n", "[I] mylogger.main >> \"Attempting to transmogrify 2...\"\n", "INFO - mylogger.main - Attempting to transmogrify 2...\n", "[W] mylogger.transmogrify >> \"No gizmo. Can't transmogrify!\"\n", "WARNING - mylogger.transmogrify - No gizmo. Can't transmogrify!\n", "[I] mylogger.main >> \"Transmogrification complete: 0\"\n", "INFO - mylogger.main - Transmogrification complete: 0\n", "[I] mylogger.log_example >> \"Result: 0\"\n", "INFO - mylogger.log_example - Result: 0\n", "[I] mylogger.log_example >> \"Completed after 0:00:00\"\n", "INFO - mylogger.log_example - Completed after 0:00:00\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex.run()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "from __future__ import division, print_function, unicode_literals\n", "from sacred import Experiment\n", "\n", "ex = Experiment('hello_config', interactive=True)\n", "\n", "\n", "@ex.named_config\n", "def rude():\n", " \"\"\"A rude named config\"\"\"\n", " recipient = \"bastard\"\n", " message = \"Fuck off you {}!\".format(recipient)\n", "\n", "\n", "@ex.config\n", "def cfg():\n", " recipient = \"world\"\n", " message = \"Hello {}!\".format(recipient)\n", "\n", "\n", "@ex.main\n", "def main(message):\n", " print(__name__)\n", " print(message)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING - hello_config - No observers have been added to this run\n", "INFO - hello_config - Running command 'main'\n", "INFO - hello_config - Started\n", "INFO - hello_config - Completed after 0:00:00\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "__main__\n", "Hello world!\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import division, print_function, unicode_literals\n", "from sacred import Experiment, Ingredient\n", "\n", "# ============== Ingredient 0: settings =================\n", "s = Ingredient(\"settings\")\n", "\n", "\n", "@s.config\n", "def cfg1():\n", " verbose = True\n", "\n", "\n", "# ============== Ingredient 1: dataset.paths =================\n", "data_paths = Ingredient(\"dataset.paths\", ingredients=[s])\n", "\n", "\n", "@data_paths.config\n", "def cfg2(settings):\n", " v = not settings['verbose']\n", " base = '/home/sacred/'\n", "\n", "\n", "# ============== Ingredient 2: dataset =======================\n", "data = Ingredient(\"dataset\", ingredients=[data_paths, s])\n", "\n", "\n", "@data.config\n", "def cfg3(paths):\n", " basepath = paths['base'] + 'datasets/'\n", " filename = \"foo.hdf5\"\n", "\n", "\n", "@data.capture\n", "def foo(basepath, filename, paths, settings):\n", " print(paths)\n", " print(settings)\n", " return basepath + filename\n", "\n", "\n", "# ============== Experiment ==============================\n", "ex = Experiment('modular_example', ingredients=[data, data_paths])\n", "\n", "\n", "@ex.config\n", "def cfg(dataset):\n", " a = 10\n", " b = 17\n", " c = a + b\n", " out_base = dataset['paths']['base'] + 'outputs/'\n", " out_filename = dataset['filename'].replace('.hdf5', '.out')\n", "\n", "\n", "@ex.automain\n", "def main(a, b, c, out_base, out_filename, dataset):\n", " print('a =', a)\n", " print('b =', b)\n", " print('c =', c)\n", " print('out_base =', out_base, out_filename)\n", " # print(\"dataset\", dataset)\n", " # print(\"dataset.paths\", dataset['paths'])\n", " print(\"foo()\", foo())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }