<jupyter_start><jupyter_text>BO<jupyter_code># %%timeit
BO = bayesopt.BayesOpt(f=f,
                       initial_input=np.array([0.]),
                       acq=bayesopt.acquisition.LCB,
                       acq_optim=bayesopt.acquisition_optimizer.Acquisition_SLSQP_Optimizer(bounds=[0,18]),
                       maximize=False,
                       )
BO.run_optim(20)
X = np.arange(0,18,0.2)
plt.plot(BO.param_history,BO.value_history,'o--')
plt.plot(BO.best_params,BO.best_value,'x')
plt.plot(X,f(X))
bayesopt.plot_history(BO)
def f(x,y):
    return (x-2.)**2 + (y+3)**2

def f2(x):
    return f(x[:,0],x[:,1])
# %%timeit
BO = bayesopt.BayesOpt(f=f2,
                       initial_input=np.array([[0.,0.]]),
                       acq=bayesopt.acquisition.LCB,
                       acq_optim=bayesopt.acquisition_optimizer.Acquisition_SLSQP_Optimizer(bounds=[[-5,5],[-5,5]]),
                       maximize=False,
                       function_input_unpacking=False,
                       )
BO.run_optim(20)<jupyter_output>BayesOpt: 100%|██████████| 20/20 [00:15<00:00,  1.31it/s, param:[ 1.02586352 -3.34440788], value:[1.06755867], current best param:[ 2.37174805 -2.85125311], current best_value:[0.16032225]]<jupyter_text>use original kernel and acq function<jupyter_code>dists = bayesopt.utils.pairwise(bayesopt.metric.euclid_distance,square=True)
def kernel(x,y):
    return np.exp(-dists(x,y))
print(kernel(np.array([[2,3],[1,2]]),np.array([[2,3],[1,2],[1,1]])))
def acq(mu,sigma,*args,**kwargs):
    '''
    mu, sigma, it=it, vmax=vmax, vmin=vminが入力されるようacq optimで決めている.
    '''
    it = kwargs.get('it',5.)
    return -mu+sigma*5*np.sqrt(np.log(it+1)/(it+1))
BO = bayesopt.BayesOpt(f=f,
                       initial_input=np.array([0.]),
                       kernel=kernel,
                       acq=acq,
                       acq_optim=bayesopt.acquisition_optimizer.Acquisition_L_BFGS_B_Optimizer(bounds=[0,18]))
BO.run_optim(20)
X = np.arange(0,18,0.2)
plt.plot(BO.param_history,BO.value_history,'o--')
plt.plot(BO.best_params,BO.best_value,'x')
plt.plot(X,f(X))
bayesopt.plot_history(BO)<jupyter_output><empty_output><jupyter_text>memo acuisition optimizer<jupyter_code>def AcquisitionSLSQPOptimizer(gpr, acq, it, bounds, n_trial=5):
    ## gprとacqとitは受け取れるようにしないといけない. 
    ##boundsはfunctoolのpartialで指定するか、内部変数に持たせるか
    bounds = np.atleast_2d(bounds)
    vmax = np.max(gpr.Y_train)
    vmin = np.min(gpr.Y_train)
    ndim = len(bounds)
    loc = None
    value = None

    import scipy.optimize  
    def Obj(x):
        mu,sigma = gpr.posterior_predictive(np.atleast_2d(x),return_std=True)
        return -1.*acq(mu,sigma, it=it, vmax=vmax, vmin=vmin).ravel()

    x_seeds = onp.random.uniform(bounds[:,0],bounds[:,1], size=(n_trial,ndim)) 
    for xtry in x_seeds:
        res = scipy.optimize.fmin_slsqp(Obj, 
                                        x0=xtry, 
                                        bounds=bounds, 
                                        iprint=0, 
                                        full_output=True, 
                                        iter=100)
        if (loc is None) or (res[1] < value):
            loc = res[0]
            value = res[1]
    return loc, value<jupyter_output><empty_output><jupyter_text>memo terminate functionを設定できるようにした   入力はit,param_history,value_historyの順番で入る<jupyter_code>def terminate_function(it, param_history, value_history):
    if value_history.min()<1e-1:
        return True
    else:
        return False<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14733276883996613, 'fraction_numerical': 0.039514535704205475, 'mean_word_length': 2.536926147704591, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '635759', 'license': 'MIT', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1340, 'n_words': 157, 'path': 'test.ipynb', 'repo_name': 'JohnYKiyo/bayesian_optimization', 'size': '3543'}