Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding complete forward and backward pass for ferminet pretraining #3553

Merged
merged 21 commits into from Oct 2, 2023

Conversation

shaipranesh2
Copy link
Contributor

Description

Fix #(issue)

Type of change

Please check the option that is related to your PR.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
    • In this case, we recommend to discuss your modification on GitHub issues before creating the PR
  • Documentations (modification for documents)

Checklist

  • My code follows the style guidelines of this project
    • Run yapf -i <modified file> and check no errors (yapf version must be 0.32.0)
    • Run mypy -p deepchem and check no errors
    • Run flake8 <modified file> --count and check no errors
    • Run python -m doctest <modified file> and check no errors
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New unit tests pass locally with my changes
  • I have checked my code and corrected any misspellings

@@ -71,6 +65,8 @@ def __init__(self,

Attributes
----------
running_diff: torch.Tensor
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an attribute tensor to keep track of running sum of MSELoss

@@ -148,6 +145,30 @@ def forward(self, input) -> torch.Tensor:
0].forward(one_electron, one_electron_vector_permuted)
return psi

def loss(self,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loss functions, right now its implemented for pertaining (MSELoss)

@@ -205,10 +226,8 @@ def __init__(self,
Torch tensor containing electrons for each atom in the nucleus
molecule: ElectronSampler
ElectronSampler object which performs MCMC and samples electrons
loss_value: Optional[torch.Tensor] (default None)
loss_value: torch.Tensor (default torch.tensor(0))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attribute that keeps track of the last loss value. It differs form running mean by calculating the mean over all batches and the number of steps in MCMC


self.molecule: ElectronSampler = ElectronSampler(
batch_no=self.batch_no,
central_value=self.nucleon_pos,
seed=self.seed,
f=lambda x: test_f(x), # Will be replaced in successive PR
f=lambda x: self.random_walk(x
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing the sampling function to the random walk function which returns the log wavefucntion probability

@@ -291,6 +310,34 @@ def __init__(self,
self.model,
loss=torch.nn.MSELoss()) # will update the loss in successive PR

def evaluate_hf(self, x: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function to evaluate hf orbitals at the given electron coordinates. (As mentioned in the last PR split the prepare_hf function into smaller ones)

self.model.loss(up_spin_mo, down_spin_mo, pretrain=True)
return np.log(hf_product + np_output**2) + np.log(0.5)

def pretrain(self,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function to invoke pretraining

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have a custom function for pretraining. Instead follow the convention used elsewhere of having a 'task` parameter and add pretraining/supervised tasks for regular training.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -313,3 +360,59 @@ def prepare_hf_solution(self):
self.mol.build(parse_arg=False)
self.mf = pyscf.scf.UHF(self.mol)
_ = self.mf.kernel()

def random_walk(self, x: np.ndarray) -> np.ndarray:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function gets called at every step of electron sampling, and it returns the probability of the sampled electrons - log of the wavefunction from the model and the HF solution (this is only for pretraining).

Copy link
Member

@rbharath rbharath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shaipranesh2 We shouldn't have a custom pretrain function but rather should have a task field that we can set. See InfoGraph or other models for examples.

We should also expect to see 22 tests passing but I see 21. Are there any new failures?

@shaipranesh2
Copy link
Contributor Author

shaipranesh2 commented Sep 29, 2023

@shaipranesh2 We shouldn't have a custom pretrain function but rather should have a task field that we can set. See InfoGraph or other models for examples.

We should also expect to see 22 tests passing but I see 21. Are there any new failures?

I can see the docs failing in this PR and some others. While it used to passing before (the changes are not due to this PR)

pretrain: List[bool] = [True]):
"""
Implements the loss function for both pretraining and the actual training parts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a follow up PR, please add more details on the loss. You should be able to list the actual latex for the formulas as well

@@ -280,7 +311,8 @@ def __init__(self,
batch_no=self.batch_no,
central_value=self.nucleon_pos,
seed=self.seed,
f=lambda x: test_f(x), # Will be replaced in successive PR
f=lambda x: self.random_walk(x
), # Will be replaced in successive PR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to remove the comment in your next update

Copy link
Member

@rbharath rbharath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rbharath rbharath merged commit 9183618 into deepchem:master Oct 2, 2023
23 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants