Пример #1
0
    def get_emulator ( self, tag ):
        """
        Recovers an emulator from storage, and returns it to 
        the calller
        """
        if os.path.exists ( self.fname ):
            # File exists, so open and get a handle to it
            emulators = shelve.open ( self.fname )
        else:
            raise IOError ("File %s doesn't exist!" % self.fname )
                

        if type(tag) != str:
            tag = self._declutter_key ( tag )
            
        if emulators[tag].has_key ( "basis_functions" ):
            gp = MultivariateEmulator ( \
                X = emulators[tag]["X"], \
                y=emulators[tag]["y"], \
                hyperparams = emulators[tag]["hyperparams"], 
                basis_functions = emulators[tag]["basis_functions"] )
        else:
            gp = GaussianProcess ( \
                emulators[tag]["inputs"], \
                emulators[tag]["targets"] )
            gp._set_params ( emulators[tag]['theta'] )
        emulators.close()
        return gp
Пример #2
0
    def get_emulator(self, tag):
        """
        Recovers an emulator from storage, and returns it to 
        the calller
        """
        if os.path.exists(self.fname):
            # File exists, so open and get a handle to it
            emulators = shelve.open(self.fname)
        else:
            raise IOError("File %s doesn't exist!" % self.fname)

        if type(tag) != str:
            tag = self._declutter_key(tag)

        if emulators[tag].has_key("basis_functions"):
            gp = MultivariateEmulator ( \
                X = emulators[tag]["X"], \
                y=emulators[tag]["y"], \
                hyperparams = emulators[tag]["hyperparams"],
                basis_functions = emulators[tag]["basis_functions"] )
        else:
            gp = GaussianProcess ( \
                emulators[tag]["inputs"], \
                emulators[tag]["targets"] )
            gp._set_params(emulators[tag]['theta'])
        emulators.close()
        return gp
Пример #3
0
def create_emulator_validation(f_simulator,
                               parameters,
                               minvals,
                               maxvals,
                               n_train,
                               n_validate,
                               do_gradient=True,
                               thresh=0.98,
                               n_tries=5,
                               args=(),
                               n_procs=None):
    """A method to create an emulator, given the simulator function, the
    parameters names and boundaries, the number of training input/output pairs. 
    The function will also provide an independent validation dataset, both for 
    the valuation of the function and its gradient. The gradient is calculated
    using finite differences, so it is a bit ropey.
    
    Parameters
    ------------
    f_simulator: function
        A function that evaluates the simulator. It should take a single 
        parameter which will be made out of the input vector, plus whatever
        other extra arguments one needs (stored in ``args``).
    parameters: list
        The parameter names
    minvals: list
        The minimum value of the parameters
    maxvals: list
        The maximum value of the parameters
    n_train: int
        The number of training samples
    n_validate: int
        The number of validation samples
    thresh: float
        For a multivariate output GP, the threshold at which to cut the 
        PCA expansion.
    n_tries: int
        The number of tries in the GP hyperparameter stage. The more the better,
        but also the longer it will take.
    args: tuple
        A list of extra arguments to the model
    do_gradient: Boolean
        Whether to do a gradient validation too.
        
        
    Returns
        The GP object, the validation input set, the validation output set, the
        emulated validation set, the emulated gradient set. If the gradient
        validation is also done, it will also return the gradient validation 
        using finite differences.
        
    """

    # First, create the training set, using the appropriate function from
    # above...
    samples, distributions = create_training_set(parameters,
                                                 minvals,
                                                 maxvals,
                                                 n_train=n_train)
    # Now, create the validation set, using the distributions object we got
    # from creating the training set
    validate = []
    for d in distributions:
        validate.append(d.rvs(n_validate))
    validate = np.array(validate).T

    # We have the input pairs for the training and validation. We will now run
    # the simulator function

    if n_procs is None:
        training_set = map(f_simulator, [((x, ) + args) for x in samples])
        validation_set = map(f_simulator, [((x, ) + args) for x in validate])

    else:
        pool = multiprocessing.Pool(processes=n_procs)

        training_set = pool.map(f_simulator, [((x, ) + args) for x in samples])
        validation_set = pool.map(f_simulator,
                                  [((x, ) + args) for x in validate])
    training_set = np.array(training_set).squeeze()
    validation_set = np.array(validation_set)

    if training_set.ndim == 1:
        gp = GaussianProcess(samples, training_set)
        gp.learn_hyperparameters(n_tries=n_tries)
    else:
        gp = MultivariateEmulator(X=training_set , \
                        y=samples, thresh=thresh, n_tries=n_tries )

    X = [gp.predict(np.atleast_2d(x)) for x in validate]
    if len(X[0]) == 2:
        emulated_validation = np.array([x[0] for x in X])
        emulated_gradient = np.array([x[1] for x in X])
    elif len(X[0]) == 3:
        emulated_validation = np.array([x[0] for x in X])
        emulated_gradient = np.array([x[2] for x in X])
    # Now with gradient... Approximate with finite differences...

    if do_gradient:
        val_set = [((x, ) + args) for x in validate]
        validation_gradient = []
        delta = [(maxvals[j] - minvals[j]) / 10000.
                 for j in xrange(len(parameters))]
        delta = np.array(delta)
        for i, pp in enumerate(val_set):
            xx0 = pp[0] * 1.
            grad_val_set = []
            f0 = validation_set[i]
            df = []
            for j in xrange(len(parameters)):
                xx = xx0 * 1
                xx[j] = xx0[j] + delta[j]
                grad_val_set.append(xx)
                df.append(f_simulator(((xx, ) + args)))
            df = np.array(df)
            try:
                validation_gradient.append((df - f0) / delta)
            except ValueError:
                validation_gradient.append((df - f0) / delta[:, None])

        return gp, validate, validation_set, np.array(validation_gradient), \
            emulated_validation, emulated_gradient.squeeze()
    else:
        return gp, validate, validation_set,  emulated_validation, \
            emulated_gradient
Пример #4
0
def create_emulator_validation ( f_simulator, parameters, minvals, maxvals, 
                                n_train, n_validate, do_gradient=True, 
                                thresh=0.98, n_tries=5, args=() ):


    """A method to create an emulator, given the simulator function, the
    parameters names and boundaries, the number of training input/output pairs. 
    The function will also provide an independent validation dataset, both for 
    the valuation of the function and its gradient. The gradient is calculated
    using finite differences, so it is a bit ropey.
    
    Parameters
    ------------
    f_simulator: function
        A function that evaluates the simulator. It should take a single 
        parameter which will be made out of the input vector, plus whatever
        other extra arguments one needs (stored in ``args``).
    parameters: list
        The parameter names
    minvals: list
        The minimum value of the parameters
    maxvals: list
        The maximum value of the parameters
    n_train: int
        The number of training samples
    n_validate: int
        The number of validation samples
    thresh: float
        For a multivariate output GP, the threshold at which to cut the 
        PCA expansion.
    n_tries: int
        The number of tries in the GP hyperparameter stage. The more the better,
        but also the longer it will take.
    args: tuple
        A list of extra arguments to the model
    do_gradient: Boolean
        Whether to do a gradient validation too.
        
        
    Returns
        The GP object, the validation input set, the validation output set, the
        emulated validation set, the emulated gradient set. If the gradient
        validation is also done, it will also return the gradient validation 
        using finite differences.
        
    """
    
    # First, create the training set, using the appropriate function from
    # above...
    samples, distributions = create_training_set ( parameters, minvals, maxvals, 
                                                  n_train=n_train )
    # Now, create the validation set, using the distributions object we got
    # from creating the training set
    validate  = []
    for d in distributions:
        validate.append ( d.rvs( n_validate ))
    validate = np.array ( validate ).T
    
    # We have the input pairs for the training and validation. We will now run
    # the simulator function
    
    pool = multiprocessing.Pool ()
    
    
    training_set = pool.map  ( f_simulator, [( (x,)+args) for x in samples] )
    validation_set = pool.map  ( f_simulator, [( (x,)+args) for x in validate] )
    training_set = np.array ( training_set )
    validation_set = np.array ( validation_set )

    try:
        gp = MultivariateEmulator(X=training_set , \
                        y=samples, thresh=thresh, n_tries=n_tries )
    except:
        gp = GaussianProcess( samples, training_set )
        gp.learn_hyperparameters( n_tries = n_tries )

    
    X = [ gp.predict ( np.atleast_2d(x) ) 
                        for x in validate ] 
    if len ( X[0] ) == 2:
        emulated_validation = np.array ( [ x[0] for x in X] )
        emulated_gradient = np.array ( [ x[1] for x in X] )
    elif len ( X[0] ) == 3:
        emulated_validation = np.array ( [ x[0] for x in X] )
        emulated_gradient = np.array ( [ x[2] for x in X] )
    # Now with gradient... Approximate with finite differences...
    
    

    if do_gradient:
        val_set = [( (x,)+args) for x in validate]
        validation_gradient = []
        delta = [(maxvals[j] - minvals[j])/10000. 
                    for j in xrange(len(parameters)) ]
        delta = np.array ( delta )
        for i, pp in enumerate( val_set ):
            xx0 = pp[0]*1.
            grad_val_set = []
            f0 = validation_set[i]
            df = []
            for j in xrange ( len ( parameters ) ):
                xx = xx0*1
                xx[j] = xx0[j] + delta[j]
                grad_val_set.append ( xx  )
                df.append ( f_simulator ( ( (xx,) + args ) ) )
            df = np.array ( df )
            try:
                validation_gradient.append (  (df-f0)/delta )
            except ValueError:
                validation_gradient.append (  (df-f0)/delta[:, None] )
                
        return gp, validate, validation_set, np.array(validation_gradient), \
            emulated_validation, emulated_gradient.squeeze()
    else:
        return gp, validate, validation_set,  emulated_validation, \
            emulated_gradient