My First Time Training a Model: Reinforcement Learning vs. Imitation Learning
The question of whether a computer can think is no more interesting than the question of whether a submarine can swim. — Edsger Dijkstra
What I built
I’d been learning about AI for a few days when I decided to try training a model myself. I taught myself enough PyTorch to build a few simple models that all play the same classic game: Flappy Bird. Here’s how they performed, what I tried, and what I learned.
Reinforcement learning
| Version | Method | Training episodes | Estimated average score |
|---|---|---|---|
| 1.0 | Reinforcement learning | 8000 | 0 |
| 1.1 | Reinforcement learning | 8000 | 2.1 |
| 1.2 | Reinforcement learning | 8000 | 0 (the model collapsed) |
Imitation learning
| Version | Method | Training | Data source | Average score |
|---|---|---|---|---|
| 2.1 | Imitation learning | 50 epochs | Rule-based expert and human play | 37.5 |
| 2.1.1 | Imitation learning | 50 epochs | Trajectories collected by the v2.1 model | 47.0 |
| 2.1.2 | Imitation and reinforcement learning | 4000 episodes | Replay prefilled with v2.1.1 data, followed by online DQN training | 53.5 |
Setting up
Here’s my setup, in case it saves you from making the same mistakes:
- An Apple-silicon Mac with MPS GPU acceleration
- Python 3.13 in a venv virtual environment
- PyTorch 2.14
- flappy-bird-gymnasium, an OpenAI Gym-style Flappy Bird environment
- pygame for rendering
The first trap: the Run button in the top right of VS Code can use a different interpreter from the virtual environment you activated in your terminal. I installed torch in the terminal, hit Run, and still got No module named 'torch'. It took me a while to realize I was using two different Python environments.
What is DQN, in my own words?
The idea behind DQN is simple: use a neural network to predict how much future reward each available action is worth in the current situation, then pick the action with the highest predicted value. That estimate is the Q-value.
Think about what goes through your head while playing: “If I flap now, I might stay in the air five seconds longer. If I don’t, I might hit the pipe.” The neural network is doing that kind of guesswork.
Two key ideas:
- Experience replay: Save what happens at each step, then randomly sample a batch for training. Training no longer depends on a sequence of highly correlated, consecutive frames.
- Target network: Use a separate network to supply the learning targets, and sync it with the training network periodically. Otherwise it’s like grading your own homework and changing the answer key every time.
How the versions evolved
v1.0: My first DQN went nowhere
Setup:
- Network: a 128 → 64 → 2 MLP
- Reward: +0.1 for surviving a frame, −1 for dying
- Epsilon: starts at 1.0 and decays
Result: Reward hovered at 3.9, and the score never budged from 0. The bird had no idea it was supposed to fly through the pipes.
The mistake: I put epsilon decay inside train_step(), so it ran every step, or every frame. With hundreds of frames per game, epsilon dropped from 1.0 to 0.01 in two games. Exploration was over before it had really started.
I spent a long time stuck on this. Why was epsilon already at 0.01 when I’d set it to 1.0? Eventually I realized it was decaying in the wrong place. I meant to reduce it once per episode, not once per frame.
v1.1: Bigger rewards got the bird through a pipe, but not reliably
Changes:
- Fixed epsilon decay so it happened once per episode
- Changed the reward for passing a pipe to +100 and the death penalty to −10
- Gave +0.01 per surviving frame and applied no flap penalty in the first 4000 episodes
Result: The bird finally got through pipes! Over 10 test games it averaged 2.1 points and reached a high of 4.
There were still problems:
- It seemed to get through pipes by luck rather than applying a reliable skill.
- Reward hovered around −9.5, suggesting the bird still died after about 50 frames most of the time.
- Every checkpoint file was exactly 129,373 bytes. At the time I took that as a sign that the weights hadn’t changed much since episode 1000, and suspected the model had settled on a poor strategy. File size alone doesn’t establish that the weights stopped changing.
Why did it happen? The first 2000 episodes were random exploration. The bird got through its first pipe by chance in episode 2494. By then epsilon was falling. The model had seen only one example of passing a pipe, so it hadn’t learned to reproduce that behavior before epsilon dropped to 0.05.
v1.2: Adaptive epsilon taught the bird to give up on the game
Changes:
- Reduced the reward scale: −1 for dying, +0.1 for surviving a frame, and +1 for passing a pipe
- Added a +10 bonus for the first successful pass
- Made epsilon adaptive: If the average score over the last 100 episodes was below 0.5, the agent kept exploring randomly with epsilon at 1.0. Only after it started succeeding would epsilon fall.
The idea: v1.1 stopped exploring before it had learned anything useful. Why not do the reverse and reduce exploration only after the bird learns to get through pipes? It sounded reasonable.
Result: The score stayed at 0 for about 2300 episodes. The average never met the threshold, which kept epsilon at 1.0. Around episode 2300 the bird finally passed a pipe, and the model started learning, just not the behavior I wanted.
How it failed: The model learned to flap constantly. The bird flew straight to the top almost immediately and got stuck near the ceiling. The score stayed at 0 because it never made it through a pipe.
Why? It found another poor strategy:
- Flap continuously and you may survive a few dozen frames, earning +0.1 per frame. That’s better than dying quickly during random play.
- The model has a simple answer: keep flapping as long as there’s a reward for staying alive.
- The next pipe is too far away for the model to learn from it. Surviving now is easier.
The reward function signaled that survival and passing pipes were good, but didn’t give the model a reason to stop flapping mindlessly. It picked the easiest way to survive: keep flying up.
This was the flip side of v1.1: when useful guidance is too sparse, the agent can find a shortcut that earns reward without getting better at the game.
Video:
The ideas that stuck with me
1. What epsilon actually is
Epsilon is the chance of trying a random move. At 1.0 the agent plays entirely at random; at 0.01 it almost always follows what it has learned.
I wanted more trial and error early on and more reliance on the learned policy later. Decay too quickly and the agent “graduates” before it has learned anything. Decay too slowly and the policy never settles down. For this project, getting that timing right mattered more than the network architecture.
2. The reward function is a compass
This was my biggest takeaway: for these experiments, reward design mattered much more than the architecture.
- The first version didn’t reward passing pipes, so it could learn “don’t die,” but not that pipes were the point.
- In the second version, +100 was much larger than the other signals. I struggled to get stable learning.
- In the third version I tried smaller, more frequent rewards. They were an improvement in scale, but the model still found a loophole.
With no clear reward signal, the model can work hard and still go the wrong way. It’s like climbing a hill blindfolded without knowing where the summit is.
3. The agent can learn to stop trying
I initially set the death penalty to −100 and watched the model give up. One pipe wasn’t enough to make up for dying, so the setup left the agent with little reason to learn how to pass it. I forget what the equivalent effect is called in psychology, but it reminded me of how it feels to stop trying after bombing an exam.
4. Exploration versus exploitation
This is a central tension in reinforcement learning:
- Exploration: Try a new action. It could uncover a better strategy or send you into a wall.
- Exploitation: Stick to the best strategy you know. It’s more reliable, but you might miss something better.
Tuning epsilon by hand felt clumsy. Adapting it to the model’s current performance seemed more promising, although I still needed to make sure the rule couldn’t lock exploration in place.
Imitation learning
What is imitation learning? In this case, it was basically copying someone’s homework.
With reinforcement learning, the AI starts from scratch. It bumps into a pipe thousands of times before figuring out that pipes are bad news. The game is stingy with feedback: most of what it tells you is “you died.” How not to die is up to you.
Imitation learning does something more direct: record how someone who knows the game plays, then have the AI learn to copy those actions. It’s how you might learn a new game yourself: watch someone skilled play, copy what they do, and gradually develop your own feel for it.
Behavior cloning in three steps
Behavior cloning is a straightforward form of imitation learning:
- Record trajectories. Let the expert play and record the state and the expert’s action at each frame.
- Train a classifier. Teach the network to predict which action the expert would take given a state.
- Run inference. Let the model play on its own, predicting an action at each frame.
Put simply, I turned “how do you play this game?” into “what label does this state get?” That felt familiar from competitive programming: first learn the standard solutions, then figure out when you need to go beyond them.
v2.1: Pure imitation reaches 37.5
I wrote a rule-based expert that steered toward the middle of each gap. It played 100 games, and I added some recordings of my own play. That gave me 58,682 frames. I trained a 12 → 128 → 64 → 2 network for 50 epochs, reaching 99.26% classification accuracy.
The average test score was 37.5. I usually only get around ten when I play by hand, which says something about my gaming skills. The AI did better after one night than I did after a day of practice. A little humbling, but I was heading in the right direction.
v2.1.1: Let the model teach its successor, and reach 47
Data was the bottleneck. The rule-based expert was steady, but its actions were mechanical and didn’t offer much variety. My idea was to let v2.1 play and record its own data, then train v2.1.1 on those recordings. A trained model became the next model’s teacher.
I hit a big snag: at first I added a little random exploration during recording, with a 5% chance of a random move. The average fell to 0.9 points. Flappy Bird depends on precise timing; a single extra flap can kill the bird. For the demonstrations I wanted, the learned policy needed to act deterministically. After switching to pure argmax, I recorded 300 games and 177,889 frames. Training accuracy reached 99.50% and the average test score hit 47, up about 25%.
v2.1.2: Imitation plus reinforcement, a lot of crashes, and finally 53.5
Imitation had worked well enough to tempt me into adding RL for another boost. I still wanted to make DQN work. Instead, I got a long run of failures:
- Start DQN from the imitation model’s weights? Crashed. The evaluation score fell to zero.
- Prefill replay with expert data and add an imitation loss? Crashed.
- Freeze the feature layers and train only the output layer? Crashed.
- Switch to PPO? Still crashed once I let the policy update freely.
I tried seven or eight combinations and none worked. Reading more made a mismatch clear: the imitation head predicted classification logits, whereas DQN needed estimates of future reward. Those outputs mean different things. Reusing one output layer as if it already spoke both languages was asking for trouble.
DQN learns “roughly how much future reward is this action worth?” Imitation learns “which button did the expert press in this state?” They’re different questions, even if both models end in a layer with two outputs.
What finally helped was a demonstration-based approach inspired by DQfD. I trained the Q-network from scratch but prefilled its replay buffer with about 178,000 expert frames. I also fixed the exploration-rate logic that had stalled v1.2. Later in training, I saw signs of bootstrapping error accumulating, and the policy broke down in longer games. Early stopping, by selecting a mid-training checkpoint, got me to 53.5.
Why did imitation work so much better?
Copying a good example is faster than figuring everything out from scratch.
Flappy Bird’s reinforcement feedback is sparse: +1 for passing a pipe and −1 for dying, with many moves between those events. Imitation provides a target every step: did the expert flap or not? It’s the difference between seeing the answer to every exercise and getting only a final exam grade.
At the same time, pure imitation gives you no explicit signal for doing better than the teacher. My model was learning from the rule-based expert and my own play. I wanted RL to help it go further. Getting those methods to work together cost me an evening, and the demonstration-prefilled replay buffer was what finally got me somewhere.
Mistakes worth remembering
- The action labels were heavily imbalanced. Across 300 games and about 178,000 frames, the expert flapped in only 5.4% of frames. A model that never flapped would be 94.6% accurate and still useless at the game. A high classification score isn’t enough; check the actual game score.
- Demonstration quality mattered. In my run, 5% random exploration was enough to drop the average from over 40 to 0.9.
- A short evaluation can be misleading. The mid-training model did well on the short evaluation. Longer games exposed a failure that would otherwise have gone unnoticed.
- Imitation and RL ask for different predictions. I couldn’t simply swap classification logits for Q-values and expect the existing weights to keep working.
What I took away
Coming from competitive programming, I found the process familiar: learn the standard tactics, then try to invent better ones. Imitation helps you learn the standard tactics quickly, but what it teaches you is limited by the examples. Reinforcement learning looks for new tactics. It can go further, but it takes more work. Learning the tactics and then trying to improve on them is where things get interesting.
There’s a lot left to learn, but I have a clearer idea of where to go next.
Summary
I started with three reinforcement-learning attempts that topped out at 2.1 points. Imitation jumped to 37.5. Training again on the model’s own demonstrations got me to 47, and imitation plus reinforcement learning reached 53.5. The scores were fun, but the takeaways were more useful:
- In RL, designing rewards and managing exploration were harder than writing code. Sparse guidance encouraged shortcuts like flapping constantly. Decay epsilon too early and the agent barely explores; leave it high too long and it never settles into a policy.
- Imitation learning was mostly data work. A three-layer MLP was enough for all this; the big difference was the data. The rule-based expert and human demonstrations got 37.5 points, and better trajectories collected from the model got 47. Adding just 5% random actions wrecked those demonstrations.
- In my runs, combining imitation and RL worked best at the data level. I stopped reusing the imitation head, trained the Q-network from scratch, and prefilled its replay buffer with expert data before online DQN training. Early stopping helped avoid the degradation I saw later.
- Evaluate over the horizon you care about. The 600-frame evaluation masked a breakdown I could see in the 3000-frame test. Model selection should use a test that reflects the real goal.
- Learn the standard tactics first, then innovate. That applies to both competitive programming and training an AI model. Starting with demonstrations was fast, and experimenting beyond them was hard but worth it.
Getting to 53.5 is only a start. Next I’d like to train a CNN that plays from the screen directly, or try Double DQN to address the late-training breakdown. I’ll update the post if I make progress. 🐦
Written late at night on September 21, 2026, in front of my MacBook Pro, after taking the bird from 2.1 all the way to 53.5.
I drafted this article and used AI to polish it. It took about three hours and is the longest technical article I’ve written.
I’m still learning. This is a record of what happened in my experiments, and discussion by email is welcome.
Zheming Zhu · September 21, 2026 · South China University of Technology

