twin-delayed deep deterministic (td3) policy gradient agents
the twin-delayed deep deterministic policy gradient (td3) algorithm is a model-free, online, off-policy reinforcement learning method. a td3 agent is an actor-critic reinforcement learning agent that searches for an optimal policy that maximizes the expected cumulative long-term reward.
for more information on the different types of reinforcement learning agents, see reinforcement learning agents.
the td3 algorithm is an extension of the ddpg algorithm. ddpg agents can overestimate value functions, which can produce suboptimal policies. to reduce value function overestimation, the td3 algorithm includes the following modifications of the ddpg algorithm.
a td3 agent learns two q-value functions and uses the minimum value function estimate during policy updates.
a td3 agent updates the policy and targets less frequently than the q functions.
when updating the policy, a td3 agent adds noise to the target action, which makes the policy less likely to exploit actions with high q-value estimates.
you can use a td3 agent to implement one of the following training algorithms, depending on the number of critics you specify.
td3 — train the agent with two q-value functions. this algorithm implements all three of the preceding modifications.
delayed ddpg — train the agent with a single q-value function. this algorithm trains a ddpg agent with target policy smoothing and delayed policy and target updates.
td3 agents can be trained in environments with the following observation and action spaces.
observation space | action space |
---|---|
continuous or discrete | continuous |
td3 agents use the following actor and critics.
critics | actor |
---|---|
one or more q-value function critics
q(s,a), which you create
using | deterministic policy actor π(s),
which you create using |
during training, a td3 agent:
updates the actor and critic properties at each time step during learning.
stores past experiences using a circular experience buffer. the agent updates the actor and critic using a mini-batch of experiences randomly sampled from the buffer.
perturbs the action chosen by the policy using a stochastic noise model at each training step.
actor and critic functions
to estimate the policy and value function, a td3 agent maintains the following function approximators:
deterministic actor π(s;θ) — the actor, with parameters θ, takes observation s and returns the corresponding action that maximizes the long-term reward.
target actor πt(s;θt) — to improve the stability of the optimization, the agent periodically updates the target actor parameters θt using the latest actor parameter values.
one or two q-value critics qk(s,a;ϕk) — the critics, each with different parameters ϕk, take observation s and action a as inputs and returns the corresponding expectation of the long-term reward.
one or two target critics qtk(s,a;ϕtk) — to improve the stability of the optimization, the agent periodically updates the target critic parameters ϕtk using the latest corresponding critic parameter values. the number of target critics matches the number of critics.
both π(s;θ) and πt(s;θt) have the same structure and parameterization.
for each critic, qk(s,a;ϕk) and qtk(s,a;ϕtk) have the same structure and parameterization.
when using two critics, q1(s,a;ϕ1) and q2(s,a;ϕ2), each critic can have a different structure, though td3 works best when the critics have the same structure. when the critics have the same structure, they must have different initial parameter values.
for more information on creating actors and critics for function approximation, see create policies and value functions.
during training, the agent tunes the parameter values in θ. after training, the parameters remain at their tuned value and the trained actor function approximator is stored in π(s).
agent creation
you can create and train td3 agents at the matlab® command line or using the reinforcement learning designer app. for more information on creating agents using reinforcement learning designer, see .
at the command line, you can create a td3 agent with default actor and critics based on the observation and action specifications from the environment. to do so, perform the following steps.
create observation specifications for your environment. if you already have an environment interface object, you can obtain these specifications using
getobservationinfo
.create action specifications for your environment. if you already have an environment interface object, you can obtain these specifications using
getactioninfo
.if needed, specify the number of neurons in each learnable layer or whether to use an lstm layer. to do so, create an agent initialization option object using .
if needed, specify agent options using an
rltd3agentoptions
object.create the agent using an
rltd3agent
object.
alternatively, you can create actor and critics and use these objects to create your agent. in this case, ensure that the input and output dimensions of the actor and critics match the corresponding action and observation specifications of the environment.
create an actor using an
rlcontinuousdeterministicactor
object.create one or two critics using
rlqvaluefunction
objects.specify agent options using an
rltd3agentoptions
object.create the agent using an
rltd3agent
object.
for more information on creating actors and critics for function approximation, see create policies and value functions.
training algorithm
td3 agents use the following training algorithm, in which they update their actor and
critic models at each time step. to configure the training algorithm, specify options using
an rltd3agentoptions
object. here, k = 2 is the number of critics and k is the critic
index.
initialize each critic qk(s,a;ϕk) with random parameter values ϕk, and initialize each target critic with the same random parameter values: .
initialize the actor π(s;θ) with random parameter values θ, and initialize the target actor with the same parameter values: .
for each training time step:
for the current observation s, select action a = π(s;θ) n, where n is stochastic noise from the noise model. to configure the noise model, use the
explorationmodel
option.execute action a. observe the reward r and next observation s'.
store the experience (s,a,r,s') in the experience buffer.
sample a random mini-batch of m experiences (si,ai,ri,s'i) from the experience buffer. to specify m, use the
minibatchsize
option.if s'i is a terminal state, set the value function target yi to ri. otherwise, set it to
the value function target is the sum of the experience reward ri and the minimum discounted future reward from the critics. to specify the discount factor γ, use the
discountfactor
option.to compute the cumulative reward, the agent first computes a next action by passing the next observation s'i from the sampled experience to the target actor. then, the agent adds noise ε to the computed action using the
targetpolicysmoothmodel
, and clips the action based on the upper and lower noise limits. the agent finds the cumulative rewards by passing the next action to the target critics.if you specify a value of
numstepstolookahead
equal to n, then the n-step return (which adds the rewards of the following n steps and the discounted estimated value of the state that caused the n-th reward) is used to calculate the target yi.at every time training step, update the parameters of each critic by minimizing the loss lk across all sampled experiences.
every d1 steps, update the actor parameters using the following sampled policy gradient to maximize the expected discounted reward. to set d1, use the
policyupdatefrequency
option.here, gai is the gradient of the minimum critic output with respect to the action computed by the actor network, and gπi is the gradient of the actor output with respect to the actor parameters. both gradients are evaluated for observation si.
every d2 steps, update the target actor and critics depending on the target update method. to specify d2, use the
targetupdatefrequency
option. for more information, see target update methods.
for simplicity, the actor and critic updates in this algorithm show a gradient update
using basic stochastic gradient descent. the actual gradient update method depends on the
optimizer you specify using in the rloptimizeroptions
object assigned to
the rlcriticoptimizeroptions
property.
target update methods
td3 agents update their target actor and critic parameters using one of the following target update methods.
smoothing — update the target parameters at every time step using smoothing factor τ. to specify the smoothing factor, use the
targetsmoothfactor
option.periodic — update the target parameters periodically without smoothing (
targetsmoothfactor = 1
). to specify the update period, use thetargetupdatefrequency
parameter.periodic smoothing — update the target parameters periodically with smoothing.
to configure the target update method, create a rltd3agentoptions
object, and set the targetupdatefrequency
and
targetsmoothfactor
parameters as shown in the following table.
update method | targetupdatefrequency | targetsmoothfactor |
---|---|---|
smoothing (default) | 1 | less than 1 |
periodic | greater than 1 | 1 |
periodic smoothing | greater than 1 | less than 1 |
references
[1] fujimoto, scott, herke van hoof, and david meger. "addressing function approximation error in actor-critic methods". arxiv:1802.09477 [cs, stat], 22 october 2018. .