Coding train_test.py file

We will now code the train_test.py file.

  1. Importing the packages: First, we import the required packages:
import tensorflow as tfimport numpy as npimport matplotlib.pyplot as pltimport gymimport sysimport timefrom class_ppo import *
  1. Define function: We then define a function for reward shaping that will give out some extra bonus rewards and penalties for good and bad performance, respectively. We do this for encouraging the car to go higher towards the side of the flag which is on the mountain top, without which the learning will be slow:
def reward_shaping(s_):     r = 0.0     if s_[0] > -0.4:          r += 5.0*(s_[0] + 0.4)     if s_[0] > 0.1:           r += 100.0*s_[0]     if s_[0] < -0.7:          r += 5.0*(-0.7 - s_[0]) if s_[0] < 0.3 and np.abs(s_[1]) ...

Get TensorFlow Reinforcement Learning Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.