def __init__(self, x_range, dx, z, k, n, sigma):
        self.dx = dx
        self.z = z
        self.k = k
        self.sigma = sigma
        self.x_range = x_range
        kern = DenseL1Kernel(self.z, self.k)

        X = np.random.uniform(x_range[0], x_range[1], (n, dx))
        kxx = kern(X) + sigma**2 * np.eye(X.shape[0])
        y = np.random.multivariate_normal(np.zeros(n), kxx).T

        self.gp = DenseKernelGP(X, y, sigma=sigma, kern=kern)
        self.gp.fit()
        self.get_max()
def generate_rp_tilecoding_gps(X,
                               y,
                               sigma,
                               tilecoding,
                               random_proj,
                               include_sparse=True,
                               include_dense=True):
    # create dense format tilecoding projector
    densephi = SparseRPTilecoding(tilecoding,
                                  random_proj=random_proj,
                                  normalize=True,
                                  output_dense=True)

    # create dense kernel based off tilecoding
    densekern = DenseKernel(tilecoding, normalize=True)

    # create sparse kernel based off tilecoding
    sparsekern = SparseKernel(tilecoding, normalize=True)

    gps = []

    if include_dense:
        df_gp = DenseFeatureGP(X, y, sigma=sigma, phi=densephi)
        dk_gp = DenseKernelGP(X, y, sigma=sigma, kern=densekern)
        gps = gps + [('Dense Feature GP', df_gp), ('Dense Kernel GP', dk_gp)]
    if include_sparse:
        sk_gp = SparseKernelGP(X, y, sigma=sigma, kern=sparsekern)
        gps = gps + [('Sparse Kernel GP', sk_gp)]
    return gps
def generate_tilecoding_gps(X,
                            y,
                            sigma,
                            tilecoding,
                            include_sparse=True,
                            include_dense=True):
    # create sparse format tilecoding projector
    sparsephi = IndexToBinarySparse(tilecoding, normalize=True)

    # create dense format tilecoding projector
    densephi = IndexToDense(tilecoding, normalize=True)

    # create dense kernel based off tilecoding
    densekern = DenseKernel(tilecoding, normalize=True)

    # create sparse kernel based off tilecoding
    sparsekern = SparseKernel(tilecoding, normalize=True)

    gps = []

    if include_dense:
        df_gp = DenseFeatureGP(X, y, sigma=sigma, phi=densephi)
        dk_gp = DenseKernelGP(X, y, sigma=sigma, kern=densekern)
        gps = gps + [('Dense Feature GP', df_gp), ('Dense Kernel GP', dk_gp)]
    if include_sparse:
        sf_gp = SparseFeatureGP(X, y, sigma=sigma, phi=sparsephi)
        sk_gp = SparseKernelGP(X, y, sigma=sigma, kern=sparsekern)
        gps = gps + [('Sparse Feature GP', sf_gp), ('Sparse Kernel GP', sk_gp)]
    return gps
class SampledGpFunc(object):
    def __init__(self, x_range, dx, z, k, n, sigma):
        self.dx = dx
        self.z = z
        self.k = k
        self.sigma = sigma
        self.x_range = x_range
        kern = DenseL1Kernel(self.z, self.k)

        X = np.random.uniform(x_range[0], x_range[1], (n, dx))
        kxx = kern(X) + sigma**2 * np.eye(X.shape[0])
        y = np.random.multivariate_normal(np.zeros(n), kxx).T

        self.gp = DenseKernelGP(X, y, sigma=sigma, kern=kern)
        self.gp.fit()
        self.get_max()

    def get_max(self):
        x = self.x_range[0].copy()
        all_cat = np.unique(self.z)
        for a in all_cat:
            active = self.z == a
            k1 = DenseL1Kernel(self.z[active], self.k[active])
            af = lambda x: np.array(k1(x, self.gp.X[:, active])).dot(self.gp.
                                                                     alpha)
            x[active] = np.squeeze(
                global_minimize(af, self.x_range[:, active], 10000))

        self.argmax = x
        self.f_max = -np.squeeze(
            np.array(self.gp.kern(x, self.gp.X)).dot(self.gp.alpha))

    def __call__(self, x):
        if x.ndim == 1:
            n = 1
        else:
            n = x.shape[0]
        kXn = np.array(self.gp.kern(x, self.gp.X))
        mu = kXn.dot(self.gp.alpha)
        f = mu
        f += np.random.normal(size=n) * self.sigma
        return -np.squeeze(f)
예제 #5
0
    def get_tilegp(self):
        nlayers = self.options['nlayers']
        indices = []
        ntiles = []
        hashing = None
        all_cat = np.unique(self.z)
        if self.tilecap:
            hashing_mem = self.tilecap / len(all_cat) / nlayers
            hashing = [rp.UNH(hashing_mem) for _ in xrange(len(all_cat))]
        for a in all_cat:
            inds = helper.find(self.z == a)
            indices.append(inds)
            ntiles.append(self.k[inds])

        phi = TileCoding(
            input_indices=indices,
            # ntiles = input dim x number of layers x tilings
            ntiles=ntiles,
            ntilings=[nlayers] * len(indices),
            hashing=hashing,
            state_range=self.x_range,
            rnd_stream=np.random,
            bias_term=False)

        if self.gp_type == 'sk':
            # densekern > sparsekern \approx sparserp
            sparsekern = SparseKernel(phi, normalize=True)
            gp = SparseKernelGP(self.X, self.y, sigma=0.1, kern=sparsekern)
        elif self.gp_type == 'sf':
            # too slow
            sparsephi = IndexToBinarySparse(phi, normalize=True)
            gp = SparseFeatureGP(self.X, self.y, sigma=0.1, phi=sparsephi)

        elif self.gp_type == 'dk':
            densekern = DenseKernel(phi, normalize=True)
            gp = DenseKernelGP(self.X,
                               self.y,
                               sigma=self.sigma,
                               kern=densekern)
        else:
            random_proj = rp.sparse_random_matrix(300,
                                                  phi.size,
                                                  random_state=np.random)
            densephi = SparseRPTilecoding(phi,
                                          random_proj=random_proj,
                                          normalize=True,
                                          output_dense=True)
            gp = DenseFeatureGP(self.X, self.y, sigma=self.sigma, phi=densephi)

        gp.fit()
        return gp
 def get_l1gp(self):
     kern = DenseL1Kernel(self.z, self.k)
     gp = DenseKernelGP(self.X, self.y, sigma=self.sigma, kern=kern)
     gp.fit()
     return gp