Пример #1
0
    def __init__(self,
                 num_dimensions,
                 user_mat,
                 first_mat,
                 gparams,
                 variance_prior=None,
                 offset_prior=None,
                 active_dims=None):
        super(MyKernel, self).__init__(active_dims=active_dims)
        self.user_mat = user_mat
        self.first_mat = first_mat

        self.psi_dim_one = gparams.psi_indices[0]
        self.psi_dim_two = gparams.psi_indices[1]
        self.psi_indices = gparams.psi_indices
        #print(self.psi_dim_one)
        #print(self.psi_dim_two)

        self.init_u1 = gparams.sigma_u[0][0]
        #init_u1 = gparams.u1

        self.init_u2 = gparams.sigma_u[1][1]
        #init_u2 = gparams.u2

        #self.register_parameter(name="u1", parameter=torch.nn.Parameter(init_u1*torch.tensor(1.0)))
        self.register_parameter(name="raw_u1",
                                parameter=torch.nn.Parameter(
                                    self.init_u1 * torch.tensor(1.0)))

        #self.register_parameter(name="u2", parameter=torch.nn.Parameter(init_u2*torch.tensor(1.0)))
        self.register_parameter(name="raw_u2",
                                parameter=torch.nn.Parameter(
                                    self.init_u2 * torch.tensor(1.0)))
        t = gparams.sigma_u[0][0]**.5 * gparams.sigma_u[1][1]**.5
        self.r = (gparams.sigma_u[0][1] + t) / t
        #r = gparams.rho_term
        #self.register_parameter(name="rho", parameter=torch.nn.Parameter(r*torch.tensor(1.0)))
        self.register_parameter(name="raw_rho",
                                parameter=torch.nn.Parameter(
                                    self.r * torch.tensor(1.0)))

        #initial_value=init_u2*torch.tensor(1.0)
        #initial_value=init_u1*torch.tensor(1.0)
        self.register_constraint(
            "raw_u1",
            constraint=constraints.Positive(initial_value=self.init_u1 *
                                            torch.tensor(1.0)))
        #initial_value=init_u2*torch.tensor(1.0)
        self.register_constraint(
            "raw_u2",
            constraint=constraints.Positive(initial_value=self.init_u2 *
                                            torch.tensor(1.0)))

        self.register_constraint("raw_rho",
                                 constraint=constraints.Interval(0.0, 2.0))
 def __init__(self, num_dimensions,user_mat, first_mat,gparams, variance_prior=None, offset_prior=None, active_dims=None):
     super(MyKernel, self).__init__(active_dims=active_dims)
     self.user_mat = user_mat
     self.first_mat = first_mat
     self.action_indices = [8,13]
     self.psi_dim_one = gparams.psi_indices[0]
     self.psi_dim_two = gparams.psi_indices[1]
     self.psi_indices =gparams.psi_indices
     print(self.psi_indices)
     self.g_indices = [i for i in range(8)]
     self.action_indices_one = [i for i in range(8,8+5)]
     self.action_indices_two = [i for i in range(8+5,18)]
     self.init_u1 = gparams.sigma_u[0][0]
     
     
     self.init_u2 = gparams.sigma_u[1][1]
     
     
     
     self.register_parameter(name="raw_u1", parameter=torch.nn.Parameter(1.0*torch.tensor(1.0)))
     self.register_parameter(name="test", parameter=torch.nn.Parameter(1.0*torch.tensor(1.0)))
     
     
     self.register_parameter(name="raw_u2", parameter=torch.nn.Parameter(1.0*torch.tensor(1.0)))
     t =gparams.sigma_u[0][0]**.5 * gparams.sigma_u[1][1]**.5
     self.r = (gparams.sigma_u[0][1]+t)/t
     
     self.register_parameter(name="raw_rho", parameter=torch.nn.Parameter(1.0*torch.tensor(1.0)))
     
     
     self.register_constraint("raw_u1",constraint= constraints.Positive())
     
     self.register_constraint("raw_u2",constraint= constraints.Positive())
         
     self.register_constraint("raw_rho",constraint= constraints.Interval(0.0,2.0))
     self.u1 = self.init_u1
     self.u2 = self.init_u2
     self.rho = self.r
    def __init__(self,
                 num_dimensions,
                 user_mat,
                 first_mat,
                 gparams,
                 variance_prior=None,
                 offset_prior=None,
                 active_dims=None):
        super(MyKernel, self).__init__(active_dims=active_dims)
        self.user_mat = user_mat
        self.first_mat = first_mat

        self.psi_dim_one = gparams.psi_indices[0]
        self.psi_dim_two = gparams.psi_indices[1]
        self.psi_indices = gparams.psi_indices
        self.action_indices_one = gparams.action_indices_one
        self.action_indices_two = gparams.action_indices_two
        self.g_indices = gparams.g_indices
        #print(self.psi_dim_one)
        #print(self.psi_dim_two)

        #init_u1 = gparams.sigma_u[0][0]

        print(gparams.u1)
        print(gparams.u2)
        self.init_u1 = gparams.u1 * torch.tensor(1.0)

        #init_u2 = gparams.sigma_u[1][1]
        self.init_u2 = gparams.u2 * torch.tensor(1.0)
        self.init_u3 = gparams.u3 * torch.tensor(1.0)
        self.init_u4 = gparams.u4 * torch.tensor(1.0)

        self.r12 = gparams.r12 * torch.tensor(1.0)
        self.r13 = gparams.r13 * torch.tensor(1.0)
        self.r14 = gparams.r14 * torch.tensor(1.0)

        self.r23 = gparams.r23 * torch.tensor(1.0)
        self.r24 = gparams.r24 * torch.tensor(1.0)

        self.r34 = gparams.r34 * torch.tensor(1.0)

        #self.register_parameter(name="u1", parameter=torch.nn.Parameter(init_u1*torch.tensor(1.0)))
        self.register_parameter(name="raw_u1",
                                parameter=torch.nn.Parameter(
                                    self.init_u1 * torch.tensor(1.0)))

        #self.register_parameter(name="u2", parameter=torch.nn.Parameter(init_u2*torch.tensor(1.0)))
        self.register_parameter(name="raw_u2",
                                parameter=torch.nn.Parameter(
                                    self.init_u2 * torch.tensor(1.0)))

        #self.register_parameter(name="u3", parameter=torch.nn.Parameter(init_u3*torch.tensor(1.0)))
        self.register_parameter(name="raw_u3",
                                parameter=torch.nn.Parameter(
                                    self.init_u3 * torch.tensor(1.0)))

        #self.register_parameter(name="u4", parameter=torch.nn.Parameter(init_u4*torch.tensor(1.0)))
        self.register_parameter(name="raw_u4",
                                parameter=torch.nn.Parameter(
                                    self.init_u4 * torch.tensor(1.0)))

        #t =gparams.sigma_u[0][0]**.5 * gparams.sigma_u[1][1]**.5
        #r = (gparams.sigma_u[0][1]+t)/t
        #sr = gparams.rho_term
        #self.register_parameter(name="rho_12", parameter=torch.nn.Parameter(r12*torch.tensor(1.0)))
        self.register_parameter(name="raw_rho_12",
                                parameter=torch.nn.Parameter(
                                    self.r12 * torch.tensor(1.0)))

        #self.register_parameter(name="rho_13", parameter=torch.nn.Parameter(r13*torch.tensor(1.0)))
        self.register_parameter(name="raw_rho_13",
                                parameter=torch.nn.Parameter(
                                    self.r13 * torch.tensor(1.0)))

        #self.register_parameter(name="rho_14", parameter=torch.nn.Parameter(r14*torch.tensor(1.0)))
        self.register_parameter(name="raw_rho_14",
                                parameter=torch.nn.Parameter(
                                    self.r14 * torch.tensor(1.0)))

        #self.register_parameter(name="rho_23", parameter=torch.nn.Parameter(r23*torch.tensor(1.0)))
        self.register_parameter(name="raw_rho_23",
                                parameter=torch.nn.Parameter(
                                    self.r23 * torch.tensor(1.0)))

        #self.register_parameter(name="rho_24", parameter=torch.nn.Parameter(r24*torch.tensor(1.0)))
        self.register_parameter(name="raw_rho_24",
                                parameter=torch.nn.Parameter(
                                    self.r24 * torch.tensor(1.0)))

        #self.register_parameter(name="rho_34", parameter=torch.nn.Parameter(r34*torch.tensor(1.0)))
        self.register_parameter(name="raw_rho_34",
                                parameter=torch.nn.Parameter(
                                    self.r34 * torch.tensor(1.0)))

        self.register_constraint("raw_u1", constraint=constraints.Positive())
        self.register_constraint("raw_u2", constraint=constraints.Positive())
        self.register_constraint("raw_u3", constraint=constraints.Positive())
        self.register_constraint("raw_u4", constraint=constraints.Positive())

        self.register_constraint("raw_rho_12",
                                 constraint=constraints.Interval(0.0, 2.0))
        self.register_constraint("raw_rho_13",
                                 constraint=constraints.Interval(0.0, 2.0))
        self.register_constraint("raw_rho_14",
                                 constraint=constraints.Interval(0.0, 2.0))
        self.register_constraint("raw_rho_23",
                                 constraint=constraints.Interval(0.0, 2.0))
        self.register_constraint("raw_rho_24",
                                 constraint=constraints.Interval(0.0, 2.0))
        self.register_constraint("raw_rho_34",
                                 constraint=constraints.Interval(0.0, 2.0))
        #print('got here')

        self.u1 = gparams.u1
        #print('initialized')
        #init_u2 = gparams.sigma_u[1][1]
        self.u2 = gparams.u2
        #print('initialized')
        self.u3 = gparams.u3

        self.u4 = gparams.u4

        self.rho_12 = gparams.r12

        self.rho_13 = gparams.r13

        self.rho_14 = gparams.r14

        self.rho_23 = gparams.r23

        self.rho_24 = gparams.r24

        self.rho_34 = gparams.r34
Пример #4
0
def run(X, users, y, global_params):
    #initial_u1,initial_u2,initial_rho,initial_noise,baseline_indices,psi_indices,user_index
    torch.manual_seed(111)
    #np.random.seed(111)
    user_mat = get_users(users, users)
    #print(user_mat.shape)
    #print(X.shape)
    #print(global_params.baseline_indices)
    first_mat = get_first_mat(np.eye(len(global_params.baseline_indices)), X,
                              global_params.baseline_indices)
    #print(first_mat.shape)
    with gpytorch.settings.fast_computations(log_prob=False, solves=False):

        test_constraint = constraints.Positive(
            initial_value=global_params.sigma_u[0][0] * torch.tensor(1.0))
        #print('upper bound {}'.format(test_constraint.upper_bound))
        likelihood = gpytorch.likelihoods.GaussianLikelihood()
        likelihood.noise_covar.initialize(noise=(global_params.noise_term) *
                                          torch.ones(1))
        #print('going on')
        #print((global_params.noise_term)*torch.ones(X.shape[0]))
        # likelihood = gpytorch.likelihoods.FixedNoiseGaussianLikelihood(noise=(1.0)*torch.ones(X.shape[0]), learn_additional_noise=True)
        #print('like worked')
        X = torch.from_numpy(np.array(X)).float()
        y = torch.from_numpy(y).float()
        #print(X.size())
        first_mat = torch.from_numpy(first_mat).float()
        user_mat = torch.from_numpy(user_mat).float()

        model = GPRegressionModel(X, y, likelihood, user_mat, first_mat,
                                  global_params)
        #print('first one {}'.format(model.covar_module.u1.item()))
        model.train()
        likelihood.train()
        sigma_u = None
        cov = None
        noise = None

        optimizer = torch.optim.Adam(
            [
                {
                    'params': model.parameters()
                },  # Includes GaussianLikelihood parameters
            ],
            lr=global_params.lr)

        mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model)
        #def train(num_iter):
        num_iter = 10
        with gpytorch.settings.use_toeplitz(False):
            for i in range(num_iter):
                try:

                    optimizer.zero_grad()
                    output = model(X)
                    #print(type(output))
                    loss = -mll(output, y)
                    loss.backward()

                    #print('Iter %d/%d - Loss: %.3f' % (i + 1, num_iter, loss.item()))
                    optimizer.step()
                    #print(get_sigma_u(model.covar_module.u1.item(),model.covar_module.u2.item(),model.covar_module.rho.item()))
                    #print(test_constraint.transform(model.covar_module.u1.item()*torch.tensor(1.0)))

                    sigma_temp = get_sigma_u(model.covar_module.u1.item(),
                                             model.covar_module.u2.item(),
                                             model.covar_module.rho.item())
                    ##print('linalg {}'.format(np.linalg.eig(sigma_temp)))

                    #print(sigma_temp)
                    eigs = np.linalg.eig(sigma_temp)
                    f_preds = model(X)
                    f_covar = f_preds.covariance_matrix
                    covtemp = f_covar.detach().numpy()
                    #print('noise two {}'.format(likelihood.second_noise_covar.noise.item()))
                    #print('noise two {}'.format(likelihood.noise_covar.noise))
                    if np.isreal(
                            sigma_temp).all() and not np.isnan(covtemp).all(
                            ) and eigs[0][0] > 0.0005 and eigs[0][1] > 0.0005:

                        sigma_u = sigma_temp
                        cov = covtemp
                        #print(np.isreal( covtemp))
                        #print(cov)
                        noise = likelihood.noise_covar.noise.item()
                    #noise = likelihood.second_noise_covar.noise.item()+1.0
                    #**.5
                    else:
                        #print(eigs)
                        break

                except Exception as e:
                    print(e)
                    print('here')
                    break
        one_test = True
        if i < 2:
            one_test = False
            likelihood = gpytorch.likelihoods.GaussianLikelihood()
            #likelihood = gpytorch.likelihoods.FixedNoiseGaussianLikelihood(noise=(1.0)*torch.ones(X.shape[0]), learn_additional_noise=True)

            likelihood.noise_covar.initialize(
                noise=(global_params.noise_term) * torch.ones(1))

            model = GPRegressionModel(X, y, likelihood, user_mat, first_mat,
                                      global_params)
            print('1 test')
            #t =global_params.sigma_u[0][0]**.5 * global_params.sigma_u[1][1]**.5
            #r = (global_params.sigma_u[0][1]+t)/t

            #model.covar_module.u1 =global_params.sigma_u[0][0]*torch.tensor(1.0)
            ##print('ok 1')
            #model.covar_module.u2 =global_params.sigma_u[1][1]*torch.tensor(1.0)
            #model.covar_module.rho =r*torch.tensor(1.0)
            ##print('ok 1')
            ##print(model.covar_module.u1.item())
            ##print(model.covar_module.u2.item())
            ##print(model.covar_module.rho.item())
            sigma_u = get_sigma_u(model.covar_module.u1.item(),
                                  model.covar_module.u2.item(),
                                  model.covar_module.rho.item())
            ##print('ok 2')
            noise = global_params.noise_term
            ##print('ok 3')
            #model.eval()
            #likelihood.eval()
            ##print('ok 4')
            f_preds = model(X)
            ##print('ok 5')
            f_covar = f_preds.covariance_matrix
            #print('ok 6')
            #print(f_covar)
            cov = f_covar.detach().numpy()
#print('ok 7')
##print('ok 6')
##print(cov.shape)

#train(50)
#model.eval()
# likelihood.eval()
#observed_pred = likelihood(model(X))
#print('o')
#print(likelihood(model(X)))
#print(likelihood(model(X)).mean.numpy())

#print('cov')
#print(cov)
    if one_test:
        sigma_u = get_sigma_u(model.covar_module.u1.item(),
                              model.covar_module.u2.item(),
                              model.covar_module.rho.item())
    return {
        'sigma_u': sigma_u,
        'cov': cov,
        'noise': noise,
        'like': 0,
        'iters': i
    }
def run(X,users,y,global_params):
    
    torch.manual_seed(111)

    user_mat= get_users(users,users)
   
    first_mat = get_first_mat(np.eye(len(global_params.baseline_indices)),X,global_params.baseline_indices)

    with gpytorch.settings.fast_computations(log_prob=False, solves=False):
        
        test_constraint = constraints.Positive(initial_value=global_params.sigma_u[0][0]*torch.tensor(1.0))
    
        likelihood = gpytorch.likelihoods.GaussianLikelihood()
        likelihood.noise_covar.initialize(noise=(global_params.noise_term)*torch.ones(1))
 
        X = torch.from_numpy(np.array(X)).float()
        y = torch.from_numpy(y).float()
  
        first_mat = torch.from_numpy(first_mat).float()
        user_mat = torch.from_numpy(user_mat).float()
    
        model = GPRegressionModel(X, y, likelihood,user_mat,first_mat,global_params)
      
        model.train()
        likelihood.train()
        sigma_u=None
        cov=None
        noise=None
    
        optimizer = torch.optim.Adam([
                                  {'params': model.parameters()},  # Includes GaussianLikelihood parameters
                                  ], lr=global_params.lr)
                                  
        mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model)

        num_iter=10
        with gpytorch.settings.use_toeplitz(False):
            for i in range(num_iter):
                try:
                   
                    optimizer.zero_grad()
                    output = model(X)
     
                    loss = -mll(output, y)
                    loss.backward()
                    
                   
                    optimizer.step()
                    
    
                    sigma_temp = get_sigma_u(model.covar_module.u1.item(),model.covar_module.u2.item(),model.covar_module.rho.item())
                    
                    eigs = np.linalg.eig(sigma_temp)
                    f_preds = model(X)
                    f_covar = f_preds.covariance_matrix
                    covtemp = f_covar.detach().numpy()
                    
                    if np.isreal(sigma_temp).all() and not np.isnan(covtemp).all() and eigs[0][0]>0.0005 and eigs[0][1]>0.0005:
                        
                        sigma_u = sigma_temp
                        cov=covtemp
                        
                        noise = likelihood.noise_covar.noise.item()
                    
                    else:
                   
                        break

                except Exception as e:
                    print(e)
    
        one_test = True
        if i<2:
            one_test=False
            likelihood = gpytorch.likelihoods.GaussianLikelihood()
        
            likelihood.noise_covar.initialize(noise=(global_params.noise_term)*torch.ones(1))
        
            model = GPRegressionModel(X, y, likelihood,user_mat,first_mat,global_params)
           
            sigma_u = get_sigma_u(model.covar_module.u1.item(),model.covar_module.u2.item(),model.covar_module.rho.item())

            noise =global_params.noise_term
        
            f_preds = model(X)
   
            f_covar = f_preds.covariance_matrix
        
            cov = f_covar.detach().numpy()

    if one_test:
        sigma_u = get_sigma_u(model.covar_module.u1.item(),model.covar_module.u2.item(),model.covar_module.rho.item())
    return {'sigma_u':sigma_u,'cov':cov,'noise':noise,'like':0,'iters':i}