aitech-ium/IUM_07.Sacred.ipynb

47 KiB
Raw Permalink Blame History

Logo 1

Inżynieria uczenia maszynowego

7. Sacred [laboratoria]

Tomasz Ziętkiewicz (2023)

Logo 2

Sacred

Every experiment is sacred
Every experiment is great
If an experiment is wasted
God gets quite irate

https://github.com/IDSIA/sacred / Sens życia według Monty Pythona

  • Przeprowadzanie eksperymentów (zmiana parametrów, trenowanie, ewaluacja) uczenia maszynowego jest kosztowne i czasochłonne
  • Dlatego warto przeprowadzać je w zorganizowany sposób
  • I tak, żebyśmy mogli powtórzyć / odtworzyć raz uzyskane wyniki

Sacred is a tool to help you:

  • configure
  • organize
  • log
  • reproduce experiments.

It is designed to do all the tedious overhead work that you need to do around your actual experiment in order to:

  • keep track of all the parameters of your experiment
  • easily run your experiment for different settings
  • save configurations for individual runs in a database
  • reproduce your results

https://github.com/IDSIA/sacred

  • ConfigScopes A very convenient way of the local variables in a function to define the parameters your experiment uses.
  • Config Injection You can access all parameters of your configuration from every function. They are automatically injected by name.
  • Command-line interface You get a powerful command-line interface for each experiment that you can use to change parameters and run different variants.
  • Observers Sacred provides Observers that log all kinds of information about your experiment, its dependencies, the configuration you used, the machine it is run on, and of course the result. These can be saved to a MongoDB, for easy access later.
  • Automatic seeding helps controlling the randomness in your experiments, such that the results remain reproducible.

Instalacja

!pip3 install sacred
Requirement already satisfied: sacred in /home/tomek/miniconda3/lib/python3.9/site-packages (0.8.4)
Requirement already satisfied: jsonpickle>=1.2 in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (3.0.1)
Requirement already satisfied: colorama>=0.4 in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (0.4.6)
Requirement already satisfied: GitPython in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (3.1.31)
Requirement already satisfied: py-cpuinfo>=4.0 in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (9.0.0)
Requirement already satisfied: wrapt<2.0,>=1.0 in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (1.15.0)
Requirement already satisfied: munch<3.0,>=2.5 in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (2.5.0)
Requirement already satisfied: docopt<1.0,>=0.3 in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (0.6.2)
Requirement already satisfied: packaging>=18.0 in /home/tomek/miniconda3/lib/python3.9/site-packages (from sacred) (23.0)
Requirement already satisfied: six in /home/tomek/miniconda3/lib/python3.9/site-packages (from munch<3.0,>=2.5->sacred) (1.16.0)
Requirement already satisfied: gitdb<5,>=4.0.1 in /home/tomek/miniconda3/lib/python3.9/site-packages (from GitPython->sacred) (4.0.10)
Requirement already satisfied: smmap<6,>=3.0.1 in /home/tomek/miniconda3/lib/python3.9/site-packages (from gitdb<5,>=4.0.1->GitPython->sacred) (5.0.0)

Funkcja main

%%writefile sacred_hello.py
from sacred import Experiment

ex = Experiment()

@ex.automain
def my_main():
    print('Witaj świecie!')
Overwriting sacred_hello.py
!python3 IUM_07/sacred_hello.py
WARNING - sacred_hello - No observers have been added to this run
INFO - sacred_hello - Running command 'my_main'
INFO - sacred_hello - Started
Witaj świecie!
INFO - sacred_hello - Completed after 0:00:00
Co się dzieje w kodzie powyżej?
  1. Tworzymy obiekt klasy Experiment
  2. Dekorujemy funkcję "my_main" dekoratorem automain Dzięki temu:
    • otrzymujemy interfejs CLI, m.in. do kontrolowania poziomu logowania, przekazywania parametrów itp.
    • oznaczamy funkcję "my_main" jako główną funkcję, która będzie wywoływana podczas wykonywania eksperymentu
    • funkcja oznaczona jako główna musi być ostatnią funkcją zdefiniowaną w pliku!
Co nam daje interejs CLI:
!python3 IUM_07/sacred_hello.py -h
Usage:
  sacred_hello.py [(with UPDATE...)] [options]
  sacred_hello.py help [COMMAND]
  sacred_hello.py (-h | --help)
  sacred_hello.py COMMAND [(with UPDATE...)] [options]



Options:
  -b VALUE --beat-interval=VALUE  Set the heart-beat interval for this run.  Time
                                between two heartbeat events is measured in
                                seconds.
  -C VALUE --capture=VALUE      Control the way stdout and stderr are captured.
                                The argument value must be one of [no, sys, fd]
  -c VALUE --comment=VALUE      Add a comment to this run.
  -d --debug                    Set this run to debug mode.  Suppress warnings
                                about missing observers and don't filter the
                                stacktrace. Also enables usage with ipython
                                `--pdb`.
  -e --enforce_clean            Fail if any version control repository is
                                dirty.
  -F VALUE --file_storage=VALUE  Add a file-storage observer to the experiment.
                                The value of the arguement should be the base-
                                directory to write the runs to
  -f --force                    Disable warnings about suspicious changes for
                                this run.
  -h --help                     Print this help message and exit.
  -i VALUE --id=VALUE           Set the id for this run.
  -l VALUE --loglevel=VALUE     Set the LogLevel.  Loglevel either as 0 - 50 or
                                as string: DEBUG(10), INFO(20), WARNING(30),
                                ERROR(40), CRITICAL(50)
  -m VALUE --mongo_db=VALUE     Add a MongoDB Observer to the experiment.  The
                                argument value is the database specification.
                                Should be in the form:  `[host:port:]db_name[.c
                                ollection[:id]][!priority]`
  -n VALUE --name=VALUE         Set the name for this run.
  -D --pdb                      Automatically enter post-mortem debugging with
                                pdb on failure.
  -p --print-config             Always print the configuration first.
  -P VALUE --priority=VALUE     Sets the priority for a queued up experiment.
                                `--priority=NUMBER` The number represent the
                                priority for this run.
  -q --queue                    Only queue this run, do not start it.
  -S VALUE --s3=VALUE           Add a S3 File observer to the experiment.  The
                                argument value should be
                                `s3://<bucket>/path/to/exp`.
  -s VALUE --sql=VALUE          Add a SQL Observer to the experiment.  The
                                typical form is:
                                dialect://username:password@host:port/database
  -t VALUE --tiny_db=VALUE      Add a TinyDB Observer to the experiment.  The
                                argument is the path to be given to the
                                TinyDbObserver.
  -u --unobserved               Ignore all observers for this run.


Arguments:
  COMMAND   Name of command to run (see below for list of commands)
  UPDATE    Configuration assignments of the form foo.bar=17


Commands:
  print_config         Print the updated configuration and exit.
  print_dependencies   Print the detected source-files and dependencies.
  save_config          Store the updated configuration in a file.
  print_named_configs  Print the available named configs and exit.
  my_main              

Konfiguracje

  • Konfiguracje pozwalają nam sparametryzować wywołania eksperymentu.
  • Ułatwiają przekazywanie parametrów - zmienne z konfiguracji są wstrzykiwane do funkcji wywoływanych
  • Mogą być automatycznie zapisywane (dzięki czemu możemy śledzić jak zmieniały się parametry i jaki miały wpływ na wyniki)
  • Konfigurację można stworzyć w jeden z 3 sposobów:
    • używając config scopes (z dekoratorem @config)
    • jako słownik
    • wczytując ją z pliku

Konfiguracje - config scopes

Jeśli oznaczymy jakąś funkcję dekoratorem @config, to zostanie ona uruchoniona przed wywołaniem eksperymentu i wszystkie jej lokalne zmienne, które da się zserializować jako json, zostaną dodane do konfiguracji. Potem ich wartości zostaną wstrzyknięte do innych funkcji wywoływanych w eksperymencie.

from sacred import Experiment

exint = Experiment("sacred_scopes", interactive=True) #Jeśli wykonujemy interaktywnie (w konsoli Pythona albo w Jupyter):
# - musimy podać nazwę eksperymentu (domyślnie jako nazwa używana jest nazwa pliku źródłowego)
# - musimy dodać parametr "interactive=True"
# - zamiast dekoratora "@ex.automain" używamy "@ex.main"

@exint.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"
    message = "{0} {1}!".format(greeting, recipient)


@exint.main
def my_main(message):
    print(message)
exint.run()
WARNING - sacred_scopes - No observers have been added to this run
INFO - sacred_scopes - Running command 'my_main'
INFO - sacred_scopes - Started
INFO - sacred_scopes - Completed after 0:00:00
Witaj Świecie!
<sacred.run.Run at 0x7f89cc3175b0>
Możemy podejrzeć wartości zmiennych w konfiguracji:
my_config()
{'recipient': 'Świecie', 'greeting': 'Witaj', 'message': 'Witaj Świecie!'}

Parametry możemy podejrzeć i modyfikować z poziomu CLI

  • wartości podane w CLI nadpiszą te podane w kodzie
# %load IUM_07/sacred_scopes.py
from sacred import Experiment

ex = Experiment()

@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"
    message = "{0} {1}!".format(greeting, recipient)

@ex.automain
def my_main(message):
    print(message)
!python3 IUM_07/sacred_scopes.py with 'recipient=Przygodo'
WARNING - sacred_scopes - No observers have been added to this run
INFO - sacred_scopes - Running command 'my_main'
INFO - sacred_scopes - Started
Witaj Przygodo!
INFO - sacred_scopes - Completed after 0:00:00
!python3 IUM_07/sacred_scopes.py print_config
INFO - sacred_scopes - Running command 'print_config'
INFO - sacred_scopes - Started
Configuration (modified, added, typechanged, doc):
  greeting = 'Witaj'
  message = 'Witaj Świecie!'
  recipient = 'Świecie'
  seed = 269258424                   # the random seed for this experiment
INFO - sacred_scopes - Completed after 0:00:00
!python IUM_07/sacred_scopes.py print_config with 'recipient=Przygodo'
INFO - sacred_scopes - Running command 'print_config'
INFO - sacred_scopes - Started
Configuration (modified, added, typechanged, doc):
  greeting = 'Witaj'
  message = 'Witaj Przygodo!'
  recipient = 'Przygodo'
  seed = 667939214                   # the random seed for this experiment
INFO - sacred_scopes - Completed after 0:00:00

Wczytywanie konfiguracji z pliku

# %load IUM_07/config.json
{
    "recipient": "samotności",
    "greeting": "Żegnaj"
}
from sacred import Experiment

ex = Experiment("sacred_scopes", interactive=True) #Jeśli wykonujemy interaktywnie (w konsoli Pythona albo w Jupyter):
# - musimy podać nazwę eksperymentu (domyślnie jako nazwa używana jest nazwa pliku źródłowego)
# - musimy dodać parametr "interactive=True"
# - zamiast "automain" używamy parametru "main"

@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"

ex.add_config("IUM_07/config.json")


@ex.main
def my_main(recipient, greeting):
    print("{0} {1}!".format(greeting, recipient))
r = ex.run()
WARNING - sacred_scopes - No observers have been added to this run
INFO - sacred_scopes - Running command 'my_main'
INFO - sacred_scopes - Started
INFO - sacred_scopes - Completed after 0:00:00
Żegnaj samotności!
r.config
{'recipient': 'samotności', 'greeting': 'Żegnaj', 'seed': 877272352}

Możemy modyfikować części konfiguracji bezpośrednio przed wywołaniem

r = ex.run(config_updates={"recipient":"nudo"})
WARNING - sacred_scopes - No observers have been added to this run
INFO - sacred_scopes - Running command 'my_main'
INFO - sacred_scopes - Started
INFO - sacred_scopes - Completed after 0:00:00
Żegnaj nudo!

Wstrzykiwanie zależności

  • Oprócz funkcji głównej, wartości z konfiguracji są też wstrzykiwane do funkcji udekorowanych dekoratorem @ex.capture
  • Możemy korzystać w nich ze specjalnych parametrów, np.:
    • _log - daje nam dostęp do obiektu logera (więcej: logowanie)
    • _run - daje dostęp do obiektu reprezentującego aktualne wywołanie eksperymentu (przykład później)
from sacred import Experiment

ex = Experiment("sacred_scopes", interactive=True)

@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"

@ex.capture
def prepare_message(recipient, greeting, _log):
    _log.info("Enterred prepare_message")
    return "{0} {1}!".format(greeting, recipient)

@ex.main
def my_main():
    print(prepare_message()) ## Nie musimy przekazywać wartości
    
ex.run()
WARNING - sacred_scopes - No observers have been added to this run
INFO - sacred_scopes - Running command 'my_main'
INFO - sacred_scopes - Started
INFO - prepare_message - Enterred prepare_message
INFO - sacred_scopes - Completed after 0:00:00
Witaj Świecie!
<sacred.run.Run at 0x7f89a0db5550>

Obserwowanie eksperymentów

Sacred zapisuje szereg informacji na temat każdego eksperymentu:

  • czas wykonania
  • konfigurację
  • tekst zwrócony na stdout/stderr
  • błędy, jeśli wystąpiły
  • podstawowe informacje o środowisku (maszynie), na której przeprowadzono eksperyment
  • użyte pliki źródłowe
  • użyte zależności i ich wersje
  • pliki otwarte za pomocą ex.open_resource() albo ex.add_resource()
  • pliki dodane za pomocą ex.add_artifact()
!ls -l my_runs
total 20
drwsrwsr-t 2 tomek tomek 4096 May 28  2022 1
drwsrwsr-t 2 tomek tomek 4096 May 28  2022 2
drwxr-sr-x 2 tomek tomek 4096 Apr 12 15:11 3
drwxr-sr-x 2 tomek tomek 4096 Apr 12 15:11 _resources
drwsrwsr-t 2 tomek tomek 4096 May 28  2022 _sources

Obserwowane infromacje mogą zostać zapisane za pomocą jednego z obserwatorów:

  • Mongo Observer - zapisuje dane w MongoDB
  • File Storage Observer - zapisuje dane lokalnie w pliku
  • TinyDB Observer - korzysta z lokalnej bazy zapisanej w pliku JSON
  • SQL Observer - przechowuje informacje w bazie SQL
  • S3 Observer - korzysta z AWS S3
  • gcs_observer - korzysta z Google Cloud Storage
  • Queue Observer - rodzaj lokalnego bufora nakładanego na jeden z powyższych
  • Slack Observer - używany do powiadomień wysyłanych na komunikator Slack
  • Telegram Observer - używany do powiadomień wysyłanych na komunikator Telegram

File storage observer

  • zapisuje informacje o eksperymencie w lokalnych plikach
  • można go dodać tak: ex.observers.append(FileStorageObserver('my_runs_directory')), gdzie my_runs_directory to ścieżka, gdzie będą zapisywane informacje o eksperymentach
%%writefile IUM_07/file_observer.py
from sacred.observers import FileStorageObserver
from sacred import Experiment

ex = Experiment("file_observer")

ex.observers.append(FileStorageObserver('my_runs'))

@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"

@ex.capture
def prepare_message(recipient, greeting):
    return "{0} {1}!".format(greeting, recipient)

@ex.automain
def my_main(recipient, greeting):
    print(prepare_message()) ## Nie musimy przekazywać wartości
!python3 IUM_07/file_observer.py
INFO - file_observer - Running command 'my_main'
INFO - file_observer - Started run with ID "4"
Witaj Świecie!
INFO - file_observer - Completed after 0:00:00

Zobaczmy jakie informacje zostały zapisane

!ls -l my_runs
total 24
drwsrwsr-t 2 tomek tomek 4096 May 28  2022 1
drwsrwsr-t 2 tomek tomek 4096 May 28  2022 2
drwxr-sr-x 2 tomek tomek 4096 Apr 12 15:11 3
drwxr-sr-x 2 tomek tomek 4096 Apr 20 12:09 4
drwxr-sr-x 2 tomek tomek 4096 Apr 12 15:11 _resources
drwsrwsr-t 2 tomek tomek 4096 May 28  2022 _sources
!ls -l my_runs/1
total 16
-rw-r--r-- 1 tomek tomek   77 May 28  2022 config.json
-rw-r--r-- 1 tomek tomek  159 May 28  2022 cout.txt
-rw-r--r-- 1 tomek tomek    2 May 28  2022 metrics.json
-rw-r--r-- 1 tomek tomek 1659 May 28  2022 run.json
# %load my_runs/1/config.json
{
  "greeting": "Witaj",
  "recipient": "\u015awiecie",
  "seed": 805857632
}
!cat my_runs/1/cout.txt
INFO - file_observer - Running command 'my_main'
INFO - file_observer - Started run with ID "1"
Witaj Świecie!
INFO - file_observer - Completed after 0:00:00
# %load my_runs/1/run.json
{
  "artifacts": [],
  "command": "my_main",
  "experiment": {
    "base_dir": "/home/tomek/repos/aitech/aitech-ium/IUM_07",
    "dependencies": [
      "sacred==0.8.2"
    ],
    "mainfile": "file_observer.py",
    "name": "file_observer",
    "repositories": [
      {
        "commit": "3055a4f1c2ef06ea1c29e3d41d862827cede7e2a",
        "dirty": true,
        "url": "git@git.wmi.amu.edu.pl:tzietkiewicz/aitech-ium.git"
      }
    ],
    "sources": [
      [
        "file_observer.py",
        "_sources/file_observer_cd34a0ef4a32fb0a966eaa01ea6371ad.py"
      ]
    ]
  },
  "heartbeat": "2022-04-25T07:51:37.853633",
  "host": {
    "ENV": {},
    "cpu": "Intel(R) Core(TM) i5-4200H CPU @ 2.80GHz",
    "hostname": "ASUSEK",
    "os": [
      "Linux",
      "Linux-4.4.0-19041-Microsoft-x86_64-with-Ubuntu-18.04-bionic"
    ],
    "python_version": "3.6.9"
  },
  "meta": {
    "command": "my_main",
    "options": {
      "--beat-interval": null,
      "--capture": null,
      "--comment": null,
      "--debug": false,
      "--enforce_clean": false,
      "--file_storage": null,
      "--force": false,
      "--help": false,
      "--loglevel": null,
      "--mongo_db": null,
      "--name": null,
      "--pdb": false,
      "--print-config": false,
      "--priority": null,
      "--queue": false,
      "--s3": null,
      "--sql": null,
      "--tiny_db": null,
      "--unobserved": false,
      "COMMAND": null,
      "UPDATE": [],
      "help": false,
      "with": false
    }
  },
  "resources": [],
  "result": null,
  "start_time": "2022-04-25T07:51:37.831461",
  "status": "COMPLETED",
  "stop_time": "2022-04-25T07:51:37.849334"
}
! ls -l my_runs/_sources
## W run.json możemy znaleźć ścieżkę do pliku z źródłami:         "_sources/file_observer_bb0a5c4720d1072b641d23da080696b6.py"
total 4
-rw-r--r-- 1 tomek tomek 464 May 28  2022 file_observer_cd34a0ef4a32fb0a966eaa01ea6371ad.py
# %load my_runs/_sources/file_observer_cd34a0ef4a32fb0a966eaa01ea6371ad.py
from sacred.observers import FileStorageObserver
from sacred import Experiment

ex = Experiment("file_observer")

ex.observers.append(FileStorageObserver('my_runs'))

@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"

@ex.capture
def prepare_message(recipient, greeting):
    return "{0} {1}!".format(greeting, recipient)

@ex.automain
def my_main(recipient, greeting):
    print(prepare_message()) ## Nie musimy przekazywać wartości

Dodawanie własnych informacji

from sacred.observers import FileStorageObserver
from sacred import Experiment
from datetime import datetime

ex = Experiment("file_observer", interactive=True)

ex.observers.append(FileStorageObserver('my_runs'))

@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"

### - Do "przechwyconej" przez @ex.capture funkcji prepare_message dodaliśmy specjalny parametr _run
### - Daje on dostęp do obiektu wywołania eksperymentu w trakcie jego wywołania
### - umożliwia m.in. zapisywanie dodatkowych informacji w słowniku info
@ex.capture
def prepare_message(recipient, greeting, _run):
    _run.info["prepare_message_ts"] = str(datetime.now())
    return "{0} {1}!".format(greeting, recipient)

@ex.main
def my_main(recipient, greeting):
    print(prepare_message()) ## Nie musimy przekazywać wartości
    
r = ex.run()
INFO - file_observer - Running command 'my_main'
INFO - file_observer - Started run with ID "5"
INFO - file_observer - Completed after 0:00:00
Witaj Świecie!
cat my_runs/5/info.json
{
  "prepare_message_ts": "2023-04-20 12:10:28.197315"
}

Artefakty

  • Artefakty służą do zapisywania plików, np. z wytrenowanym modelem
  • Plik można zapisać jako artefakt korzystając z : ex.add_artifact()
    ex.add_artifact("model.pb")
    

Otwieranie zasobów

from sacred import Experiment
from sacred.observers import FileStorageObserver


ex = Experiment("resources", interactive=True)
ex.observers.append(FileStorageObserver('my_runs'))

@ex.main
def my_main():
    f = ex.open_resource("Iris.csv", "r")
    print(f.readline())
    
ex.run()
INFO - resources - Running command 'my_main'
INFO - resources - Started run with ID "6"
INFO - resources - Completed after 0:00:00
Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species

<sacred.run.Run at 0x7f89a0db5a30>
!ls -l my_runs/_resources
total 8
-rw-r--r-- 1 tomek tomek 5107 Apr 12 15:11 Iris_717820ef0af287ff346c5cabfb4c612c.csv
!grep -e "resources" -R my_runs/6
my_runs/6/run.json:    "name": "resources",
my_runs/6/run.json:  "resources": [
my_runs/6/run.json:      "my_runs/_resources/Iris_717820ef0af287ff346c5cabfb4c612c.csv"

Obserwator mongo

  • Żeby skorzystać z obserwatora Mongo, musimy mieć dostęp do bazy Mongo.
  • Można ją łatwo "postawić" za pomocą docker-compose .
  • W tym celu wystarczy skopiować katalog examples/docker z repozytorium SACRED i uruchomić docker-compose up - dostaniemy uruchomioną bazę MongoDB i dodatkowo Omniboard . Więcej informacji w dokumentacji
  • Baza taka została już postawiona na serwerze Jenkins, więc pracując na Jenkinsie można skorzystać z lokalnej bazy (localhost:27017)
!pip3 install pymongo
Requirement already satisfied: pymongo in /home/tomek/miniconda3/lib/python3.9/site-packages (4.3.3)
Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /home/tomek/miniconda3/lib/python3.9/site-packages (from pymongo) (2.3.0)
from sacred.observers import MongoObserver
from sacred import Experiment

ex = Experiment("sacred_scopes", interactive=True)
ex.observers.append(MongoObserver(url='mongodb://admin:IUM_2021@172.17.0.1:27017',
                                  db_name='sacred'))  # Tutaj podajemy dane uwierzytelniające i nazwę bazy skonfigurowane w pliku .env podczas uruchamiania bazy.
# W przypadku instancji na Jenkinsie url będzie wyglądał następująco: mongodb://admin:IUM_2021@172.17.0.1:27017
@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"

@ex.capture
def prepare_message(recipient, greeting):
    return "{0} {1}!".format(greeting, recipient)

@ex.main
def my_main(recipient, greeting):
    print(prepare_message()) ## Nie musimy przekazywać wartości
    
ex.run()
INFO - sacred_scopes - Running command 'my_main'
ERROR - sacred_scopes - Failed after 0:00:30!

Metryki

  • W trakcie eksperymentu możemy śledzić metryki, np. aktualny loss
  • W tym celu wystarczy:
    • dodać do funkcji udekorowanej @ex.main albo @ex.capure parametr _run
    • potem wywołać np. _run.log_scalar()
from sacred.observers import MongoObserver
from sacred import Experiment
import random
import time

ex = Experiment("sacred_scopes", interactive=True)
ex.observers.append(MongoObserver(url='mongodb://admin:IUM_2021@172.17.0.1:27017',
                                  db_name='sacred'))  # Tutaj podajemy dane uwierzytelniające i nazwę bazy skonfigurowane w pliku .env podczas uruchamiania bazy.
# W przypadku instancji na Jenkinsie url będzie wyglądał następująco: mongodb://admin:IUM_2021@172.17.0.1:27017
@ex.config
def my_config():
    recipient = "Świecie"
    greeting = "Witaj"

@ex.capture
def prepare_message(recipient, greeting):
    return "{0} {1}!".format(greeting, recipient)

@ex.main
def my_main(recipient, greeting, _run):
    print(prepare_message()) ## Nie musimy przekazywać wartości  
    counter = 0
    while counter < 20:
        counter+=1
        value = counter
        ms_to_wait = random.randint(5, 5000)
        time.sleep(ms_to_wait/1000)
        noise = 1.0 + 0.1 * (random.randint(0, 10) - 5)
        # This will add an entry for training.loss metric in every second iteration.
        # The resulting sequence of steps for training.loss will be 0, 2, 4, ...
        if counter % 2 == 0:
           _run.log_scalar("training.loss", value * 1.5 * noise, counter)
        # Implicit step counter (0, 1, 2, 3, ...)
        # incremented with each call for training.accuracy:
        _run.log_scalar("training.accuracy", value * 2 * noise)

ex.run()    

Wartości metryk możemy na żywo śledzić w Omniboard

Zadanie [15 pkt] (do 2023-06-?)

  1. "Owiń" wywołanie swojego eksperymentu za pomocą Sacred, w ten sposób, żeby zapisane zostały [10pkt]:
  • parametry, z którymi wywołany był trening

  • powstały plik z modelem (jako artefakt)

  • kod źródłowy użyty do przeprowadzenia treningu

  • pliki wejściowe otwarte za pomocą open_resource

  • metryki

    Jako nazwę eksperymentu użyj swojego numeru indeksu tak, żebyś mogła/mógł je odnaleźć w Omniboard

  1. Wykorzystaj 2 obserwatory [5pkt]:
  • MongoObserver, skorzytaj nastęþującego URL: mongodb://admin:IUM_2021@172.17.0.1:27017 (będziesz mógł przeglądać wyniki na http://tzietkiewicz.vm.wmi.amu.edu.pl:9000/sacred)
  • FileObserver - zapisane pliki zarchiwizuj na Jenkinsie jako jego artefakty