Git Product home page Git Product logo

jax-vqvae-gpt's People

Contributors

andylolu2 avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ipsec nazirnayal8

jax-vqvae-gpt's Issues

Possible bug in VQ loss gradient

Hey, nice and clean implementation!

I think I might have spotted a bug in the gradient of the commitment loss. It's supposed to pull the embeddings to the inputs that mapped to them. But you're computing it after patching the straight-through gradient onto the embeddings, so they actually now have the gradient of the inputs.

In other words, the current code:

quantize = self.codebook[encoding_indices]
quantize = inputs + jax.lax.stop_gradient(quantize - inputs)
encoding_loss = jnp.mean((jax.lax.stop_gradient(quantize) - inputs)**2)
commit_loss = jnp.mean((quantize - jax.lax.stop_gradient(inputs))**2)

Should have the commitment loss computed before applying the straight-through gradient:

quantize = self.codebook[encoding_indices]
encoding_loss = jnp.mean((jax.lax.stop_gradient(quantize) - inputs)**2)
commit_loss = jnp.mean((quantize - jax.lax.stop_gradient(inputs))**2)
quantize = inputs + jax.lax.stop_gradient(quantize - inputs)

Incorrect Codebook Loss Function Implementation in VQ-VAE

There seems to be a discrepancy in the implementation of the codebook loss function for the VQ-VAE model, which does not align with the one described in the original paper "Neural Discrete Representation Learning" by van den Oord et al. (2017).

The current implementation of the loss function is as follows:

# Current implementation
# loss = ||sg[z_e(x)] - e|| + beta||z_e(x) - sg[e]||
encoding_loss = jnp.mean((jax.lax.stop_gradient(quantize) - inputs) ** 2)
commit_loss = jnp.mean((quantize - jax.lax.stop_gradient(inputs)) ** 2)
loss = encoding_loss + self.beta * commit_loss

However, I think you have flipped the variables inputs and quantize. I believe that $z_e(x)$ correponds to input, since it is the output of the encoder. Then $e$ would refer to the output of the codebook: quantize.

Adjusting the loss function as suggested should align the implementation with the theoretical model presented in the original paper, potentially improving the model's performance and stability.

The (simple) fix would be the following:

# Current implementation
# loss = ||sg[z_e(x)] - e|| + beta||z_e(x) - sg[e]||
encoding_loss = jnp.mean((jax.lax.stop_gradient(inputs) - quantize) ** 2)
commit_loss = jnp.mean((inputs - jax.lax.stop_gradient(quantize)) ** 2)
loss = encoding_loss + self.beta * commit_loss

Thank you for taking the time to read this issue!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.