Entropy
its a measure of how disordered a system is e.g. a gas with molecules moving in random directions has a higher entropy than a neatly arranged crystal.
High entropy means the system is more disordered
Lower entropy means the system is less disordered
Overtime your room naturally gets messy → entropy is increasing
Overtime hot chai gets to the room temperature → initially the heat in coffee was concentrated (low entropy), since energy spreads out over time, disorder has increased meaning Entropy was increased
Second law of thermodynamics: "Entropy always increases in a closed system"
Closed system is in which we can exchange energy but not matter
In an open system we can exchange energy and exchange matter e.g. Boiling water in an open pot (heat & steam escapes)
Since entropy always increases, one day the universe will reach maximum entropy, at that point all energy will evenly spread out, and no work/life will be possible. No temperature differences → no useable energy → no life or movement → just cold, uniform emptiness.
Why does entropy increases? Theory suggests that there are far more ways for things to be disordered than ordered. Imagine you have a jar with 100 marbles: 50 red and 50 blue. If you randomly throw the marbles in, they’ll spread out randomly. There are many more ways to arrange them in a mixed-up state than to have all reds on one side and all blues on the other. Therefore, a disordered state is much more probable than an ordered state. This is why nature tends to favor disorder, and entropy increases as a result. Therefore, a disordered state is much more probable than an ordered state. This is why nature tends to favor disorder, and entropy increases as a result.
Since over time everything becomes disordered and chaotic, How can life (organisms that maintain order) exist in universe where entropy is supposed to increase? Life doesn’t violates the second law because living organisms are open systems. Organisms take in energy, which they use to decrease their internal entropy, but since this is an open system, it causes entropy to increase somewhere else.
Since initial AI researchers came from electrical/mechanical engineering backgrounds, they tried to enforce their thermodynamics concepts into artificial intelligence. e.g. decision trees, Boltzmann machines, etc. Cross entropy loss is also inspired from entropy concept, where cross-entropy is a thermodynamics concept which measures difference between true distribution and predicted distribution, this entropy tells how far off we are from the actual probabilities.
Before AI, energy concept came from physics, every system has a certain energy state and systems naturally evolve towards lower energy states. When a system reaches its lowest energy state, it is in equilibrium, e.g. a ball rolling downhill eventually comes to rest in a valley, it can no longer roll further down because it has reached lowest point. Imagine an energy landscape where the height of the landscape represents the energy of a state. High peaks correspond to high-energy, unstable states, and valleys correspond to low-energy, stable states. Systems naturally "roll downhill" toward the valleys because that is where the energy is minimized. Moving from a high-energy state to a low-energy state often releases energy, while moving uphill (to a higher-energy state) requires an input of energy. This is why spontaneous processes tend to go downhill—the universe "saves" energy and tends toward efficiency.

Ludwig Boltzmann studied how particles in gases distribute themselves, he found that particles are more likely to be in low-energy states. The probability of a state depends on its energy E(s), following this rule:
←aka Boltzmann distribution→
Fast forward 1980s Geoffry Hinton thought 'what if we treat neurons in a neural network like particles in a physical system'
Training a Boltzmann Machine is like finding the lowest-energy configuration where the model best represents the data.
Energy based models (EBM)
Energy based models EBMs model distribution of data by associating an energy score to each data point. Lower energy score indicates a more probable data point and higher energy means less likely datapoint.
We wanna design a neural network to model an energy function that
maps to an boltzmann distribution.
← Boltzmann distribution

Langevin dynamics technique
It's a sampling technique, used to generate samples that are approximately drawn from the distribution p(x)
When we say that Langevin dynamics converges to the Boltzmann distribution, we mean that after enough iterations, the samples generated by Langevin dynamics will follow the same probability distribution as the Boltzmann distribution. Like you can mathematically even calculate the convergence.
Langevin technique approximates p(x) value
It doesn’t computes partition function, it uses gradient of E(x) to steer sampling process iteratively from gaussian noise towards the target distribution.
langevin technique is inspired by brownian motion and follows this update rule:-
at timestep t
η : step size (learning rate)
: gradient of energy function
: Gaussian noise (randomness helps explore the space)
Combines gradient descent and noise to explore low energy regions
- Initialize as Gaussian noise
- Iterate over Langevin updates
- Compute how much energy changes when input changes slightly using energy gradient wali term
- and Add Gaussian noise too to prevent mode collapse and improves diversity
- Process is repeated until the sample has reached a low-energy region
Langevin dynamics and neural networks both update use gradient descent to find optimal values. In NN training, we optimize weights to minimize loss, in Langevin dynamics, we optimize input to minimize energy. We aren’t optimizing the energy function itself. Instead, we move the input x to a position where the energy function assigns a low value, meaning x is now a high-quality, realistic sample.
Contrastive Divergence ~ (2002 Geoffrey Hinton God Father of AI)
Is a technique designed train models where output is unnormalized score rather than a proper probability.
langevin dynamics helps us sample from p(x),
training an energy based model requires updating energy function E(x) to improve the model.
Goal is to learn an energy function that assigns low energy to good samples and high energy to bad samples. Contrastive divergence estimates the gradient needed for this probabilistic training.
P(x) is a likelihood function
We need to minimize the loss term by converting it into a log likelihood

While we can compute energy scores for green component of loss term very easily, we already have during training. Putting it into gives energy score
To compute cyan term fake samples need to be generated, we don’t have fake samples initially. To obtain a fake sample we need to run the sampling process (langevin dynamics). Process is like:

- Start with an initial guess (often a real sample or noise)
- Use langevin dynamics to update the sample into a region where the model assigns a low energy value
- We iterate the process for n times
- Once process ends and we have sampled the we compute its energy value using the same Energy function
- We compute the loss term to calculate loss value
The most optimal model is achieved when the model distribution is indistinguishable from the data distribution, not when each real sample has exactly the same energy as each fake sample.
At the start the model doesn’t knows how to assign energy values correctly, it might assign both real and fake images similar energies, langevin dynamics generates samples that don’t resemble real images.

As the model learns, it starts assigning lower energy to real samples and higher energy to fake samples, langevin dynamics now starts moving towards regions of lower energy which begins to resemble real images more.
As the energy function is optimized, it assigns very low energy to real images and higher energy to fake images. Langevin dynamics now generates fake samples that are close to real images because the energy function has learned to shape the probability distribution so that low-energy regions correspond to realistic images
Will the energy function mistakenly assign high energy to real images after it has learned to generate good fake images?
Yes and this problem is called mode collapse. This is why we have a gaussian noise term inside the langevin term to ensure fake image energy is not overfitted onto a real image energy.
Sample buffer procedure
Sample buffer acts like a memory bank that stores previously generated samples.
Buffer starts with random noise samples, each sample will go through langevin dynamics to become more structured over time. 5% of the time, new samples are generated from scratch (random noise), 95% of the time samples are drawn from buffer. Reusing previously refined samples prevent model from starting over every iteration, fake images evolve over time, moving closer to real images.