In The Previous Parts:

  • First part explained and created the game environment.

Reinforcement Learning With Python | Part 1 | Creating The Environment

Designing and Building a Game Environment that allows RL agents to train on it and play it.

towardsdatascience.com

  • Second part discussed the process of training the DQN, explained DQNs and gave reasons to choose DQN over Q-Learning.

Deep Reinforcement Learning With Python | Part 2 | Creating & Training The RL Agent Using Deep Q…

In the first part, we went through making the game environment and explained it line by line. In this part, we are…

towardsdatascience.com

In This Part:

We are going to:

  • Use Tensorboard to visualize the goodness of trained models.
  • Explain the way of loading and trying a trained model.
  • Use the best model and let it play the game.

Using Tensorboard:

1- Use a modified Tensorboard to make Tensorboard logs the data from all episodes in the training process in one log file instead of making a new log file every time we fit the model.

The next code of the ModifiedTensorBoard is from this blog by sentdex, I just changed it a little bit to make it run on TensorFlow2.0:

class ModifiedTensorBoard(TensorBoard):
	    # Overriding init to set initial step and writer (we want one log file for all .fit() calls)
	    def __init__(self, name, **kwargs):
	        super().__init__(**kwargs)
	        self.step = 1
	        self.writer = tf.summary.create_file_writer(self.log_dir)
	        self._log_write_dir = os.path.join(self.log_dir, name)

	    # Overriding this method to stop creating default log writer
	    def set_model(self, model):
	        pass

	    # Overrided, saves logs with our step number
	    # (otherwise every .fit() will start writing from 0th step)
	    def on_epoch_end(self, epoch, logs=None):
	        self.update_stats(**logs)

	    # Overrided
	    # We train for one batch only, no need to save anything at epoch end
	    def on_batch_end(self, batch, logs=None):
	        pass

	    # Overrided, so won't close writer
	    def on_train_end(self, _):
	        pass

	    def on_train_batch_end(self, batch, logs=None):
	        pass

	    # Custom method for saving own metrics
	    # Creates writer, writes custom metrics and closes writer
	    def update_stats(self, **stats):
	        self._write_logs(stats, self.step)

	    def _write_logs(self, logs, index):
	        with self.writer.as_default():
	            for name, value in logs.items():
	                tf.summary.scalar(name, value, step=index)
	                self.step += 1
	                self.writer.flush()
view raw
ModifiedTensorBoard.py hosted with ❤ by GitHub

#programming #machine-learning #artificial-intelligence #data-science #reinforcement-learning #deep learning

Part 3| Using Tensorboard to Analyse Trained Models
7.40 GEEK