Skip to content

Persisting experiments#

The state of experiments is persisted to database with the method Experiment.persist(...). In this example, we demonstrate how to run an experiment with two runs, persisting it and loading it from database, querying with with SQL an with Pandas.

Storing and reloading an experiment

import numpy as np

from mltraq import create_session

session = create_session()
experiment = session.create_experiment("example")

experiment.fields.a = 10

with experiment.run() as run:

    run.fields.b = 20
    run.fields.c = np.array([1, 2, 3])

with experiment.run() as run:
    run.fields.b = 30

experiment.persist()

experiment = session.load_experiment("example")
print("Experiment:")
print(experiment.df())
print("\n--")

print("SQL query:")
print(session.db.query("SELECT id_run, b FROM experiment_example"))
print("\n--")


print("NumPy array in first run:")
print(experiment.runs.first().fields.c)
print("\n--")
Output
Experiment:
                          id_experiment     name   a
0  d65df69e-1175-44a5-be2f-2232765703b8  example  10

--
SQL query:
                                 id_run   b
0  d65df69e-1175-44a5-be2f-2232765703b9  20
1  d65df69e-1175-44a5-be2f-2232765703ba  30

--
NumPy array in first run:
[1 2 3]

--

Tip

The dictionary fields is available for both Experiment and Run objects. Experiment.fields is unique to the experiment while Run.fields holds values from the Run instance.

Info

The serialization format and more details on the persistence logic are presented in depth at State storage.

Congratulations!

You experimented with serialization, deserialization, and the usage of native database types.