Skip to content

Commit

Permalink
fix flat_model in Scube
Browse files Browse the repository at this point in the history
  • Loading branch information
Bink98 committed Jul 13, 2023
1 parent 39bbee4 commit f0c14b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ __pycache__/
# Distribution / packaging
.Python
build/
generated/
develop-eggs/
dist/
downloads/
Expand Down
17 changes: 5 additions & 12 deletions SPACEL/Scube/gpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def prepare_gpr_model(self, lengthscale_prior=None,outputscale_prior=None,noise_
noise_prior=noise_prior
)
self.flat_model = ExactGPModel(self.train_x, self.train_y, likelihood, lengthscale_prior=lengthscale_prior,outputscale_prior=outputscale_prior)
# self.init_model(self.flat_model,lengthscale=torch.tensor(99999999))
self.init_model(self.flat_model,lengthscale=torch.inf)
# self.init_model(self.flat_model,lengthscale=torch.tensor(99999))
self.init_model(self.flat_model,lengthscale=torch.tensor(1000))
else:
if self.model is None:
likelihood = gpytorch.likelihoods.GaussianLikelihood(
Expand Down Expand Up @@ -148,8 +148,8 @@ def train_single_model(self,model,lr=1,training_iter=500,save=False,save_path=No

# "Loss" for GPs - the marginal log likelihood
mll = gpytorch.mlls.ExactMarginalLogLikelihood(model.likelihood, model)
best_loss = None
best_model_state = None
best_loss = np.inf
best_model_state = model.state_dict()
if optimize_method=='Adam':
optimizer = torch.optim.Adam(model.parameters(),lr=lr)
for i in range(training_iter):
Expand All @@ -158,10 +158,7 @@ def train_single_model(self,model,lr=1,training_iter=500,save=False,save_path=No
loss = -mll(output, self.train_y)
loss.backward()
optimizer.step()
if best_loss is None:
best_loss = loss.item()
if best_model_state is None:
best_model_state = model.state_dict()
print(loss.item())
if loss.item() < best_loss:
best_model_state = model.state_dict()
best_loss = loss.item()
Expand All @@ -177,10 +174,6 @@ def closure():
return loss
for i in range(training_iter):
loss = optimizer.step(closure)
if best_loss is None:
best_loss = loss.item()
if best_model_state is None:
best_model_state = model.state_dict()
if loss.item() < best_loss:
best_model_state = model.state_dict()
best_loss = loss.item()
Expand Down

0 comments on commit f0c14b1

Please sign in to comment.