-
Notifications
You must be signed in to change notification settings - Fork 23
gaussian grbm initialization #71
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
base: main
Are you sure you want to change the base?
Changes from all commits
3fb3cdb
05cc617
bd9fdab
0c1ddec
d4bdfbf
60ee81a
eee250a
bc551b8
8ec902e
54b2862
8cde437
4e48419
d9a399c
0e4761d
c6e98c8
85b3e98
2703b26
50561bd
b59be54
a67c421
577c5e3
224e656
5cf88d0
5004f81
c0b9ec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| upgrade: | ||
| - | | ||
| Initialize ``GraphRestrictedBoltzmannMachine`` weights using Gaussian \ | ||
| random variables with graph-connectivity-dependent standard deviations. \ | ||
| For an edge :math:`(u, v)`, the default standard deviation is \ | ||
| :math:`2.5 / (\deg(u)\deg(v))^{1/4}`. \ | ||
| The weight-initialization strategy is grounded in `Hinton's practical \ | ||
| guide for RBM training \ | ||
| <https://www.cs.toronto.edu/~hinton/absps/guideTR.pdf>`_, \ | ||
| which recommends sampling weights from a Gaussian distribution with mean 0 and standard \ | ||
| deviation 0.01 (for zero-one-valued RBMs). The connectivity scaling keeps \ | ||
| the energy functional extensive on sparse graphs, while the temperature factor initializes \ | ||
| the GRBM deep in a paramagnetic regime for QPU-backed sampling, \ | ||
| consistent with the `Sherrington-Kirkpatrick model \ | ||
| <https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.35.1792>`_. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -78,12 +78,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: | |||||||||||||||||||
| # are the models themselves | ||||||||||||||||||||
| latent_dims_list = [1, 2] | ||||||||||||||||||||
| self.encoders = {i: Encoder(i) for i in latent_dims_list} | ||||||||||||||||||||
| # self.decoders is independent of number of latent dims, but we also create a dict to separate | ||||||||||||||||||||
| # them | ||||||||||||||||||||
| # self.decoders is independent of number of latent dims, but we also create a dict to | ||||||||||||||||||||
| # separate them | ||||||||||||||||||||
| self.decoders = {i: Decoder(latent_features, input_features) for i in latent_dims_list} | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # self.dvaes is a dict whose keys are the numbers of latent dims and the values are the models | ||||||||||||||||||||
| # themselves | ||||||||||||||||||||
| # self.dvaes is a dict whose keys are the numbers of latent dims and the values are the | ||||||||||||||||||||
| # models themselves | ||||||||||||||||||||
|
|
||||||||||||||||||||
| self.dvaes = {i: DVAE(self.encoders[i], self.decoders[i]) for i in latent_dims_list} | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -248,19 +247,22 @@ def test_latent_to_discrete(self, n_samples, expected): | |||||||||||||||||||
| @parameterized.expand([(i, j) for i in range(1, 3) for j in [0, 1, 5, 1000]]) | ||||||||||||||||||||
| def test_forward(self, n_latent_dims, n_samples): | ||||||||||||||||||||
| """Test the forward method.""" | ||||||||||||||||||||
| torch.manual_seed(1234) # Set seed for reproducibility of latent_to_discrete sampling | ||||||||||||||||||||
| expected_latents = self.encoders[n_latent_dims](self.data) | ||||||||||||||||||||
| expected_discretes = self.dvaes[n_latent_dims].latent_to_discrete( | ||||||||||||||||||||
| expected_latents, n_samples | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| expected_reconstructed_x = self.decoders[n_latent_dims](expected_discretes) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| torch.manual_seed(1234) # Set seed again to ensure that the sampling in the forward method | ||||||||||||||||||||
| # is the same as in the expected_discretes | ||||||||||||||||||||
| latents, discretes, reconstructed_x = self.dvaes[n_latent_dims].forward( | ||||||||||||||||||||
| x=self.data, n_samples=n_samples | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| torch.testing.assert_close(latents, expected_latents) | ||||||||||||||||||||
| torch.testing.assert_close(discretes, expected_discretes) | ||||||||||||||||||||
| torch.testing.assert_close(reconstructed_x, expected_reconstructed_x) | ||||||||||||||||||||
|
Comment on lines
+262
to
+264
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @VolodyaCO can you review this ^?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes are just to separate each test within their own scope. It should be fine. |
||||||||||||||||||||
|
|
||||||||||||||||||||
| assert torch.equal(reconstructed_x, expected_reconstructed_x) | ||||||||||||||||||||
|
jquetzalcoatl marked this conversation as resolved.
|
||||||||||||||||||||
| assert torch.equal(discretes, expected_discretes) | ||||||||||||||||||||
| assert torch.equal(latents, expected_latents) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.