Пример #1
0
def create_inverse_emulators(original_emulator, band_pass, state_config):
    """
    This function takes a multivariable output trained emulator
    and "retrains it" to take input reflectances and report a
    prediction of single input parameters (i.e., regression from
    reflectance/radiance to state). This is a useful starting 
    point for spatial problems.
    
    Parameters
    ------------
    original_emulator: emulator
        An emulator (type gp_emulator.GaussianProcess)
    band_pass: array
        A 2d bandpass array (nbands, nfreq). Logical type
    state_config: ordered dict
        A state configuration ordered dictionary.
    """

    # For simplicity, let's get the training data out of the emulator
    X = original_emulator.X_train * 1.
    y = original_emulator.y_train * 1.
    # Apply band pass functions here...
    n_bands = band_pass.shape[0]
    xx = np.array( [ X[:, band_pass[i,:]].sum(axis=1)/ \
        (1.*band_pass[i,:].sum()) \
        for i in xrange( n_bands ) ] )

    # A container to store the emulators
    gps = {}
    for i, (param, typo) in enumerate(state_config.iteritems()):
        if typo == VARIABLE:
            gp = GaussianProcess(xx.T, y[:, i])
            gp.learn_hyperparameters(n_tries=3)
            gps[param] = gp
    return gps
Пример #2
0
def create_inverse_emulators ( original_emulator, band_pass, state_config ):
    """
    This function takes a multivariable output trained emulator
    and "retrains it" to take input reflectances and report a
    prediction of single input parameters (i.e., regression from
    reflectance/radiance to state). This is a useful starting 
    point for spatial problems.
    
    Parameters
    ------------
    original_emulator: emulator
        An emulator (type gp_emulator.GaussianProcess)
    band_pass: array
        A 2d bandpass array (nbands, nfreq). Logical type
    state_config: ordered dict
        A state configuration ordered dictionary.
    """
    
    # For simplicity, let's get the training data out of the emulator
    X = original_emulator.X_train*1.
    y = original_emulator.y_train*1.
    # Apply band pass functions here...
    n_bands = band_pass.shape[0]
    xx = np.array( [ X[:, band_pass[i,:]].sum(axis=1)/ \
        (1.*band_pass[i,:].sum()) \
        for i in xrange( n_bands ) ] )

    # A container to store the emulators
    gps = {}
    for  i, (param, typo) in enumerate ( state_config.iteritems()) :
        if typo == VARIABLE:
            gp = GaussianProcess ( xx.T, y[:, i] )
            gp.learn_hyperparameters( n_tries = 3 )
            gps[param] = gp 
    return gps
Пример #3
0
def perband_emulators ( emulators, band_pass ):
    """This function creates per band emulators from the full-spectrum
    emulator. Should be faster in many cases"""
    
    n_bands = band_pass.shape[0]
    x_train_pband = [ emulators.X_train[:,band_pass[i,:]].mean(axis=1) \
        for i in xrange( n_bands ) ]
    x_train_pband = np.array ( x_train_pband )
    emus = []
    for i in xrange( n_bands ):
        gp = GaussianProcess ( emulators.y_train[:]*1, \
                x_train_pband[i,:] )
        gp.learn_hyperparameters ( n_tries=5 )
        emus.append ( gp )
    return emus
Пример #4
0
def perband_emulators(emulators, band_pass):
    """This function creates per band emulators from the full-spectrum
    emulator. Should be faster in many cases"""

    n_bands = band_pass.shape[0]
    x_train_pband = [ emulators.X_train[:,band_pass[i,:]].mean(axis=1) \
        for i in xrange( n_bands ) ]
    x_train_pband = np.array(x_train_pband)
    emus = []
    for i in xrange(n_bands):
        gp = GaussianProcess ( emulators.y_train[:]*1, \
                x_train_pband[i,:] )
        gp.learn_hyperparameters(n_tries=5)
        emus.append(gp)
    return emus
Пример #5
0
def test_rosenbrock_gp(size, d):

    # The training dataset created from rosenbrock function
    train_x = random.uniform(-5, 10, d * size).reshape(size, d)
    train_y = np.apply_along_axis(rosenbrock, 1, train_x)
    train_y = (train_y - train_y.mean())/ train_y.std() 
    
    # The random input
    input_ = random.uniform(-5, 10, d * size).reshape(size, d)
    
    # Initialise GP 
    gp = GaussianProcess.GaussianProcess(train_x, train_y, tau=1)
    
    # Learn hyperparameters with CPU 
    start = time.time()
    theta_min = gp.learn_hyperparameters(n_tries=2, gpu=False)
    end = time.time()
    cpu_time = end - start
    # Predict
    pred_mu_cpu, pred_var, par_dev = gp.predict(input_)
   

    # Learn hyperparameters with GPU
    start = time.time()
    theta_min = gp.learn_hyperparameters(n_tries=2, gpu=True)
    end = time.time()
    gpu_time = end - start
    # Predict
    pred_mu_gpu, pred_var, par_dev = gp.predict(input_)



    print(pred_mu_cpu - pred_mu_gpu)
    print("gpu time: " + gpu_time)
    print("cpu time: " + cpu_time)
Пример #6
0
def perband_emulators ( emulators, band_pass ):
    """This function creates per band emulators from the full-spectrum
    emulator. Should be faster in many cases"""
    
    n_bands = band_pass.shape[0]
    x_train_pband = [ emulators.X_train[:,band_pass[i,:]].mean(axis=1) \
        for i in xrange( n_bands ) ]
    x_train_pband = np.array ( x_train_pband )
    emus = []
    # add get_testing_val 
    #GaussianProcess.get_testing_val = MethodType(get_testing_val, None, GaussianProcess)

    for i in xrange( n_bands ):
        gp = GaussianProcess ( emulators.y_train[:]*1, \
                x_train_pband[i,:] )
        print 'p1', (emulators.y_train[:]*1).shape, 'p2', x_train_pband[i,:].shape
        gp.learn_hyperparameters ( n_tries=5 )
        emus.append ( gp )
    return emus
Пример #7
0
def test_rosenbrock_gp():
    size = 30
    d = 5
    train_x = random.uniform(-5, 10, d * size).reshape(size, d)
    train_y = np.apply_along_axis(rosenbrock, 1, train_x)
    train_y = (train_y - train_y.mean()) / train_y.std()

    gp = GaussianProcess.GaussianProcess(train_x, train_y)

    start = time.time()
    print(gp.learn_hyperparameters(n_tries=2, gpu=True))
    end = time.time()
    print("GPU time: ")
    print(end - start)

    gp = GaussianProcess.GaussianProcess(train_x, train_y)
    print("CPU time: ")
    start = time.time()
    print(gp.learn_hyperparameters(n_tries=2, gpu=False))
    end = time.time()
    print(end - start)
Пример #8
0
def test_gpu_set_param(size, d):
    train_x = random.uniform(-5, 10, d * size).reshape(size, d)
    train_y = np.apply_along_axis(rosenbrock, 1, train_x)
    train_y = (train_y - train_y.mean()) / train_y.std()

    gp = GaussianProcess.GaussianProcess(train_x, train_y)
    theta = np.random.rand(gp.D + 2)

    gp.theta = theta

    start = time.time()
    gp._set_params(gp.theta, gpu=False)
    end = time.time()

    invQt1 = gp.invQt
    invQ1 = gp.invQ
    Z1 = gp.Z
    Q1 = gp.Q
    logdetQ1 = gp.logdetQ
    cpu_time = end - start

    gp = GaussianProcess.GaussianProcess(train_x, train_y)
    gp.theta = theta
    start = time.time()
    gp._set_params(gp.theta, gpu=True)
    end = time.time()
    gpu_time = end - start

    invQt2 = gp.invQt
    invQ2 = gp.invQ
    Z2 = gp.Z
    Q2 = gp.Q
    logdetQ2 = gp.logdetQ

    print(
        str(size) + ', ' + str(d) + ', ' + str(cpu_time) + ', ' +
        str(gpu_time) + ', ' + str(cpu_time / gpu_time) + ', ' +
        str(np.max(np.abs(invQt1 - invQt2))) + ', ' +
        str(np.max(np.abs(invQ1 - invQ2))) + ', ' + str(logdetQ2 - logdetQ1))
Пример #9
0
if __name__ == '__main__':

    GaussianProcess.set_testing_val = MethodType(set_testing_val, None,
                                                 GaussianProcess)
    print 'Problem_size\tCPU time\tGPU time\tSpeedup\tStatus'
    print '-----------------------------'

    for Npredict in xrange(np.int(1e5), np.int(1e6), np.int(1e5)):
        Ntrain = 250
        Ninputs = 10

        inputs = np.random.random((Ntrain, Ninputs))
        testing = np.random.random((Npredict, Ninputs))

        gp = GaussianProcess(inputs, [])
        gp.set_testing_val(Ninputs, Npredict, Ntrain)

        #CPU predict
        start = time.time()
        [mu_c, var_c, deriv_c] = gp.predict(testing, is_gpu=False)
        end = time.time()
        cputime = end - start

        #GPU predict
        start = time.time()
        [mu_g, var_g, deriv_g] = gp.predict(testing,
                                            is_gpu=True,
                                            precision=np.float32,
                                            threshold=1e5)
        end = time.time()
Пример #10
0

if __name__ == '__main__':

    GaussianProcess.set_testing_val = MethodType(set_testing_val, None,
                                                 GaussianProcess)
    GaussianProcess.predict_benchmark = MethodType(predict_benchmark, None,
                                                   GaussianProcess)
    print 'Problem_size\tCPU time\tGPU time\tSpeedup\tStatus'
    print '-----------------------------'

    for i in xrange(10):

        Npredict = np.int(1e6)
        Ntrain = 250
        Ninputs = 10

        inputs = np.random.random((Ntrain, Ninputs))
        testing = np.random.random((Npredict, Ninputs))

        gp = GaussianProcess(inputs, [])
        gp.set_testing_val(Ninputs, Npredict, Ntrain)

        #CPU predict
        start = time.time()
        [mu_c, var_c, deriv_c] = gp.predict_benchmark(testing)
        end = time.time()
        cputime = end - start
        print 'cputime', cputime
        print '=================='
Пример #11
0
def create_emulators ( sun_angles, x_min, x_max, n_train=250, n_validate=1000 ):
    """This is a helper function to create emulators from the 2stream model.
    The emulators operate on all parameters (7) and the user needs to provide
    a numer of `sun_angles`. This then allows the inversion of BHR data from e.g.
    MODIS MCD43. We need to specify minimum and maximum boundaries for the 
    parameters (`x_min`, `x_max`). The number of training samples (`n_train`) 
    is set to 250 as this results in a very good emulation while still 
    being reasonably fast. The validation on 1000 randomly drawn parameters
    will be reported too. The output will be one dictionary, indexed by
    sun angle, one for VIS and one for NIR.
    
    """
    
    from gp_emulator import GaussianProcess, lhd
    import scipy.stats as ss

    n_params = x_min.size
    ## Putting boundaries on parameter space is useful
    #x_min = np.array ( 7*[ 0.001,] )
    #x_max = np.array ( 7*[0.95, ] )
    #x_max[-1] = 10.
    #x_min[1] = 0.
    #x_max[1] = 5.
    #x_min[4] = 0.
    #x_max[4] = 5.
    # First we create the sampling space for the emulators. In the
    # absence of any further information, we assume a uniform 
    # distribution between x_min and (x_max - x_min):
    dist = []
    for k in xrange( n_params ):
        dist.append ( ss.uniform ( loc=x_min[k], \
                              scale = x_max[k] - x_min[k] ) )
    # The training dataset is obtaiend by a LatinHypercube Design
    x_train = lhd(dist=dist, size=n_train )
    # The validation dataset is randomly drawn from within the 
    # parameter boundaries
    x_validate = np.random.rand (  n_validate, n_params  )*(x_max - x_min) + \
        x_min
    emu_vis = {}
    emu_nir = {}
    # We next loop over the input sun angles
    for sun_angle in sun_angles:
        # If we've done this sun angle before, skip it
        if not emu_vis.has_key ( sun_angle ):
            albedo_train = []
            albedo_validate = []
            # The following loop creates the validation dataset
            for i in xrange( n_validate ):
                [a_vis, a_nir] = two_stream_model ( x_validate[i,:], \
                    sun_angle )
                albedo_validate.append ( [a_vis, a_nir] )
            # The following loop creates the training dataset
            for i in xrange ( n_train ):
                [a_vis, a_nir] = two_stream_model ( x_train[i,:], \
                    sun_angle )
                albedo_train.append ( [a_vis, a_nir] )

            albedo_train = np.array ( albedo_train )
            albedo_validate = np.array ( albedo_validate )
            # The next few lines create and train the emulators
            # GP for visible
            gp_vis = GaussianProcess ( x_train, albedo_train[:,0])
            theta = gp_vis.learn_hyperparameters(n_tries=4)
            
            # GP for NIR
            gp_nir = GaussianProcess ( x_train, albedo_train[:,1])
            theta = gp_nir.learn_hyperparameters(n_tries=4)
            pred_mu, pred_var, par_dev = gp_vis.predict ( x_validate )
            r_vis = (albedo_validate[:,0] - pred_mu)
            pred_mu, pred_var, par_dev = gp_nir.predict ( x_validate )
            r_nir = (albedo_validate[:,1] - pred_mu)
            # Report some goodness of fit. Could do with more
            # stats, but for the time being, this is enough.
            print "Sun Angle: %g, RMSE VIS: %g, RMSE NIR: %g" % \
                ( sun_angle, r_vis.std(), r_nir.std() )
            emu_vis[sun_angle] = gp_vis
            emu_nir[sun_angle] = gp_nir
    emulators = {}
    for sun_angle in emu_vis.iterkeys():
        emulators[sun_angle] = [ emu_vis[sun_angle], emu_nir[sun_angle] ]
    return emulators
Пример #12
0

if __name__ == '__main__':

    GaussianProcess.set_testing_val = MethodType(set_testing_val, None, GaussianProcess)
    print 'Problem_size\tCPU time\tGPU time\tSpeedup\tStatus'
    print '-----------------------------'
    
    for Npredict in xrange(np.int(1e5), np.int(1e6), np.int(1e5)):
        Ntrain = 250
        Ninputs = 10

        inputs = np.random.random(( Ntrain, Ninputs))
        testing = np.random.random(( Npredict, Ninputs))
        
        gp = GaussianProcess(inputs, [])
        gp.set_testing_val(Ninputs, Npredict, Ntrain)

        #CPU predict
    	start = time.time()
    	[mu_c, var_c, deriv_c] = gp.predict(testing, is_gpu=False )
    	end = time.time()
    	cputime = end -start
    	
        #GPU predict
        start = time.time()
        [mu_g, var_g, deriv_g] = gp.predict(testing, is_gpu = True, precision = np.float32, threshold = 1e5)
    	end =time.time()
    	gputime = end - start
        print "%d\t%.2fs\t%.2fs\t%.2fx\t" % (Npredict, cputime, gputime, cputime/gputime),
Пример #13
0
        return mu, deriv


if __name__ == '__main__':

    GaussianProcess.set_testing_val = MethodType(set_testing_val, None, GaussianProcess)
    GaussianProcess.predict_benchmark = MethodType(predict_benchmark, None, GaussianProcess)
    print 'Problem_size\tCPU time\tGPU time\tSpeedup\tStatus'
    print '-----------------------------'
    
    for i in xrange(10):
        
        Npredict = np.int(1e6)
	Ntrain = 250
        Ninputs = 10

        inputs = np.random.random(( Ntrain, Ninputs))
        testing = np.random.random(( Npredict, Ninputs))
        
        gp = GaussianProcess(inputs, [])
        gp.set_testing_val(Ninputs, Npredict, Ntrain)

        #CPU predict
    	start = time.time()
    	[mu_c, var_c, deriv_c] = gp.predict_benchmark(testing )
    	end = time.time()
    	cputime = end -start
	print 'cputime', cputime
	print '=================='