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

Gibbs with Gradients demo #524

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

duvenaud
Copy link
Contributor

'Main algorithm from "Oops I Took A Gradient: Scalable Sampling for Discrete Distributions"
by Will Grathwohl, Kevin Swersky, Milad Hashemi, David Duvenaud, Chris J. Maddison
Arxiv Link demonstrated on an Ising model.

This one already works - the main thing that needs to be done is make the colors nicer.

examples/gibbs_with_gradients.dx Outdated Show resolved Hide resolved
(MkImage rows cols pixels) = fromJust $ loadImageP6 "examples/peace.ppm"

-- Convert to binary image.
def pixelToBool (x:Char) : Bool = (W8ToI x) < 0
Copy link
Contributor

Choose a reason for hiding this comment

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

This is confusing isn't this already going to return false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You'd think so, but casting an unsigned W8 to a signed int means that half the values are negative. This is evidently a confusing way to have written this, though.

addnoise = rand (ixkey2 (newKey 0) i j) < noisefrac
case addnoise of
True -> not image_bool.i.j
False -> image_bool.i.j
Copy link
Contributor

@oxinabox oxinabox Aug 19, 2021

Choose a reason for hiding this comment

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

Can tje above 3 lines be written
(not addnoise) & image_bool.i.j ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's xor, but the syntax for that is .^. and I thought this would be easier to read.

examples/gibbs_with_gradients.dx Outdated Show resolved Hide resolved
@duvenaud duvenaud changed the title [WIP] Gibbs with Gradients demo Gibbs with Gradients demo Aug 20, 2021
Copy link
Collaborator

@apaszke apaszke left a comment

Choose a reason for hiding this comment

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

This is awesome. I especially enjoyed the commentary. IMO this is good to go, except that I'd like to reduce the size of the peace.ppm image. Checking in binary files is generally a bad practice and I'd like to avoid doing that as much as possible. Plus, are there any licensing constraints on it?

which is a Boolean vector `n=>Bool`,
and an unnormalized probability mass function `f`.
The pmf maps Boolean vectors of the same size as `x` and maps
them to an unnormalized log probability density.
Copy link
Collaborator

Choose a reason for hiding this comment

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

maps twice?

'The Gibbs with Gradients sampler has a slightly different function signature
than standard Gibbs. Instead of its log probability function taking in a
discrete array, it takes in an array of floats of the same size.
This is because Gibbs with Gradients needs to be able to differentiate the energy function
Copy link
Collaborator

Choose a reason for hiding this comment

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

is pmf/pdf the same as the energy function?

of Algorithm 1 from the paper. In fact, the reason this demo got
started was because I wasn't sure I understood the pseudocode
in our own paper. Writing the algorithm in this terse (but machine-checked)
format made me realize we were missing a line from the algorithm box.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Whoah, nice!

@duvenaud
Copy link
Contributor Author

I'd like to reduce the size of the peace.ppm image. Checking in binary files is generally a bad practice and I'd like to avoid doing that as much as possible.

Yes, I realize this. How about if I host the image somewhere, and use the shell to download it?

Plus, are there any licensing constraints on it?

Nope, it's in the public domain. Source

@apaszke
Copy link
Collaborator

apaszke commented Aug 24, 2021

Downloading via the shell sounds good! Alternatively you could assume that it's in the file system and add the download instructions (+ insert them in CI). That's what we do for the tutorial IIRC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants