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
Conversation
…into ferminet_hf
59821a7
to
3a81881
Compare
@@ -71,6 +65,8 @@ def __init__(self, | |||
|
|||
Attributes | |||
---------- | |||
running_diff: torch.Tensor |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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]: |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function to invoke pretraining
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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).
There was a problem hiding this 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?
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. | ||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
Fix #(issue)
Type of change
Please check the option that is related to your PR.
Checklist
yapf -i <modified file>
and check no errors (yapf version must be 0.32.0)mypy -p deepchem
and check no errorsflake8 <modified file> --count
and check no errorspython -m doctest <modified file>
and check no errors