示例#1
0
文件: fittedgp.py 项目: kiminh/HBBS
    class Agent(agents.random.RandomAgent(epochs)):

        def __init__(self, *args):
            super().__init__(*args)
            self.embed = Featurizer(self.encode, dim=dim, alpha=5e-4, shape=self.shape, lam=0., minibatch=100)
        
        def act(self, seqs):
            if not self.seen.items():
                return sample(seqs, self.batch)
            seqs = np.array(seqs)
            X, Y = map(np.array, zip(*self.seen.items()))
            model = FittedGP(self.embed(X), Y)
            model.fit(epochs=epochs)
            mu, cov = model.predict_(self.embed(seqs))
            mvn = MultivariateNormal(torch.tensor(mu), covariance_matrix=torch.tensor(cov) + 1e-4 * torch.eye(cov.shape[0])) 
            mask = np.array([False for _ in seqs])
            choices = []
            for i in range(self.batch):
                samp = mvn.sample().data.numpy()
                low = samp.min()
                samp[mask] = low
                idx = np.argmax(samp)
                mask[idx] = True
                choices.append(seqs[idx])
            return choices

        def observe(self, data):
            super().observe(data)
            self.embed.fit(*zip(*self.seen.items()), epochs=epochs)
示例#2
0
文件: fittedgp.py 项目: kiminh/HBBS
    class Agent(agents.random.RandomAgent(epochs)):

        def __init__(self, *args):
            super().__init__(*args)
            self.embed = Featurizer(self.encode, dim=dim, alpha=5e-4, shape=self.shape, lam=0., minibatch=100)
            self.beta = beta
        
        def act(self, seqs):
            if not self.seen.items():
                return sample(seqs, self.batch)
            seqs = np.array(seqs)
            X, Y = map(np.array, zip(*self.seen.items()))
            choices = []
            mu = None
            model = FittedGP(self.embed(X), Y)
            model.fit(epochs=epochs)
            while len(choices) < self.batch:
                mu_, sigma = model.predict(self.embed(seqs))
                if mu is None:
                    mu = mu_
                ucb = mu + np.sqrt(self.beta) * sigma
                selected = np.argsort(ucb)[-mb:]
                choices += list(seqs[selected])
                X = np.concatenate((X, seqs[selected]))
                Y = np.concatenate((Y, mu[selected]))
                seqs = np.delete(seqs, selected)
                mu = np.delete(mu, selected)
                model.update(self.embed(X), Y)
            return choices[:self.batch]

        def observe(self, data):
            super().observe(data)
            self.embed.fit(*zip(*self.seen.items()), epochs=epochs)
 def __init__(self, *args):
     super().__init__(*args)
     self.embed = Featurizer(self.encode,
                             dim=dim,
                             alpha=5e-4,
                             shape=self.shape,
                             lam=0.,
                             minibatch=100)
     if len(self.prior):
         self.embed.fit(*zip(*self.prior.items()),
                        epochs=initial_epochs)
示例#4
0
    class Agent(agents.random.RandomAgent(epochs)):

        def __init__(self, *args):
            super().__init__(*args)
            self.model = Featurizer(self.encode, shape=self.shape, dim=dim)
        
        def act(self, seqs):
            selections = np.array(list(zip(*sorted(zip(self.model.predict(seqs), seqs))[-int(k * self.batch):]))[1])
            idx = utils.mcmc.mcmc(self.batch, self.model.embed(selections),
                            iters=1000) if k > 1. else np.arange(len(selections))
            return selections[idx]

        def observe(self, data):
            super().observe(data)
            self.model.fit(*zip(*self.seen.items()), epochs=epochs) 
示例#5
0
 def __init__(self,
              encoder,
              dim,
              shape,
              alpha=5e-4,
              prior=(0.5, 10, 1, 1),
              eps=0.,
              rho=0.,
              k=100,
              minibatch=100):
     '''encoder: convert sequences to one-hot arrays.
     alpha: embedding learning rate.
     shape: sequence shape (len, channels)
     dim: embedding dimensionality
     prior: (mu0, n0, alpha, beta) prior over gamma and gaussian bucket score distributions.
     eps: epsilon for greedy maximization step
     rho: portion of steps to use inverse gamma conjugate instead of normal gamma
     k: cluster count or method
     '''
     super().__init__()
     self.X, self.Y = (), ()
     self.embed = Featurizer(encoder,
                             shape,
                             dim=dim,
                             alpha=alpha,
                             minibatch=minibatch)
     self.prior = prior
     self.eps = eps
     self.rho = rho
     self.k = k
 def __init__(self,
              encoder,
              dim,
              shape,
              alpha=5e-4,
              beta=0.05,
              lam=0,
              mu=0.5,
              sigma=0.5,
              eps=1e-4,
              tau=1.,
              minibatch=100,
              gpbatch=5000):
     '''encoder: convert sequences to one-hot arrays.
     alpha: embedding learning rate.
     shape: sequence shape (len, channels)
     dim: embedding dimensionality
     lam: l2 regularization constant
     mu: GP prior mean
     sigma: GP prior standard deviation
     tau: kernel covariance parameter
     beta: GP hyperparameter fitting rate
     eps: noise
     '''
     super().__init__()
     self.X, self.Y = (), ()
     self.minibatch = gpbatch
     self.embed = Featurizer(encoder,
                             dim=dim,
                             alpha=alpha,
                             shape=shape,
                             lam=lam,
                             minibatch=minibatch)
     self.mu = mu
     self.sigma = sigma
     self.tau = torch.tensor(tau,
                             requires_grad=True,
                             device=self.embed.device,
                             dtype=torch.double)
     self.mu = torch.tensor(mu,
                            requires_grad=True,
                            device=self.embed.device,
                            dtype=torch.double)
     self.sigma = torch.tensor(sigma,
                               requires_grad=True,
                               device=self.embed.device,
                               dtype=torch.double)
     self.eps = torch.tensor(eps,
                             requires_grad=True,
                             device=self.embed.device,
                             dtype=torch.double)
     self.opt = torch.optim.Adam([self.tau, self.mu, self.sigma, self.eps],
                                 lr=beta)
示例#7
0
 def __init__(self, *args):
     super().__init__(*args)
     self.model = Featurizer(self.encode, shape=self.shape, dim=dim)
示例#8
0
文件: fittedgp.py 项目: kiminh/HBBS
 def __init__(self, *args):
     super().__init__(*args)
     self.embed = Featurizer(self.encode, dim=dim, alpha=5e-4, shape=self.shape, lam=0., minibatch=100)