CartPole RL TF
Training RL to do Cartpole Balancing
This notebooks is part of AI for Beginners Curriculum. It has been inspired by this blog post, official TensorFlow documentation and this Keras RL example.
In this example, we will use RL to train a model to balance a pole on a cart that can move left and right on horizontal scale. We will use OpenAI Gym environment to simulate the pole.
Note: You can run this lesson's code locally (eg. from Visual Studio Code), in which case the simulation will open in a new window. When running the code online, you may need to make some tweaks to the code, as described here.
We will start by making sure Gym is installed:
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: gym in /home/leo/.local/lib/python3.10/site-packages (0.25.0) Requirement already satisfied: pygame in /home/leo/.local/lib/python3.10/site-packages (2.1.2) Requirement already satisfied: gym-notices>=0.0.4 in /home/leo/.local/lib/python3.10/site-packages (from gym) (0.0.7) Requirement already satisfied: cloudpickle>=1.2.0 in /home/leo/.local/lib/python3.10/site-packages (from gym) (2.1.0) Requirement already satisfied: numpy>=1.18.0 in /usr/lib/python3/dist-packages (from gym) (1.21.5)
Now let's create the CartPole environment and see how to operate on it. An environment has the following properties:
- Action space is the set of possible actions that we can perform at each step of the simulation
- Observation space is the space of observations that we can make
Action space: Discrete(2) Observation space: Box([-4.8000002e+00 -3.4028235e+38 -4.1887903e-01 -3.4028235e+38], [4.8000002e+00 3.4028235e+38 4.1887903e-01 3.4028235e+38], (4,), float32)
/home/leo/.local/lib/python3.10/site-packages/gym/core.py:329: DeprecationWarning: WARN: Initializing wrapper in old step API which returns one bool instead of two. It is recommended to set `new_step_api=True` to use new step API. This will be the default behaviour in future. deprecation( /home/leo/.local/lib/python3.10/site-packages/gym/wrappers/step_api_compatibility.py:39: DeprecationWarning: WARN: Initializing environment in old step API which returns one bool instead of two. It is recommended to set `new_step_api=True` to use new step API. This will be the default behaviour in future. deprecation(
Let's see how the simulation works. The following loop runs the simulation, until env.step does not return the termination flag done. We will randomly chose actions using env.action_space.sample(), which means the experiment will probably fail very fast (CartPole environment terminates when the speed of CartPole, its position or angle are outside certain limits).
Simulation will open in the new window. You can run the code several times and see how it behaves.
/home/leo/.local/lib/python3.10/site-packages/gym/core.py:57: DeprecationWarning: WARN: You are calling render method, but you didn't specified the argument render_mode at environment initialization. To maintain backward compatibility, the environment will render in human mode.
If you want to render in human mode, initialize the environment in this way: gym.make('EnvName', render_mode='human') and don't call the render method.
See here for more information: https://www.gymlibrary.ml/content/api/
deprecation(
[ 0.00425272 -0.19994313 0.00917169 0.34113726] -> 1.0 [ 0.00025386 -0.00495286 0.01599443 0.05136059] -> 1.0 [ 1.5480528e-04 1.8993615e-01 1.7021643e-02 -2.3623335e-01] -> 1.0 [ 0.00395353 0.38481084 0.01229698 -0.5234989 ] -> 1.0 [ 0.01164974 0.18951797 0.001827 -0.22696657] -> 1.0 [ 0.0154401 0.38461378 -0.00271233 -0.51907265] -> 1.0 [ 0.02313238 0.5797738 -0.01309379 -0.812609 ] -> 1.0 [ 0.03472786 0.38483363 -0.02934597 -0.5240733 ] -> 1.0 [ 0.04242453 0.580356 -0.03982743 -0.8258571 ] -> 1.0 [ 0.05403165 0.38580072 -0.05634458 -0.54596174] -> 1.0 [ 0.06174766 0.19151384 -0.06726381 -0.27155042] -> 1.0 [ 0.06557794 -0.00258703 -0.07269482 -0.00081817] -> 1.0 [ 0.0655262 -0.19659522 -0.07271118 0.26807207] -> 1.0 [ 0.0615943 -0.00051497 -0.06734974 -0.04662942] -> 1.0 [ 0.061584 0.19550486 -0.06828233 -0.3597784 ] -> 1.0 [ 0.06549409 0.00141663 -0.0754779 -0.08938391] -> 1.0 [ 0.06552242 -0.19254686 -0.07726558 0.17856352] -> 1.0 [ 0.06167149 0.00359088 -0.0736943 -0.1374588 ] -> 1.0 [ 0.0617433 0.19968675 -0.07644348 -0.45245075] -> 1.0 [ 0.06573704 0.3958018 -0.0854925 -0.7682167 ] -> 1.0 [ 0.07365308 0.20195423 -0.10085683 -0.50361156] -> 1.0 [ 0.07769216 0.0083876 -0.11092906 -0.24433874] -> 1.0 [ 0.07785992 -0.18498953 -0.11581583 0.01139782] -> 1.0 [ 0.07416012 0.01158649 -0.11558788 -0.31546465] -> 1.0 [ 0.07439185 0.20814891 -0.12189718 -0.64224803] -> 1.0 [ 0.07855483 0.01491799 -0.13474214 -0.3903015 ] -> 1.0 [ 0.07885319 -0.17806001 -0.14254816 -0.14295265] -> 1.0 [ 0.07529199 0.01878517 -0.14540721 -0.47699296] -> 1.0 [ 0.07566769 -0.17401667 -0.15494707 -0.23344138] -> 1.0 [ 0.07218736 0.0229406 -0.1596159 -0.57071024] -> 1.0 [ 0.07264617 0.21989843 -0.1710301 -0.9091196 ] -> 1.0 [ 0.07704414 0.02745241 -0.1892125 -0.6747003 ] -> 1.0 [ 0.07759319 -0.16460665 -0.20270652 -0.4470505 ] -> 1.0 [ 0.07430106 -0.35637102 -0.21164753 -0.22448184] -> 1.0 Total reward: 34.0
Youn can notice that observations contain 4 numbers. They are:
- Position of cart
- Velocity of cart
- Angle of pole
- Rotation rate of pole
rew is the reward we receive at each step. You can see that in CartPole environment you are rewarded 1 point for each simulation step, and the goal is to maximize total reward, i.e. the time CartPole is able to balance without falling.
During reinforcement learning, our goal is to train a policy , that for each state will tell us which action to take, so essentially .
If you want probabilistic solution, you can think of policy as returning a set of probabilities for each action, i.e. would mean a probability that we should take action at state .
Policy Gradient Method
In simplest RL algorithm, called Policy Gradient, we will train a neural network to predict the next action.
/usr/local/lib/python3.10/dist-packages/tensorflow/__init__.py:29: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives import distutils as _distutils 2022-07-24 16:50:47.597258: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory 2022-07-24 16:50:47.597280: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. /usr/local/lib/python3.10/dist-packages/flatbuffers/compat.py:19: DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses import imp 2022-07-24 16:50:49.838826: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:975] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-07-24 16:50:49.839078: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839143: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcublas.so.11'; dlerror: libcublas.so.11: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839194: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcublasLt.so.11'; dlerror: libcublasLt.so.11: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839245: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcufft.so.10'; dlerror: libcufft.so.10: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839295: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcurand.so.10'; dlerror: libcurand.so.10: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839345: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcusolver.so.11'; dlerror: libcusolver.so.11: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839392: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcusparse.so.11'; dlerror: libcusparse.so.11: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839441: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudnn.so.8'; dlerror: libcudnn.so.8: cannot open shared object file: No such file or directory 2022-07-24 16:50:49.839449: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2022-07-24 16:50:49.839649: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
We will train the network by running many experiments, and updating our network after each run. Let's define a function that will run the experiment and return the results (so-called trace) - all states, actions (and their recommended probabilities), and rewards:
You can run one episode with untrained network and observe that total reward (AKA length of episode) is very low:
Total reward: 27.0
One of the tricky aspects of policy gradient algorithm is to use discounted rewards. The idea is that we compute the vector of total rewards at each step of the game, and during this process we discount the early rewards using some coefficient . We also normalize the resulting vector, because we will use it as weight to affect our training:
Now let's do the actual training! We will run 300 episodes, and at each episode we will do the following:
- Run the experiment and collect the trace
- Calculate the difference (
gradients) between the actions taken, and by predicted probabilities. The less the difference is, the more we are sure that we have taken the right action. - Calculate discounted rewards and multiply gradients by discounted rewards - that will make sure that steps with higher rewards will make more effect on the final result than lower-rewarded ones
- Expected target actions for our neural network would be partly taken from the predicted probabilities during the run, and partly from calculated gradients. We will use
alphaparameter to determine to which extent gradients and rewards are taken into account - this is called learning rate of reinforcement algorithm. - Finally, we train our network on states and expected actions, and repeat the process
0 -> 29.0
2022-07-24 16:50:51.475024: W tensorflow/core/data/root_dataset.cc:247] Optimization loop failed: CANCELLED: Operation was cancelled
100 -> 135.0 200 -> 484.0
2022-07-24 16:51:35.910774: W tensorflow/core/data/root_dataset.cc:247] Optimization loop failed: CANCELLED: Operation was cancelled 2022-07-24 16:51:37.151017: W tensorflow/core/data/root_dataset.cc:247] Optimization loop failed: CANCELLED: Operation was cancelled 2022-07-24 16:51:39.284311: W tensorflow/core/data/root_dataset.cc:247] Optimization loop failed: CANCELLED: Operation was cancelled 2022-07-24 16:51:42.235074: W tensorflow/core/data/root_dataset.cc:247] Optimization loop failed: CANCELLED: Operation was cancelled 2022-07-24 16:51:44.691458: W tensorflow/core/data/root_dataset.cc:247] Optimization loop failed: CANCELLED: Operation was cancelled 2022-07-24 16:51:48.381946: W tensorflow/core/data/root_dataset.cc:247] Optimization loop failed: CANCELLED: Operation was cancelled
[<matplotlib.lines.Line2D at 0x7f40201e7b20>]
Now let's run the episode with rendering to see the result:
--------------------------------------------------------------------------- error Traceback (most recent call last) /tmp/ipykernel_44248/1459719159.py in <module> ----> 1 _ = run_episode(render=True) /tmp/ipykernel_44248/3855001447.py in run_episode(max_steps_per_episode, render) 4 for _ in range(max_steps_per_episode): 5 if render: ----> 6 env.render() 7 action_probs = model(np.expand_dims(state,0))[0] 8 action = np.random.choice(num_actions, p=np.squeeze(action_probs)) ~/.local/lib/python3.10/site-packages/gym/core.py in render(self, *args, **kwargs) 64 ) 65 ---> 66 return render_func(self, *args, **kwargs) 67 68 return render ~/.local/lib/python3.10/site-packages/gym/core.py in render(self, *args, **kwargs) 429 def render(self, *args, **kwargs): 430 """Renders the environment.""" --> 431 return self.env.render(*args, **kwargs) 432 433 def close(self): ~/.local/lib/python3.10/site-packages/gym/core.py in render(self, *args, **kwargs) 64 ) 65 ---> 66 return render_func(self, *args, **kwargs) 67 68 return render ~/.local/lib/python3.10/site-packages/gym/wrappers/order_enforcing.py in render(self, *args, **kwargs) 49 "set `disable_render_order_enforcing=True` on the OrderEnforcer wrapper." 50 ) ---> 51 return self.env.render(*args, **kwargs) 52 53 @property ~/.local/lib/python3.10/site-packages/gym/core.py in render(self, *args, **kwargs) 64 ) 65 ---> 66 return render_func(self, *args, **kwargs) 67 68 return render ~/.local/lib/python3.10/site-packages/gym/core.py in render(self, *args, **kwargs) 429 def render(self, *args, **kwargs): 430 """Renders the environment.""" --> 431 return self.env.render(*args, **kwargs) 432 433 def close(self): ~/.local/lib/python3.10/site-packages/gym/core.py in render(self, *args, **kwargs) 64 ) 65 ---> 66 return render_func(self, *args, **kwargs) 67 68 return render ~/.local/lib/python3.10/site-packages/gym/wrappers/env_checker.py in render(self, *args, **kwargs) 53 return env_render_passive_checker(self.env, *args, **kwargs) 54 else: ---> 55 return self.env.render(*args, **kwargs) ~/.local/lib/python3.10/site-packages/gym/core.py in render(self, *args, **kwargs) 64 ) 65 ---> 66 return render_func(self, *args, **kwargs) 67 68 return render ~/.local/lib/python3.10/site-packages/gym/envs/classic_control/cartpole.py in render(self, mode) 215 return self.renderer.get_renders() 216 else: --> 217 return self._render(mode) 218 219 def _render(self, mode="human"): ~/.local/lib/python3.10/site-packages/gym/envs/classic_control/cartpole.py in _render(self, mode) 296 297 self.surf = pygame.transform.flip(self.surf, False, True) --> 298 self.screen.blit(self.surf, (0, 0)) 299 if mode == "human": 300 pygame.event.pump() error: display Surface quit
Hopefully, you can see that pole can now balance pretty well!
Actor-Critic Model
Actor-Critic model is the further development of policy gradients, in which we build a neural network to learn both the policy and estimated rewards. The network will have two outputs (or you can view it as two separate networks):
- Actor will recommend the action to take by giving us the state probability distribution, as in policy gradient model
- Critic would estimate what the reward would be from those actions. It returns total estimated rewards in the future at the given state.
Let's define such a model:
We would need to slightly modify our run_episode function to return also critic results:
Now we will run the main training loop. We will use manual network training process by computing proper loss functions and updating network parameters:
running reward: 5.82 at episode 10 running reward: 9.43 at episode 20 running reward: 10.30 at episode 30 running reward: 10.28 at episode 40 running reward: 11.00 at episode 50 running reward: 13.01 at episode 60 running reward: 21.78 at episode 70 running reward: 40.54 at episode 80 running reward: 73.70 at episode 90 running reward: 100.19 at episode 100 running reward: 159.20 at episode 110 Solved at episode 114!
Let's run the episode and see how good our model is:
Finally, let's close the environment.
Takeaway
We have seen two RL algorithms in this demo: simple policy gradient, and more sophisticated actor-critic. You can see that those algorithms operate with abstract notions of state, action and reward - thus they can be applied to very different environments.
Reinforcement learning allows us to learn the best strategy to solve the problem just by looking at the final reward. The fact that we do not need labelled datasets allows us to repeat simulations many times to optimize our models. However, there are still many challenges in RL, which you may learn if you decide to focus more on this interesting area of AI.