Heteroscedastic evolutionary bayesian optimisation
Project description
README
Installation
python -m pip install .
Demo
import mindspore.nn as nn
import mindspore.dataset as ds
from mindspore import Tensor
import numpy as np
from sklearn.datasets import load_boston
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from hebo.design_space import DesignSpace
from hebo.optimizers.hebo import HEBO
ds.config.set_num_parallel_workers(1)
def obj(lr : float, weight_decay : float, hidden_size : int) -> float:
X, y = load_boston(return_X_y = True)
y = y.reshape(-1, 1)
X_tr, X_tst, y_tr, y_tst = train_test_split(
X.astype(np.float32),
y.astype(np.float32),
test_size = 0.3,
shuffle = True,
random_state = 42)
dataset = ds.GeneratorDataset(
lambda: ((x_, y_) for (x_, y_) in zip(X_tr, y_tr)),
column_names = ['x_train', 'y_train'],
shuffle = True,
python_multiprocessing = False
)
dataset = dataset.batch(32)
net = nn.SequentialCell(
nn.Dense(13, hidden_size),
nn.ReLU(),
nn.Dense(hidden_size, 1))
crit = nn.MSELoss()
opt = nn.Adam(params = net.trainable_params(), learning_rate = lr, weight_decay = weight_decay)
net_with_crit = nn.WithLossCell(net, crit)
train_net = nn.TrainOneStepCell(net_with_crit, opt)
for _ in range(100):
for d in dataset.create_dict_iterator():
train_net(d['x_train'], d['y_train'])
py_tst = net(Tensor(X_tst)).asnumpy()
r2 = r2_score(y_tst, py_tst)
return -1 * np.array(r2).reshape(-1, 1)
if __name__ == '__main__':
space = DesignSpace().parse([
{'name' : 'lr' , 'type' : 'pow', 'lb' : 1e-4, 'ub' : 3e-2},
{'name' : 'weight_decay' , 'type' : 'pow', 'lb' : 1e-6, 'ub' : 3e-2},
{'name' : 'hidden_size' , 'type' : 'int', 'lb' : 16, 'ub' : 128},
])
opt = HEBO(space)
for iter in range(50):
rec = opt.suggest()
lr = float(rec.iloc[0].lr)
weight_decay = float(rec.iloc[0].weight_decay)
hidden_size = int(rec.iloc[0].hidden_size)
observation = obj(lr, weight_decay, hidden_size)
opt.observe(rec, observation)
print('After %d iterations, best obj is %.3f' % (iter + 1, opt.y.min()))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file HEBO_mindspore-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: HEBO_mindspore-0.2.1-py3-none-any.whl
- Upload date:
- Size: 47.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc55ff944be8070e77c69be3f810495a24fdaa644889baebdb8a6a932dae7513 |
|
MD5 | b150084c7725dc057bdd207ef9090e0d |
|
BLAKE2b-256 | 29168e8eaddf3719430698bb2a783c76711124a6ae89fa1aa6d33f4605a5f00a |