In [ ]:
Copied!
# Uncomment the following line when running on Google Colab
# !pip install autora
# Uncomment the following line when running on Google Colab
# !pip install autora
In [ ]:
Copied!
import autora.workflow
import dill
controller = autora.workflow.Controller()
with open("default-controller.dill", "wb") as file:
dill.dump(controller, file)
import autora.workflow
import dill
controller = autora.workflow.Controller()
with open("default-controller.dill", "wb") as file:
dill.dump(controller, file)
The same Controller state can be loaded in a separate session as follows:
In [ ]:
Copied!
import dill
with open("default-controller.dill", "rb") as file:
reloaded_controller = dill.load(file)
reloaded_controller
import dill
with open("default-controller.dill", "rb") as file:
reloaded_controller = dill.load(file)
reloaded_controller
Out[ ]:
<autora.workflow.controller.Controller at 0x12a49b400>
Executors¶
A Controller for real use always includes at least one (and usually more than one) Executor.
In this example, the Controller includes:
- an experimentalist, which suggests a random sample across a particular problem domain,
- an experiment runner, which makes synthetic observations of a polynomial function,
- and a theorist, which is a scikit-learn regressor.
In [ ]:
Copied!
import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline as make_theorist_pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import autora.workflow
from autora.experimentalist.pipeline import make_pipeline as make_experimentalist_pipeline
from autora.variable import VariableCollection, Variable
rng = np.random.default_rng(180)
experimentalist = make_experimentalist_pipeline(
[np.linspace, rng.choice],
params={"linspace": {"start": [-10], "stop": [+10], "num":1001},
"choice": {"size": 10}}
)
def experiment_runner(x, coefs=[2.0, 3.0, 1.0], noise_std=0.1):
"""Simple experiment."""
x_ = np.array(x)
y_ = (
coefs[0] * x_**2.0
+ coefs[1] * x_
+ coefs[2]
+ rng.normal(0.0, noise_std, size=x_.shape)
)
return y_
theorist = GridSearchCV(make_theorist_pipeline(PolynomialFeatures(), LinearRegression()),
param_grid={"polynomialfeatures__degree": [0, 1, 2, 3, 4]}, scoring="r2")
controller = autora.workflow.Controller(
variables=VariableCollection(independent_variables=[Variable("x")], dependent_variables=[Variable("y")]),
experiment_runner=experiment_runner,
experimentalist=experimentalist,
theorist=theorist,
)
with open("simple-controller.dill", "wb") as file:
dill.dump(controller, file)
import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline as make_theorist_pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import autora.workflow
from autora.experimentalist.pipeline import make_pipeline as make_experimentalist_pipeline
from autora.variable import VariableCollection, Variable
rng = np.random.default_rng(180)
experimentalist = make_experimentalist_pipeline(
[np.linspace, rng.choice],
params={"linspace": {"start": [-10], "stop": [+10], "num":1001},
"choice": {"size": 10}}
)
def experiment_runner(x, coefs=[2.0, 3.0, 1.0], noise_std=0.1):
"""Simple experiment."""
x_ = np.array(x)
y_ = (
coefs[0] * x_**2.0
+ coefs[1] * x_
+ coefs[2]
+ rng.normal(0.0, noise_std, size=x_.shape)
)
return y_
theorist = GridSearchCV(make_theorist_pipeline(PolynomialFeatures(), LinearRegression()),
param_grid={"polynomialfeatures__degree": [0, 1, 2, 3, 4]}, scoring="r2")
controller = autora.workflow.Controller(
variables=VariableCollection(independent_variables=[Variable("x")], dependent_variables=[Variable("y")]),
experiment_runner=experiment_runner,
experimentalist=experimentalist,
theorist=theorist,
)
with open("simple-controller.dill", "wb") as file:
dill.dump(controller, file)
Later, we can reload the same Controller and run the cycle:
In [ ]:
Copied!
with open("simple-controller.dill", "rb") as file:
controller_loaded = dill.load(file)
controller_loaded.run(num_steps=10)
with open("simple-controller.dill", "rb") as file:
controller_loaded = dill.load(file)
controller_loaded.run(num_steps=10)
Out[ ]:
<autora.workflow.controller.Controller at 0x12a4e7430>
In [ ]:
Copied!
controller_loaded.state.models[-1].best_params_
controller_loaded.state.models[-1].best_params_
Out[ ]:
{'polynomialfeatures__degree': 2}
In [ ]:
Copied!
import matplotlib.pyplot as plt
x = np.linspace([-10], [10], 100)
plt.plot(x, experiment_runner(x, noise_std=0.),
label="ground truth",
lw=10, alpha=0.2, c="black")
plt.plot(x, controller_loaded.state.models[-1].best_estimator_.predict(x),
label="prediction",
lw=1, alpha=1, c="red")
plt.legend()
import matplotlib.pyplot as plt
x = np.linspace([-10], [10], 100)
plt.plot(x, experiment_runner(x, noise_std=0.),
label="ground truth",
lw=10, alpha=0.2, c="black")
plt.plot(x, controller_loaded.state.models[-1].best_estimator_.predict(x),
label="prediction",
lw=1, alpha=1, c="red")
plt.legend()
Out[ ]:
<matplotlib.legend.Legend at 0x12f5cdbd0>