<jupyter_start><jupyter_text>Learning to Use a Hyper-parameter optimizer<jupyter_code>import sherpa
import rescomp as rc
import numpy as np

TOL = 0.2
EXPERIMENT = ("lorenz", 15, 0.01, None, 0)
DEFAULTS = {
    "res_sz":100,
    "activ_f": lambda x: 1/(1+np.exp(-1*x)),
    "sparse_res":True,
    "uniform_weights":True,
    "signal_dim":3,
    "max_weight":2,
    "min_weight":0,
    "batchsize":2000
}
def rcompacc(system, duration, dt, window, overlap, **kwargs):
    tr, Utr, ts, Uts = train_test_orbit(system, duration=duration, dt=dt)
    rcomp = rc.ResComp(**kwargs)
    rcomp.train(tr, Utr)
    r0 = rcomp.r0
    pre = rcomp.predict(ts, r0=r0)
    idx = accduration(Uts, pre, tol=TOL)
    acc = ts[idx-1] - ts[0]
    return acc
def train_test_orbit(system, duration=10, dt=0.01):
    t, U = rc.orbit(system, duration=duration, dt=dt)
        # Train and test data
    N = len(t)
    mid = int(N//2)
    tr, Utr = t[:mid], U[:mid, :]
    ts, Uts = t[mid:], U[mid:, :]
    return tr, Utr, ts, Uts
def L2(x, axis=0):
    return np.sum(x**2, axis=axis)**0.5
def Linf(x, axis=0):
    return np.max(np.abs(x), axis=axis)

def relerr(true, pre, order=2, axis=0):
    if order == 2:
        norm = L2
    if order == "inf":
        norm = Linf
    return norm(true - pre, axis=axis) / norm(true, axis=axis)

def accduration(true, pre, tol=0.2, order="inf", axis=0):
    n = pre.shape[axis]
    for i in range(n):
        if axis == 0:
            t = true[i, :]
            p = pre[i, :]
        if axis == 1:
            t = true[:, i]
            p = pre[:, i]
        if relerr(t, p, order=order, axis=0) > tol:
            return i
    return n

def system_fit_error(t, pre, system, order=2):
    df = rc.SYSTEM[system]["df"]
    trueprime = df(t, pre)
    dt = np.mean(np.diff(t))
    fdprime = np.gradient(pre, dt, axis=1)
    err = relerr(trueprime, fdprime)
    if order == 2:
        return np.mean(err)
    if order == inf:
        return np.max(err)
    return err<jupyter_output><empty_output><jupyter_text>Hyper Parameter Opt<jupyter_code>parameters = [sherpa.Continuous(name='gamma', range=[0.1, 25], ),
              sherpa.Continuous(name='sigma', range=[0.01, 5.0]),
              sherpa.Continuous(name='spect_rad', range=[0.1, 25]),
              sherpa.Continuous(name='ridge_alpha', range=[1e-8, 2], scale='log'),
              sherpa.Continuous(name='mean_degree', range=[0.1, 5])]

algorithm = sherpa.algorithms.RandomSearch(max_num_trials=150)

study = sherpa.Study(parameters=parameters,
                 algorithm=algorithm,
                 lower_is_better=False)
num_iterations = 10
for trial in study:
    for i in range
    acc = rcompacc(*EXPERIMENT, **trial.parameters, **DEFAULTS)
    study.add_observation(trial=trial, objective=acc)
    study.finalize(trial)
study.get_best_result()
study.results
import pandas as pd
df = study.results
df.aggregate(np.mean,"Tiral-ID",)
study.save()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15778758059043094, 'fraction_numerical': 0.025788937902952155, 'mean_word_length': 2.973045822102426, 'pattern_counts': {'":': 9, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '160005', 'license': 'MIT', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1115, 'n_words': 250, 'path': 'Notebooks/GettingStarted/LearnSherpa.ipynb', 'repo_name': 'tpool2/RCInitialCond', 'size': '2947'}