![]() |
VOOZH | about |
REINFORCE is a method used in reinforcement learning to improve how decisions are made. It learns by trying actions and then adjusting the chances of those actions based on the total reward received afterwards. Unlike other methods that estimate how good each action is REINFORCE directly learns the best way to choose actions. This makes it useful for tasks where there are many possible actions or continuous choices and when it is hard to estimate the value of each action.
The REINFORCE algorithm works in the following steps:
1. Collect Episodes: The agent interacts with the environment for a fixed number of steps or until an episode is complete, following the current policy. This generates a trajectory consisting of states, actions and rewards.
2. Calculate Returns: For each time step , calculate the return β which is the total reward obtained from time onwards. Typically, this is the discounted sum of rewards:
Where is the discount factor, is the final time step of the episode and β is the reward received at time step .
3. Policy Gradient Update: The policy parameters are updated using the following formula:
Where:
4. Repeat: This process is repeated for several episodes, iteratively updating the policy in the direction of higher rewards.
In this example we will train a policy network to solve a basic environment such as CartPole from OpenAI's gym. The aim is to use REINFORCE to directly optimize the policy without using value function approximations.
The first step is to create the environment using OpenAI's Gym. For this example we use the CartPole-v1 environment where the agent's task is to balance a pole on a cart.
In this step we define hyperparameters for the algorithm like discount factor gamma, the learning rate, number of episodes and batch size. These hyperparameters control how the algorithm behaves during training.
We define the policy network as a simple neural network with two dense layers. The input to the network is the state and the output is a probability distribution over the actions (softmax output). The network learns the policy that maps states to action probabilities.
Here, we initialize the policy network and the Adam optimizer. The optimizer is used to update the weights of the policy network during training.
In reinforcement learning, the return is the discounted sum of future rewards. This function computes the return for each time step , based on the rewards collected during the episode.
The training step computes the gradients of the policy network using the log of action probabilities and the computed returns. The loss is the negative log-likelihood of the actions taken, weighted by the return. The optimizer updates the policy networkβs parameters to maximize the expected return.
The training loop collects experiences from episodes and then performs training in batches. The policy is updated after each batch of experiences. In each episode, we record the states, actions and rewards and then compute the returns. The policy is updated based on these returns.
After training the agent, we evaluate its performance by letting it run in the environment without updating the policy. The agent chooses actions based on the highest probabilities (greedy behavior).
Output:
Episode 0/1000
Episode 100/1000
Episode 200/1000
Episode 300/1000
Episode 400/1000
Episode 500/1000
Episode 600/1000
Episode 700/1000
Episode 800/1000
Episode 900/1000
Test Total Reward: 49.0
Several modifications to the original REINFORCE algorithm have been proposed to address its high variance:
The update rule becomes:
Where β is the baseline such as the expected reward from state β.
REINFORCE has been applied in several domains: