Deep Reinforcement Learning: Pong from Pixels
Open the sourceKarpathy teaches Policy Gradients through a compact Pong implementation, explaining them as a stochastic guess-and-check process that increases the probability of actions associated with high rewards. He also emphasizes their limitations: they require many samples, struggle when rewards are sparse or exploration is expensive, and fall far short of the abstract model-building and planning humans use.
- Policy Gradients directly optimize expected reward by sampling actions, observing outcomes, and reinforcing actions associated with better outcomes. - The reinforcement-learning credit assignment problem is difficult because rewards may arrive many frames after the actions that caused them. - Policy-gradient learning resembles supervised learning, except sampled actions serve as labels and each example is weighted by an advantage. - Discounted and standardized returns help assign value to actions and control the variance of the policy-gradient estimator. - Policy Gradients can train stochastic, non-differentiable components embedded within otherwise differentiable neural networks. - In practice, Policy Gradients demand many samples, train slowly, are difficult to debug, and should not displace simpler baselines without justification.
Based on Andrej Karpathy's public content. Not affiliated with or endorsed by Andrej Karpathy.
Supports these rules
Quotes from this video
“policy and rinse and repeat. Policy Gradients: Run a policy for a while. See what actions led to high rewards. Increase their probability.”
“actions and then actions that happen to eventually lead to good outcomes get encouraged in the future, and actions taken that lead to bad outcomes get discouraged.”
“Policy gradients is exactly the same as supervised learning with two minor differences: 1) We don’t have the correct labels yi so as a “fake label” we substitute the action we happened to sample from the policy when it saw xi, and 2) We modulate the loss for each example multiplicatively based on the eventual outcome, since we want to increase the log probability for actions that worked and decrease it for those that didn’t.”
“So reinforcement learning is exactly like supervised learning, but on a continuously changing dataset (the episodes), scaled by the advantage, and we only want to do one (or very few) updates based on each sampled dataset.”
“One common choice is to use a discounted reward, so the “eventual reward” in the diagram above would become Rt=∑∞k=0γkrt+k, where γ is a number between 0 and 1 called a discount factor (e.g. 0.99).”
“One good idea is to “standardize” these returns (e.g. subtract mean, divide by standard deviation) before we plug them into backprop.”
“As we go through the solution keep in mind that we’ll try to make very few assumptions about Pong because we secretly don’t really care about Pong; We care about complex, high-dimensional problems like robot manipulation, assembly and navigation.”
“Pong is just a fun toy test case, something we play with while we figure out how to write very general AI systems that can one day do arbitrary useful tasks.”
“One should always try a BB gun before reaching for the Bazooka.”
“In the case of Reinforcement Learning for example, one strong baseline that should always be tried first is the [cross-entropy method (CEM)](https://en.wikipedia.org/wiki/Cross-entropy_method), a simple stochastic hill-climbing “guess and check” approach inspired loosely by evolution.”
“and check” approach inspired loosely by evolution. And if you insist on trying out Policy Gradients for your problem make sure you pay close attention to the _tricks_ section in papers, start simple first, and use a variation of PG called [TRPO](https://arxiv.org/abs/1502.05477), which almost always works better and more consistently than vanilla PG [in”
“The core idea is to avoid parameter updates that change your policy too much, as enforced by a constraint on the KL divergence between the distributions predicted by the old and the new policy on a batch of data (instead of conjugate gradients the simplest instantiation of this idea could be implemented by doing a line search and checking the KL along the way).”