Reinforcement learning is an important type of Machine Learning where an agent learns how to behave in an environment by performing actions and seeing the results. It seeks to create intelligent agents that adapt to the environment by analyzing their own experiences. It is a computational approach to learn something from the action.
Let’s understand this in simple terms.
My son Rigved started playing with the TV remote when he was just 2 years old. At the age of 3, he started liking kids channels but he did not know how to tune in to those channels. So, he began pressing the keys randomly and used to feel happy upon finding those channels. Gradually, he started memorizing the keys (combination only and not the numbers). Though to reach this stage he failed multiple times but, slowly became perfect in tuning to those kids channels.
This is nothing but reinforcement learning – starting without any data, collecting data with random attempts based on the problem defined in a domain. All results get memorized as either success or failure.
Let’s understand this better with a simple game: Landing A Rocket
In our demo, the environment is a Lunar Lander which gives us the states as observations to the agent and the rewards that the agent receives as it tries to beat the environment. Lunar Lander is an environment taken from OpenAI gym. OpenAI’s gym is a library which provides a huge array of learning environments.
Agent: Lunar Lander State: A pixel data from the screen represented by a state vector. This represents state space(continuous). Action: Lunar Lander has 4 possible actions: do-nothing, a fire left orientation engine, fire main engine, fire right orientation engine. This represents the output space(discrete). Reward: Reward may be positive or negative. A successful landing agent gets 200 reward points. If an agent moves away from the landing pad, it loses reward points. Goal: To safely land on the landing pad. Episode: Episode finishes if the agent crashes or comes to rest or after 5000 timesteps. Working: It works on the Monte Carlo Approach. It is episodic i.e. it gives the output for each episode and that knowledge is used for the next episode. Every single output is used as a reference for the next episode.
The expected solution is to safely land the agent on the launch pad with consistency on both legs with the average cumulative reward of 200 over 100 episodes.
Points to remember before training the agent:
Agent always starts at the same starting point.
We terminate the episode after a certain number of steps or when it reaches the goal state.
At the end of the episode, we have a list of States, Actions, Rewards, and the New States.
The agent will sum the total rewards. It updates its experience based on top ‘x’ percentile rewards.
Then start a new game with this new knowledge.
By running more and more episodes, the agent will learn to play better and better.
The method our agent uses to learn is the Cross-Entropy Method.
Now we are ready to train the Agent. The agent will be trained using a forward neural net of architecture 8(nodes in input layer)*200 (nodes in hidden layer)*4(nodes in the output layer) and weights will be updated for every session. This can be done by updating the session_size variable in the training agent code.
Because the agent was new to the environment, he got crashed.
Again the agent was trained for 50 sessions.
As you can see, he was almost close to the goal state but had fired right engine more when it was not required. As a result, he had to undergo more training.
This time the agent was trained for 100 sessions.
This time again he was close but did not move into the goal state. Showed some improvement though as while he was near to the goal state, he had not fired any engines.
Back to training again, update session size to 150.
Even worse than the previous result. He was a little overconfident and went very fast.
Update session size to 200 now.
This time he was a little careful and successfully landed in the goal state.
Now you must think about how he has learned this on his own. Some work to your brains. 😛
To implement the deep cross-entropy method, we need to follow a few steps as described in the flowchart below:
The total reward received for each episode is recorded. A batch of these episodes is then generated, ~100 episodes per batch. Once we have gathered the data of episodes from the batch, we can pick the episodes that performed the best in that batch.
Shiva Prasad Koyyada is a DataScientist training technical and nontechnical individuals in data science, consulting with various clients across domains since 2016. He has worked as a faculty in various reputed engineering institutions for 6 years.
Shiva is passionate about connecting with students and hence his love for teaching continues. He is known for his patience and the instant rapport he builds with people. For him, spirituality is the essence of his life.
Shiva believes in 'Making it Simple' in his professional and personal life.