SLM Lab
Search…
REINFORCE
REINFORCE Williams, 1992 directly learns a parameterized policy,
π\pi
, which maps states to probability distributions over actions.
Starting with random parameter values, the agent uses this policy to act in an environment and receive rewards. After an episode has finished, the "goodness" of each action, represented by,
f(τ)f(\tau)
, is calculated using the episode trajectory. The parameters of the policy are then updated in a direction which makes good actions
(f(τ)>0)(f(\tau) > 0)
more likely, and bad actions
(f(τ)<0)(f(\tau) < 0)
less likely. Good actions are reinforced, bad actions are discouraged.
The agent then uses the updated policy to act in the environment, and the training process repeats.
REINFORCE is an on policy algorithm. Only data that is gathered using the current policy can be used to update the parameters. Once the policy parameters have been updated all previous data gathered must be discarded and the collection process started again with the new policy.
There are a number of different approaches to calculating
f(τ)f(\tau)
. Method 3, outlined below, is common. It captures the idea that the absolute quality of the actions matters less than their quality relative to some baseline. One option for a baseline is the average of
f(τ)f(\tau)
over the training data (typically one episode trajectory).
Algorithm: REINFORCE with baseline
Initialize weights θ, learning rate αfor each episode (trajectory) τ={s0,a0,r0,s1,,rT}πθfor t=0 to T doθθ+α f(τ)tθlog πθ(atst)end forend for\begin{aligned} & \text{Initialize weights } \theta \text{, learning rate } \alpha \\ & \text{for each episode (trajectory) } \tau = \{s_0, a_0, r_0, s_1, \cdots, r_T\} \sim \pi_\theta \\ & \quad \text{for } t = 0 \text{ to } T \text{ do} \\ & \quad \quad \theta \leftarrow \theta + \alpha \ f(\tau)_t \nabla_\theta log ~ \pi_\theta(a_t|s_t) \\ & \quad \text{end for} \\ & \text{end for} \\ \end{aligned}
Methods for calculating
f(τ)tf(\tau)_t
:
Given θJ(θ) t0f(τ)θlogπθ(atst), improve baseline with: 1. reward as weightage f(τ)=ttrt2. add discount factor f(τ)=ttγttrt3. introduce baseline f(τ)=ttγttrtb(st)\begin{aligned} & \text{Given } \nabla_\theta J(\theta) \ \approx \sum_{t \geq 0} f(\tau) \nabla_\theta log \pi_\theta(a_t|s_t), ~ \text{improve baseline with: }\\ & \quad \quad 1.\ \text{reward as weightage } f(\tau) = \sum\limits_{t' \geq t} r_{t'} \\ & \quad \quad 2.\ \text{add discount factor } f(\tau) = \sum\limits_{t' \geq t} \gamma^{t'-t} r_{t'} \\ & \quad \quad 3.\ \text{introduce baseline } f(\tau) = \sum\limits_{t' \geq t} \gamma^{t'-t} r_{t'} - b(s_t) \\ \end{aligned}
See reinforce.json for example specs of variations of the REINFORCE algorithm.
Basic Parameters
1
"agent": [{
2
"name": str,
3
"algorithm": {
4
"name": str,
5
"action_pdtype": str,
6
"action_policy": str,
7
"gamma": float,
8
"training_frequency": int,
9
"add_entropy": bool,
10
"entropy_coef": float,
11
},
12
"memory": {
13
"name": str,
14
"max_size": int
15
"batch_size": int
16
},
17
"net": {
18
"type": str,
19
"hid_layers": list,
20
"hid_layers_activation": str,
21
"optim_spec": dict,
22
}
23
}],
24
...
25
}
Copied!
  • algorithm
    • action_pdtype general param
    • action_policy string specifying which policy to use to act. For example, "Categorical" (for discrete action spaces), "Normal" (for continuous actions spaces with one dimension), or "default" to automatically switch between the two depending on the environment.
    • gamma general param
    • training_frequency how many episodes of data to collect before each training iteration. A common value is 1.
    • entropy whether to add entropy to the
      f(τ)tf(\tau)_t
      to encourage exploration
    • entropy_coef coefficient to multiply the entropy of the distribution with when adding it to
      f(τ)tf(\tau)_t
  • memory
  • net
Advanced Parameters
1
"agent": [{
2
"net": {
3
"rnn_hidden_size": int,
4
"rnn_num_layers": int,
5
"seq_len": int,
6
"clip_grad": bool,
7
"clip_grad_val": float,
8
"lr_decay": str,
9
"lr_decay_frequency": int,
10
"lr_decay_min_timestep": int,
11
"lr_anneal_timestep": int,
12
"gpu": int
13
14
}
15
}],
16
...
17
}
Copied!
Last modified 5mo ago
Copy link