Take small steps in reinforcement learning

Contents

Introduction

Let's solve Cartpole environments, OpenAI's Lunar Lander and Pong with the STRENGTH algorithm.

The reinforcement learning es posiblemente la rama más genial de la inteligencia artificial. Has already demonstrated its prowess: amaze the world, beat the world champions in Chess games, Go and include DotA 2.

In this article, I would analyze a rather rudimentary algorithm and show how even this can achieve a superhuman level of performance in certain games.

Reinforcement learning offers with designing “Agents” that interacts with a “Environment” and learn for yourself how “sort out” the environment by systematic trial and error. An environment could be a game like chess or racing, or it could even be a task like solving a maze or achieving a goal. The agent is the bot that performs the activity.

1jrwddpm1lz8eatlkukkt-g-1197737

An agent receives “rewards” when interacting with the environment. The agent learns to perform the “Actions” necessary to maximize the reward you receive from the environment. An environment is considered resolved if the agent accumulates some predefined reward threshold. This nerdy talk is how we teach bots to play superhuman chess or biped androids to walk.

STRENGTHEN Algorithm

REINFORCE pertenece a una clase especial de algoritmos de aprendizaje por refuerzo llamados algoritmos de gradient de políticas. A simple implementation of this algorithm would involve creating a Politics: a model that takes a state as input and generates the probability of performing an action as output. A policy is essentially a guide or cheat sheet for the agent that tells what action to take in each state.. Later, the policy is repeated and slightly modified at each step until we get a policy that resolves the environment.

La política suele ser una red neuronal que toma el estado como entrada y genera una distribución de probabilidad en el espacio de acción como salida.

Politics: Example

The goal of the policy is to maximize the "Expected reward".

Each policy generates the probability of taking an action in each station in the environment.

Politics 1 vs Politics 2 – Different trajectories

The agent samples these probabilities and selects an action to take in the environment. At the end of an episode, we know the total rewards the agent can get if they follow that policy. We repropagate the reward through the route the agent took to estimate the “expected reward” in each state for a given policy.

10pe4hhootwki5lljxzv47w-5225362

Here the discounted reward is the sum of all the rewards that the agent receives in that future discounted by a Gamma factor.

1piymd_6px1euk21zadxzka-5954891

The discounted reward at any stage is the reward you receive in the next step + a discounted sum of all rewards the agent receives in the future.

The discount factor is calculated for each state by back-propagating the rewards.

For the above equation, this is how we calculate the expected reward:

1r086if2zz_hx89wowmkfoa-1118659

According to the original implementation of the STRENGTH algorithm, the expected reward is the sum of the products of a record of discounted odds and rewards.

Algorithm steps

The steps involved in the implementation of REINFORCE would be the following:

  1. Initialize a random policy (an NN that takes the state as input and returns the probability of actions)
  2. Use politics to play N steps of the game: record probabilities of action, of politics, the reward of the environment, the action, sampled by agent
  3. Calculate the discounted reward for each step by backpropagation
  4. Calculate the expected reward G
  5. Adjust the policy weights (reverse propagation error in NN) to increase G
  6. Repeat from 2

See the implementation using Pytorch in my Github.

Population

I have tested the algorithm in Pong, CartPole and Lunar Lander. It takes forever to train in Pong and Lunar Lander: more of 96 horas de training cada uno en una GPU en la nube. There are several updates to this algorithm that can make it converge faster, which I have not discussed or implemented here. Check out Critical Actor Models and Upcoming Policy Optimization if you are interested in learning more.

Trolley

1wlhepk4me-s334ngw8mhqg-5416157

Condition:

Horizontal position, horizontal speed, pole angle, angular velocity

Behaviour:

Push the carriage to the left, Push the carriage to the right

Random politics game:

1ty7qktykcnzoo1b8j-ie8a-6249674

Random play: total reward 18

Policy of agents trained with REINFORCE:

1i2fwnr2iyevctybt6vm5uq-9680510

Trained agent – Total reward 434

Lander lunar

1muxe2ynxy8ierbjt4uxuzw-9306370

Random game agent

Condition:

The state is a matrix of 8 vector. I'm not sure what they represent.

Behaviour:

0: do nothing

1: left fire engine

2: Fire engine

3: right fire engine

Policy of agents trained with REINFORCE:

1pbsjgwjyxcy5tz-8zwhjka-3069322

Lunar Lander trained with REINFORCE

Stink

This was much more difficult to train. Trained on a GPU cloud server for days.

Condition: Image

Behaviour: Move palette left, move palette right

1rtvh57xmxnv3pkkzih1q1w-1060950

Trained agent

Reinforcement learning has progressed by leaps and bounds beyond REINFORCEMENT. My goal in this article was 1. learn the basics of reinforcement learning and 2. show how powerful even such simple methods can be to solve complex problems. I would love to try them on some “games” to make money like stock trading … I guess that's the holy grail among data scientists.

Github repository: https://github.com/kvsnoufal/reinforce

Shoulders of giants:

  1. Policy gradient algorithms (https://lilianweng.github.io/lil-log/2018/04/08/policy-gradient-algorithms.html)
  2. Deriving REINFORCE (https://medium.com/@thechrisyoon/deriving-policy-gradients-and-implementing-reinforce-f887949bd63)
  3. Udacity Reinforcement Learning Course (https://github.com/udacity/deep-reinforcement-learning)

About the Author

photo_noufal-4824971

Noufal kvs

Work at Dubai Holding, UAE as a data scientist. You can contact me at [email protected] O https://www.linkedin.com/in/kvsnoufal/

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker