Exemplo n.º 1
0
    def preprocess(self):
        self.target.mi.target = self.target

        # assemble hyper parameters
        self.length_scales, self.amp_variance, self.single_noise_variance, self.mean_noise_variance, self.precision_matrix = normscales.normscales(
            self.target.mi, self.devices, correlationsQ=self.correlationsQ)

        # build precision_matrix if not returned
        print('Precision before', self.precision_matrix)
        if self.precision_matrix is None:
            self.covarmat = np.diag(self.length_scales)**2
            print('Covariance', self.covarmat)
            self.precision_matrix = np.linalg.inv(self.covarmat)
        print('Precision', self.precision_matrix)
        print('Length Scales', self.length_scales)
        # create OnlineGP model
        dim = len(self.devices)
        hyperparams = (self.precision_matrix, np.log(self.amp_variance),
                       np.log(self.mean_noise_variance))
        #        self.model = OGP(dim, hyperparams, maxBV=self.numBV, weighted=False)
        self.model = OGP(dim,
                         hyperparams,
                         maxBV=self.numBV,
                         covar=['RBF_ARD', 'MATERN32_ARD', 'MATERN52_ARD'][0],
                         weighted=False)
        # initialize model on prior data if available
        if (self.prior_data is not None):
            p_X = self.prior_data.iloc[:, :-1]
            p_Y = self.prior_data.iloc[:, -1]
            num = p_X.shape[0]
            self.model.fit(p_X, p_Y, min(self.m, num))

        # create Bayesian optimizer
        dev_ids = [dev.eid for dev in self.devices]
        dev_vals = [dev.get_value() for dev in self.devices]
        self.scanner = BayesOpt(
            model=self.model,
            target_func=self.target,
            acq_func=self.acq_func,
            xi=self.xi,
            alt_param=self.alt_param,
            m=self.m,
            bounds=self.bounds,
            iter_bound=self.iter_bound,
            prior_data=self.prior_data,
            start_dev_vals=dev_vals,
            dev_ids=dev_ids,
            searchBoundScaleFactor=self.searchBoundScaleFactor)
        self.scanner.max_iter = self.max_iter
        self.scanner.opt_ctrl = self.opt_ctrl
Exemplo n.º 2
0
def testFunc():
    """ Function to try and find a bug in the model load function """

    numBV = 50
    xi = .01
    bnds = None

    thing = GpScanner()
    pvs = [
        "SIOC:SYS0:ML00:CALCOUT000", "SIOC:SYS0:ML00:CALCOUT999",
        "SIOC:SYS0:ML00:CALCOUT998", "SIOC:SYS0:ML00:CALCOUT997"
    ]

    #thing.setup(pvs, 'GDET:FEE1:241:ENRCHSTBR')
    thing.pvs = pvs
    thing.objective_func_pv = 'GDET:FEE1:241:ENRCHSTBR'
    total_delay = .2
    mi = LCLSMachineInterface()
    mi.setUpDetector(pvs, detector=thing.objective_func_pv)
    mi.setup_data_record(pvs)
    dp = LCLSDeviceProperties()
    dp.get_start_values(pvs)
    interface = GpInterfaceWrapper(pvs, mi, dp, total_delay)
    s_data = thing.loadSeedData(thing.seed_file)

    hyps = thing.loadHyperParams(thing.hyp_file)
    thing.model = OGP(len(pvs), hyps, maxBV=50)

    filename = '/u1/lcls/matlab/data/2016/2016-04/2016-04-25/OcelotScan-2016-04-25-181811.mat'
    model_file = scipy.io.loadmat(filename)['data']
    thing.model.alpha = model_file['alpha'].flatten(0)[0]
    thing.model.C = model_file['C'].flatten(0)[0]
    thing.model.BV = model_file['BV'].flatten(0)[0]
    thing.model.covar_params = model_file['covar_params'].flatten(0)[0]
    thing.model.KB = model_file['KB'].flatten(0)[0]
    thing.model.KBinv = model_file['KBinv'].flatten(0)[0]
    thing.model.weighted = model_file['weighted'].flatten(0)[0]

    thing.model.covar_params = (thing.model.covar_params[0][0],
                                thing.model.covar_params[1][0])
    print type(thing.model.covar_params)
    print thing.model.covar_params
    thing.model.predict(np.array(s_data[0, :-1], ndmin=2))

    thing.opt = BOpt.BayesOpt(thing.model,
                              interface,
                              prior_data=pd.DataFrame(s_data))
Exemplo n.º 3
0
 def preprocess(self):
     hyp_params = HyperParams(pvs=self.devices, filename=self.hyper_file)
     self.energy = str(round(self.target.get_energy()))
     dev_ids = [dev.eid for dev in self.devices]
     hyps1 = hyp_params.loadHyperParams(self.hyper_file, self.energy,
                                        self.target, dev_ids,
                                        self.multiplier)
     dim = len(self.devices)
     self.model = OGP(dim, hyps1, maxBV=self.numBV, weighted=False)
     self.scanner = BayesOpt(model=self.model,
                             target_func=self.target,
                             acq_func=self.acq_func,
                             xi=self.xi,
                             alt_param=self.alt_param,
                             m=self.m,
                             bounds=self.bounds,
                             iter_bound=self.iter_bound,
                             prior_data=self.prior_data)
     self.scanner.max_iter = self.max_iter
Exemplo n.º 4
0
    def __init__(self,
                 dim,
                 hidden_layers=[],
                 dim_z=None,
                 mask=None,
                 alpha=1.0,
                 noise=0.1,
                 activations='lrelu',
                 weight_dir=None):
        self.dim = dim
        self.dim_z = dim_z or dim

        # initialize the OGP object we use to actually make our predictions
        OGP_params = (np.zeros((self.dim_z, )), np.log(alpha), np.log(noise)
                      )  # lengthscales of one (logged)
        self.ogp = OGP(self.dim_z, OGP_params)

        # our embedding function, initially the identity
        # if unchanged, the DKLGP should match the functionality of OGP
        self.embed = lambda x: x

        # build the neural network structure of the DKL
        self.layers = []
        for l in hidden_layers:
            self.layers.append(Dense(l, activation=activations))

        # add the linear output layer and the GP (used for likelihood training)
        if len(self.layers) > 0:
            self.layers.append(Dense(dim_z))
        else:
            self.mask = mask
            self.layers.append(Dense(dim_z, mask=mask))
        self.layers.append(CovMat(
            kernel='rbf',
            alpha_fixed=False))  # kernel should match the one used in OGP

        # if weight_dir is specified, we immediately initialize the embedding based on the specified neural network
        if weight_dir is not None:
            self.load_embedding(weight_dir)
Exemplo n.º 5
0
def test_GP():
    """
    test GP method
    :return:
    """
    def get_limits():
        return [-100, 100]

    d1 = TestDevice(eid="d1")
    d1.get_limits = get_limits
    d2 = TestDevice(eid="d2")
    d2.get_limits = get_limits
    d3 = TestDevice(eid="d3")
    d3.get_limits = get_limits

    devices = [d1, d2]
    target = TestTarget()

    opt = Optimizer()
    opt.timeout = 0

    opt_smx = Optimizer()
    opt_smx.timeout = 0
    minimizer = Simplex()
    minimizer.max_iter = 3
    opt_smx.minimizer = minimizer
    #opt.debug = True

    seq = [Action(func=opt_smx.max_target_func, args=[target, devices])]
    opt_smx.eval(seq)
    s_data = np.append(np.vstack(opt_smx.opt_ctrl.dev_sets),
                       np.transpose(-np.array([opt_smx.opt_ctrl.penalty])),
                       axis=1)
    print(s_data)

    # -------------- GP config setup -------------- #
    #GP parameters
    numBV = 30
    xi = 0.01
    #no input bounds on GP selection for now

    pvs = [dev.eid for dev in devices]
    hyp_params = HyperParams(pvs=pvs,
                             filename="../parameters/hyperparameters.npy")
    ave = np.mean(-np.array(opt_smx.opt_ctrl.penalty))
    std = np.std(-np.array(opt_smx.opt_ctrl.penalty))
    noise = hyp_params.calcNoiseHP(ave, std=0.)
    coeff = hyp_params.calcAmpCoeffHP(ave, std=0.)
    len_sc_hyps = []
    for dev in devices:
        ave = 10
        std = 3
        len_sc_hyps.append(hyp_params.calcLengthScaleHP(ave, std))
    print("y_data", opt_smx.opt_ctrl.penalty)
    print("pd.DataFrame(s_data)", pd.DataFrame(s_data))
    print("len_sc_hyps", len_sc_hyps)

    bnds = None
    #hyps = hyp_params.loadHyperParams(energy=3, detector_stat_params=target.get_stat_params())
    hyps1 = (np.array([len_sc_hyps]), coeff, noise
             )  #(np.array([hyps]), coeff, noise)
    print("hyps1", hyps1)
    #exit(0)
    #init model
    dim = len(pvs)

    model = OGP(dim, hyps1, maxBV=numBV, weighted=False)

    minimizer = BayesOpt(model,
                         target_func=target,
                         xi=0.01,
                         acq_func='EI',
                         bounds=bnds,
                         prior_data=pd.DataFrame(s_data))
    minimizer.devices = devices
    minimizer.max_iter = 300
    opt.minimizer = minimizer

    seq = [Action(func=opt.max_target_func, args=[target, devices])]
    opt.eval(seq)
    def setup(self, devices, objective_func, iters=45):
        """
        Basic setup procedure for the GP scan objects, input args that are common to optimizer classes.

        Similar setup funtion with the same args as the ocelot scanner.
        Does not contain option to load in MachineInterface and DeviceProperties

        Could probably just write all this into the init function if you have motivation.

        Args:
                devices (List[str]): List of the PVs to be used in scan
                objective_func (str): String for the PV for the value to maximize
                iters (int): Number of iterations for the scanner to run
        """

        #load in the pvs to scan
        self.devices = devices
        self.objective_func = objective_func
        # for testing
        self.objective_func.devices = self.devices

        #number of iterations to scan (hardcode for now)
        self.iters = iters

        #set new timing variables
        #self.mi.secs_to_ave = self.parent.data_delay
        self.total_delay = self.parent.trim_delay + self.parent.data_delay

        # -------------- GP config setup -------------- #

        #GP parameters
        self.numBV = 30
        self.xi = 0.01

        #no input bounds on GP selection for now
        bnds = None

        pvs = [dev.eid for dev in devices]

        hyp_params = BOpt.HyperParams(pvs=pvs, filename=self.hyp_file)
        hyps = hyp_params.loadHyperParams(
            objective_func.get_energy(),
            detector_stat_params=objective_func.get_stat_params())

        #init model
        dim = len(devices)

        self.model = OGP(dim, hyps, maxBV=self.numBV, weighted=False)

        #load model stuff
        filename = '/u1/lcls/matlab/data/2016/2016-04/2016-04-25/OcelotScan-2016-04-25-181811.mat'

        #if you would lake to load a model from file, use this function
        #self.model = self.loadModelParams(self.model,filename)

        #if this is not a simplex seeded scan, setup the seed data from mat file and build optimizer object\
        if not self.seedScanBool:
            #load seed data file
            s_data = self.loadSeedData(self.seed_file)
            #optimzer object
            self.opt = Optimizer()
            self.opt.timeout = self.total_delay
            minimizer = BOpt.BayesOpt(self.model,
                                      self.objective_func,
                                      acq_func='EI',
                                      xi=self.xi,
                                      bounds=bnds,
                                      prior_data=pd.DataFrame(s_data))
            #self.opt = BOpt.BayesOpt(self.model, self.interface, xi=self.xi, acq_func='EI', bounds=bnds, prior_data=pd.DataFrame(s_data))
            #bool to kill scanner thread from GUI
            minimizer.devices = devices
            minimizer.max_iter = iter
            self.opt.minimizer = minimizer
            self.seq = [
                Action(func=self.opt.max_target_func,
                       args=[objective_func, self.devices])
            ]
            self.opt.kill = False