Getting started with PyGeneses
Tags: machinelearning, deeplearning, reinforcementlearning, ai
Published: 2020-09-06 15:12:48

What is PyGeneses? What is this blog post about? Well if you have no idea what PyGeneses is then I would suggest you to first go through the <a href="https://dev.to/projectdc/introduction-to-pygeneses-26oc">introductory post</a>. If you know the answer to these two questions, then you are in the right place. Let’s get started.

In this post we will go through installation process and PyGeneses’ training and hyperparameter tuning packages.

# Installation

PyGeneses can be installed using pip in either your local system or a cloud based platform. The steps for installation will be the same for both cloud and own system. Let’s install PyGeneses:-

```bash
user@programmer:~$ pip install git+https://github.com/Project-DC/pygeneses
```

It will soon be available in pypi, till then installation will be done from the official GitHub repo for <a href="https://github.com/Project-DC/pygeneses">PyGeneses</a>.

# Let's start training

## pygeneses.envs

Now that we have installed PyGeneses, let’s write some code to train the PrimaVita agents. First, we will look at the most basic code example to train agents with default hyperparameter setting and REINFORCE algorithm (we will be adding more RL algorithms in near future).

{% gist https://gist.github.com/frankhart2018/5e2af613ba737750b6489a56db773deb %}

Yeah it is just these 3 lines. The three steps to train agents are:-

1) Import PrimaVita (First life) class from envs.prima_vita.

2) Instanstiate the PrimaVita class.

3) Call the run() method on the PrimaVita object.

That was super simple wasn’t it. This default setting will allow you to train the agents in ‘bot’ mode which means you will not be able to see the environment while training, in case you want to see the environment throughout training you just need to change the mode to ‘human’:-

{% gist https://gist.github.com/frankhart2018/a638d5e8287a3461c2bb949928b4f19a %}

Let us now look at an example where the default hyperparameters are changed:-

{% gist https://gist.github.com/frankhart2018/40efb6091d89a037ab598c4aab59ddde %}

You just have to pass a dictionary with keys as the name of hyperparameter (here the initial energy of agents and the time in ticks after which model will be updated are changed) and the values as the value you want to specify. For a more detailed list of available hyperparameters refer to the <a href="https://project-dc.github.io/docs">official docs</a>. Just one more line and you can customize your training.

## pygeneses.hypertune

Now let’s move on to hyperparameter tuning package (hypertune) of PyGeneses. PyGeneses has two different algorithms for trying out different hyperparameter values:-

1) <strong>Grid Search:</strong> This takes in a pool of hyperparameter values as input and trains the model with all the possible combinations of these values. This is useful when you want to try out different values of a hyperparameter or group of hyperparameters.

Following is a snippet of code for applying a grid search over pool of hyperparameters using hypertune:-

{% gist https://gist.github.com/frankhart2018/3cee63273f1e32ed4e5092d1c787ebe2 %}

This too in 3 lines of code

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/38rkrosckqdyasryof3u.png)

Again the three steps are as follows:-

1) Import HyperTune class from pygeneses.hypertune.

2) Instantiate HyperTune class with the name of environment class, hyperparameter list, hyperparameter values (2D list) and number of logs to be generated for each run.

3) Finally call hypertuner( ) method of HyperTune class.

The values are to be listed in order in which the hyperparameters are listed.

Moving on to the next algorithm:-

2) <strong>Randomized Search:</strong> This is similar to grid search in that it too takes a pool of hyperparameter values but instead of training model with all possible combinations it selects a subset out of those combinations randomly based on a probability.

Here is the code example:-

{% gist https://gist.github.com/frankhart2018/302e9eb206a79bbb8f3acb8cc146a6d0 %}

The only change in this code and the grid search code is the extra parameter randomize_percent which tells the randomized search algorithm the percentage of combinations to pick up randomly from the pool of available hyperparameters.

This was the end to this part of PyGeneses blog post series. In the next part we will talk about VitaBoard — an interactive dashboard for visualization of training results in PyGeneses which helps makes sense of the actions taken by agents during training and studying their behaviour. See you there :)