andylolu2 / jax-vqvae-gpt Goto Github PK
View Code? Open in Web Editor NEWImplementation of VQ-VAE with a GPT-style sampler in the JAX and Haiku ecosystem.
License: MIT License
Implementation of VQ-VAE with a GPT-style sampler in the JAX and Haiku ecosystem.
License: MIT License
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)
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 input
, since it is the output of the encoder. Then 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!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.