0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Learning to throw a ball: the hook
Think about how you learned to throw a ball. You didn't build a table scoring every possible arm movement. You just threw, and when a throw worked, you became a little more likely to throw that way again. When it didn't, a little less. Last episode, our robot learned a value for every action, then picked the best one. That works brilliantly for a few actions. But a steering wheel has endless possible angles, and a chatbot chooses among tens of thousands of words at every step. Today's method skips the scoring and learns the behaviour itself: the policy.
It's called the policy gradient, and its core idea fits in four words: do more of what worked. It's also, with a few refinements, a close relative of the methods used to fine-tune modern chatbots. And at the end, we'll fly over everything we've built in Season One. Season One, Module Six, final stop of the season: policy gradients.
1:15A policy you can tune
First, we need a policy we can adjust smoothly. Instead of always picking one action, the robot keeps a probability for each action. At this junction, the right exit leads to a fast road and the left to a slow one, though the robot doesn't know that yet. It gives each action a preference score and turns the scores into probabilities with softmax, the same function from logistic regression. Both scores start at zero, so it's fifty-fifty. Those scores are the policy's parameters, which we call theta. In bigger problems, theta would be the weights of a neural network that looks at the state and outputs these probabilities.
For continuous actions like steering, the policy outputs a distribution instead, say an average angle and a spread, and samples from it. The idea is the same.
2:09Do more of what worked: REINFORCE
Our goal is to maximise the expected return, so this time we climb uphill instead of walking down: gradient ascent. The classic algorithm is called REINFORCE. After an episode, for each action taken, we nudge theta in the direction that makes that action more likely, scaled by the return that followed. Good outcome: a big push towards repeating the action. Poor outcome: a small push. Let's try it. The robot happens to choose right and collects a return of eight. For a softmax policy, the direction that raises the probability of "right" increases right's score and lowers left's, each by one minus the probability, here a half.
Multiply by the learning rate of zero point one and the return of eight: right's score goes up by zero point four, and left's goes down by zero point four. The dial moves from fifty percent to sixty-nine percent for "right". The robot is now noticeably more likely to repeat what worked.
3:15Why the log? The log-derivative trick
Why does the log of the probability appear? Short deep dive. The expected return is the sum, over actions, of each action's probability times its return. Taking the gradient, we use a small identity: the gradient of a probability equals the probability times the gradient of its log. That turns the gradient into an average, over the actions we actually sample, of the return times the gradient of the log-probability. So we don't need to know how the world works. We just try actions, observe results, and weight each one accordingly.
And notice a familiar face: minus the log-probability of the correct answer is exactly the cross-entropy loss from logistic regression. REINFORCE is like training a classifier on your own actions, with each action's lesson weighted by how well it turned out.
4:10The noise problem, and baselines
There's a catch. Suppose the robot picks left and gets a return of three. Three is positive, so REINFORCE still increases the probability of going left, and "right" drops to forty-three percent. It's learning the wrong lesson. It only works out on average, because good actions get pushed up harder than bad ones. And averages built from random samples are noisy. The fix is to compare each return with what we expected. Subtract a baseline, like the average return so far, and use the difference, called the advantage. A return of eight against an expectation of five and a half is two and a half better than expected: reinforce it.
A return of three is two and a half worse: discourage it. In this example, every update now pushes in the right direction; with noisy returns, most do. Pause and predict: does the baseline make learning faster, steadier, or both?
Here are fifty training runs each way. Without a baseline, the curves fan out widely: after twenty-five updates, the probability of going right averages ninety-one percent, but varies a lot from run to run. With a baseline, the fan is tight, averaging ninety-four percent with about six times less spread. Both, then: a little faster on average and much steadier, which matters enormously in big problems.
5:40Actor-critic
Where does a good baseline come from? We can learn it. Train a second model, called the critic, to predict the expected return from each state. The policy, called the actor, chooses actions; the critic judges whether things went better or worse than expected, using the same temporal-difference idea as Q-learning. Actor and critic learn together: the critic gets better at predicting, and the actor gets clearer signals about what to repeat. Actor–critic methods combine the two families of reinforcement learning, values and policies, and many widely used methods, including P-P-O, are built this way.
6:22PPO, and the road to chatbots
Policy gradients have one more danger. Because the policy changes itself, one oversized update can push the probabilities to an extreme and wreck performance, and then the data it collects next is bad too. To prevent this, a widely used method called P-P-O, proximal policy optimisation, puts a leash on each update: the new policy isn't allowed to move its probabilities too far from the old one in a single step. And here's where Season One meets the future. Picture a chatbot as an agent. The state is the conversation so far.
Each action is the next word. And the reward comes from a model trained to predict which answers people prefer. Train that agent with a policy-gradient method like P-P-O, and you have the core of reinforcement learning from human feedback, one of the techniques used to make language models more helpful. We'll take it apart properly in Season Three.
7:25Value-based vs policy-based methods
So, two families. Value-based methods, like Q-learning, score actions and pick the best. They're well suited to a modest number of discrete actions. Policy-based methods learn the behaviour directly. They handle continuous actions and huge action spaces naturally, at the cost of noisier learning that needs baselines and careful step sizes. Actor–critic methods combine both. And one myth to clear up: policy gradients don't just reward success. With advantages, they actively push down actions that turned out worse than expected.
Learning from failure is half the method.
8:05Try it yourself: updates with a baseline
Let's check you've got it. The policy is fifty-fifty, the baseline is five and a half, and the robot happens to choose left, earning three. With the baseline, does the probability of going right go up or down? Pause and think about the advantage.
The advantage is three minus five and a half: minus two and a half. Left did worse than expected, so it's discouraged: its score drops and right's score rises, and the probability of going right becomes about fifty-six percent. Without the baseline, as we saw, the very same experience would have encouraged left. That's the baseline doing its job.
8:51Steering: continuous actions
Policy gradients handle continuous actions naturally. For a steering angle, the policy outputs a bell curve: an average angle and a spread. The robot samples an angle from that curve. When an angle turns out better than expected, the update shifts the average towards it, and as the agent grows confident, the spread can narrow. Watch the bell curve slide towards good angles and tighten over training. No table of actions could ever do that.
9:23Entropy bonuses and exploration
Policy gradients can become confident too quickly, locking onto one action before exploring enough. A common remedy is an entropy bonus: a small reward for keeping the policy a little uncertain, using the same entropy we met with decision trees. It keeps the probability dial from slamming to one hundred percent too early. Like most remedies, it needs balance: too much, and the agent behaves randomly; too little, and it gets stuck.
9:53Learning from batches of experience
In practice, agents don't update after every single episode. They collect a batch of experiences, average the gradients, and then update, just like the mini-batches of gradient descent in episode five. Bigger batches give less noisy gradient estimates. P-P-O, the method we met earlier, collects a batch, then takes several careful, clipped update steps on it before collecting more.
10:22From robots to chatbots: the same loop
Here's the bridge to the rest of the course. Put the delivery robot and a chatbot side by side. The robot's state is its block on the street; the chatbot's state is the conversation so far. The robot chooses left or right; the chatbot chooses its next token. The robot earns rewards for deliveries; the chatbot earns a score from a model trained on human preferences. Both use baselines, and both keep their updates on a leash. Both can use P-P-O's clipping to keep each update small; the chatbot adds a second leash, a penalty for drifting too far from its original fine-tuned model.
And the pitfalls carry over too. A chatbot can reward-hack by becoming long-winded or flattering, the same way the robot drove back and forth. We'll study exactly that in Season Three.
11:17Recap
Let's recap this episode. A policy assigns probabilities to actions. REINFORCE raises the log-probability of each action in proportion to the return that followed. Subtracting a baseline turns returns into advantages, and makes learning far steadier. A learned critic provides that baseline, and P-P-O keeps updates safe, which leads straight to how chatbots are fine-tuned with human feedback. One sentence to remember: do more of what worked better than expected.
11:51Season 1 finale: the flight over the coast
And that's the end of Season One. Let's look back at the road we've travelled. We started with the maths toolkit: vectors and dot products, gradients and the chain rule, probability and Bayes. We learned how models learn: gradient descent, honest testing, regularisation and feature engineering. We built regression from scratch, then a complete classification toolkit, from logistic regression to forests, boosting and support vector machines. We discovered structure with no labels at all: clusters, components, shopping rules and anomalies.
And finally, we taught a robot to act through rewards. Twenty-two missions for Brightcart, and every number on screen was a real calculation. Now look ahead, at the mountains. In Season Two, we'll take the single most important idea from this season, a weighted sum followed by a squashing function, and stack it into layers. We'll build neural networks from one neuron upwards, train them with backpropagation, and climb to the architectures that see images, read sequences, and power today's language models.
13:04Next: Season 2, the neuron
Mission complete, and Season One complete. The robot learned to choose the fast road almost every time, and the baseline made its learning six times steadier. Season Two begins with a mission that sounds impossible: build a brain cell. It's simpler than you think, and you already know most of it. Thank you for learning with MOFUSA Tech Academy. See you in the mountains.