rlTD3AgentOptions
Options for TD3 agent
Description
Use an rlTD3AgentOptions
object to specify options when creating
a twin-delayed deep deterministic policy gradient (TD3) agent. To create a TD3 agent, use
rlTD3Agent
.
For more information see Twin-Delayed Deep Deterministic (TD3) Policy Gradient Agent.
For more information on the different types of reinforcement learning agents, see Reinforcement Learning Agents.
Creation
Description
creates an options
object for use as an argument when creating a TD3 agent using all default options. You can
modify the object properties using dot notation.opt
= rlTD3AgentOptions
creates the options object opt
= rlTD3AgentOptions(Name=Value
)opt
and sets its properties using one
or more name-value arguments. For example,
rlTD3AgentOptions(DiscountFactor=0.95)
creates an option set with a
discount factor of 0.95
. You can specify multiple name-value
arguments.
Properties
Sample time of the agent, specified as a positive scalar or as -1
.
Within a MATLAB® environment, the agent is executed every time the environment advances,
so, SampleTime
does not affect the timing of the agent execution.
If SampleTime
is set to -1
, in MATLAB environments, the time interval between consecutive elements in the
returned output experience is considered equal to 1
.
Within a Simulink® environment, the RL Agent block
that uses the agent object executes every SampleTime
seconds of
simulation time. If SampleTime
is set to -1
the
block inherits the sample time from its input signals. Set
SampleTime
to -1
when the block is a child
of an event-driven subsystem.
Set SampleTime
to a positive scalar when the block is not a child
of an event-driven subsystem. Doing so ensures that the block executes at appropriate
intervals when input signal sample times change due to model variations. If
SampleTime
is a positive scalar, this value is also the time
interval between consecutive elements in the output experience returned by sim
or
train
,
regardless of the type of environment.
If SampleTime
is set to -1
, in Simulink environments, the time interval between consecutive elements in the
returned output experience reflects the timing of the events that trigger the RL Agent block
execution.
This property is shared between the agent and the agent options object within the agent. If you change this property in the agent options object, it also changes in the agent, and vice versa.
Example: SampleTime=-1
Discount factor applied to future rewards during training, specified as a nonnegative scalar less than or equal to 1.
Example: DiscountFactor=0.9
Noise model options, specified as a GaussianActionNoise
object or
an OrnsteinUhlenbeckActionNoise
object. For more information on noise
models, see Noise Models.
For an agent with multiple actions, if the actions have different ranges and units, it is likely that each action requires different noise model parameters. If the actions have similar ranges and units, you can set the noise parameters for all actions to the same value.
For example, for an agent with two actions, set the initial standard deviation of each action to a different value while using the same decay rate for both standard deviations.
opt = rlTD3AgentOptions; opt.ExplorationModel.StandardDeviation = [0.1 0.2]; opt.ExplorationModel.StandardDeviationDecayRate = 1e-4;
To use Ornstein-Uhlenbeck action noise, first create a default
OrnsteinUhlenbeckActionNoise
object. Then, specify any nondefault
model properties using dot notation.
opt = rlTD3AgentOptions; opt.ExplorationModel = rl.option.OrnsteinUhlenbeckActionNoise; opt.ExplorationModel.StandardDeviation = 0.05;
Experience buffer size, specified as a positive integer. During training, the agent computes updates using a mini-batch of experiences randomly sampled from the buffer.
Example: ExperienceBufferLength=1e6
Size of random experience mini-batch, specified as a positive integer. During each training episode, the agent randomly samples experiences from the experience buffer when computing gradients for updating the critic properties. Large mini-batches reduce the variance when computing gradients but increase the computational effort.
Example: MiniBatchSize=128
Maximum batch-training trajectory length when using a recurrent neural network, specified as a positive integer. This value must be greater than 1
when using a recurrent neural network and 1
otherwise.
Example: SequenceLength=4
Actor optimizer options, specified as an rlOptimizerOptions
object. It allows you to specify training parameters of
the actor approximator such as learning rate, gradient threshold, as well as the
optimizer algorithm and its parameters. For more information, see rlOptimizerOptions
and rlOptimizer
.
Example: ActorOptimizerOptions =
rlOptimizerOptions(LearnRate=2e-3)
Critic optimizer options, specified as an rlOptimizerOptions
object. It allows you to specify training parameters of
the critic approximator such as learning rate, gradient threshold, as well as the
optimizer algorithm and its parameters. For more information, see rlOptimizerOptions
and rlOptimizer
.
Example: CriticOptimizerOptions =
rlOptimizerOptions(LearnRate=5e-3)
Number of future rewards used to estimate the value of the policy, specified as a positive
integer. Specifically,
ifNumStepsToLookAhead
is equal
to N, the target value of the policy at a
given step is calculated adding the rewards for the following
N steps and the discounted
estimated value of the state that caused the
N-th reward. This target is also
called N-step return.
Note
When using a recurrent neural network for the critic,
NumStepsToLookAhead
must be
1
.
For more information, see [1], Chapter 7.
Example: NumStepsToLookAhead=3
Minimum number of samples to generate before learning starts. Use this option to
ensure that learning takes place over a more diverse data set at the beginning of
training. The default, and minimum, value is the value of
MiniBatchSize
. After the software collects a minimum of
NumWarmStartSteps
samples, learning occurs at the intervals
specified by the LearningFrequency
property.
Example: NumWarmStartSteps=20
Number of times an agent learns over the data set stored in the experience buffer, specified as a positive integer. For off-policy agents that support this property (DQN, DDPG, TD3 and SAC), this value defines the number of passes over the data in the replay buffer at each learning iteration.
Example: NumEpoch=2
Maximum number of mini-batches used for learning during a single epoch, specified as a positive integer.
For off-policy agents that support this property (DQN, DDPG, TD3, and SAC), the actual
number of mini-batches used for learning depends on the length of the replay buffer, and
MaxMiniBatchPerEpoch
specifies the upper bound. This value also
specifies the maximum number of gradient steps per learning iteration because the
maximum number of gradient steps is equal to the
MaxMiniBatchPerEpoch
value multiplied by the
NumEpoch
value.
For off-policy agents that support this property, a high
MaxMiniBatchPerEpoch
value means that more time is spent on
learning than collecting new data. Therefore, you can use this parameter to control the
sample efficiency of the learning process.
Example: MaxMiniBatchPerEpoch=200
Minimum number of environment interactions between learning iterations, specified as a
positive integer or -1
. This value defines how many new data samples
need to be generated before learning. For DQN, DDPG, TD3, and SAC agents, the default
value of -1
means that learning occurs after each episode is
finished. Note that for these agents learning can start only after the software collects
a minimum of NumWarmStartSteps
samples. It then occurs at the
intervals specified by the LearningFrequency
property.
Example: LearningFrequency=4
Period of policy update with respect to critic update, specified as a positive
integer. This option defines how often the actor is updated with respect to each critic
update. For example, a value of 3
means that the actor is updated
every three critic updates. Updating the actor less frequently than the critic can
improve convergence at the cost of longer training times.
Example: PolicyUpdateFrequency=3
Target smoothing noise model options, specified as a
GaussianActionNoise
object. This model helps the policy exploit
actions with high Q-value estimates. For more information on noise models, see Noise Models.
For an agent with multiple actions, if the actions have different ranges and units, it is likely that each action requires different smoothing noise model parameters. If the actions have similar ranges and units, you can set the noise parameters for all actions to the same value.
For example, for an agent with two actions, set the standard deviation of each action to a different value while using the same decay rate for both standard deviations.
opt = rlTD3AgentOptions; opt.TargetPolicySmoothModel.StandardDeviation = [0.1 0.2]; opt.TargetPolicySmoothModel.StandardDeviationDecayRate = 1e-4;
Smoothing factor for target actor and critic updates, specified as a positive scalar less than or equal to 1. For more information, see Target Update Methods.
Example: TargetSmoothFactor=1e-2
Number of steps between target actor and critic updates, specified as a positive integer. For more information, see Target Update Methods.
Example: TargetUpdateFrequency=5
Batch data regularizer options, specified as an
rlBehaviorCloningRegularizerOptions
object. These options are
typically used to train the agent offline, from existing data. If you leave this option
empty, no regularizer is used.
For more information, see rlBehaviorCloningRegularizerOptions
.
Example: BatchDataRegularizerOptions =
rlBehaviorCloningRegularizerOptions(BehaviorCloningRegularizerWeight=10)
Option for clearing the experience buffer before training, specified as a logical value.
Example: ResetExperienceBufferBeforeTraining=true
Options to save additional agent data, specified as a structure containing the following fields.
Optimizer
PolicyState
Target
ExperienceBuffer
You can save an agent object using one of these methods:
Use the
save
command.Specify
saveAgentCriteria
andsaveAgentValue
in anrlTrainingOptions
object.Specify an appropriate logging function within a
FileLogger
object.
When you save an agent using any method, the fields in the
InfoToSave
structure determine whether the
corresponding data saves with the agent. For example, if you set the
PolicyState
field to true
,
then the policy state saves along with the agent.
You can modify the InfoToSave
property only after you
create the agent options object.
Example: options.InfoToSave.Optimizer=true
Option to save the actor and critic optimizers,
specified as a logical value. If you set the
Optimizer
field to
false
, then the actor and
critic optimizers (which are hidden properties of
the agent and can contain internal states) are not
saved along with the agent, therefore saving disk
space and memory. However, when the optimizers
contains internal states, the state of the saved
agent is not identical to the state of the original
agent.
Example: true
Option to save the state of the explorative policy,
specified as a logical value. If you set the
PolicyState
field to
false
, then the state of the
explorative policy (which is a hidden agent
property) is not saved along with the agent. In this
case, the state of the saved agent is not identical
to the state of the original agent.
Example: true
Option to save the actor and critic targets, specified
as a logical value. If you set the
Target
field to
false
, then the actor and
critic targets (which are hidden agent properties)
are not saved along with the agent. In this case,
when the targets contain internal states, the state
of the saved agent is not identical to the state of
the original agent.
Example: true
Option to save the experience buffer, specified as a
logical value. If you set the
PolicyState
field to
false
, then the content of the
experience buffer (which is accessible as an agent
property using dot notation) is not saved along with
the agent. In this case, the state of the saved
agent is not identical to the state of the original
agent.
Example: true
Object Functions
rlTD3Agent | Twin-delayed deep deterministic (TD3) policy gradient reinforcement learning agent |
Examples
Create an rlTD3AgentOptions
object that specifies the mini-batch size.
opt = rlTD3AgentOptions(MiniBatchSize=48)
opt = rlTD3AgentOptions with properties: SampleTime: 1 DiscountFactor: 0.9900 ExplorationModel: [1×1 rl.option.GaussianActionNoise] ExperienceBufferLength: 10000 MiniBatchSize: 48 SequenceLength: 1 ActorOptimizerOptions: [1×1 rl.option.rlOptimizerOptions] CriticOptimizerOptions: [1×2 rl.option.rlOptimizerOptions] NumStepsToLookAhead: 1 NumWarmStartSteps: 48 NumEpoch: 1 MaxMiniBatchPerEpoch: 100 LearningFrequency: -1 PolicyUpdateFrequency: 2 TargetPolicySmoothModel: [1×1 rl.option.GaussianActionNoise] TargetSmoothFactor: 0.0050 TargetUpdateFrequency: 2 BatchDataRegularizerOptions: [] ResetExperienceBufferBeforeTraining: 0 InfoToSave: [1×1 struct]
You can modify options using dot notation. For example, set the agent sample time to 0.5
.
opt.SampleTime = 0.5;
Algorithms
A GaussianActionNoise
object has the following numeric value
properties.
Property | Description | Default Value
(ExplorationModel ) | Default Value
(TargetPolicySmoothModel ) |
---|---|---|---|
Mean | Noise mean value | 0 | 0 |
StandardDeviationDecayRate | Decay rate of the standard deviation | 0 | 0 |
StandardDeviation | Initial value of noise standard deviation | sqrt(0.1) | sqrt(0.2) |
StandardDeviationMin | Minimum standard deviation, which must be less than
StandardDeviation | 0.1 | 0.1 |
LowerLimit | Noise sample lower limit | -Inf | -0.5 |
UpperLimit | Noise sample upper limit | Inf | 0.5 |
At each time step k
, the Gaussian noise v
is
sampled as shown in the following code.
w = Mean + randn(ActionSize).*StandardDeviation(k); v(k+1) = min(max(w,LowerLimit),UpperLimit);
Where the initial value v(1) is defined by the InitialAction
parameter. At each sample time step, the standard deviation decays as shown in the
following code.
decayedStandardDeviation = StandardDeviation(k).*(1 - StandardDeviationDecayRate); StandardDeviation(k+1) = max(decayedStandardDeviation,StandardDeviationMin);
Note that StandardDeviation(k)
is conserved between the end of an
episode and the start of the next one. Therefore, it keeps on uniformly decreasing
over multiple episodes until it reaches
StandardDeviationMin
.
Note
The StandardDeviation
property of a
GaussianActionNoise
object represents the
initial standard deviation
StandardDeviation(1)
at the beginning of the first
episode.
An OrnsteinUhlenbeckActionNoise
object has the following numeric value
properties.
Property | Description | Default Value |
---|---|---|
InitialAction | Initial value of action | 0 |
Mean | Noise mean value | 0 |
MeanAttractionConstant | Constant specifying how quickly the noise model output is attracted to the mean | 0.15 |
StandardDeviationDecayRate | Decay rate of the standard deviation | 0 |
StandardDeviation | Initial value of noise standard deviation | 0.3 |
StandardDeviationMin | Minimum standard deviation | 0 |
At each sample time step k
, the noise value v(k)
is
updated using the following formula, where Ts
is the agent sample
time, and the initial value v(1) is defined by the InitialAction
parameter.
v(k+1) = v(k) + MeanAttractionConstant.*(Mean - v(k)).*Ts + StandardDeviation(k).*randn(size(Mean)).*sqrt(Ts)
At each sample time step, the standard deviation decays as shown in the following code.
decayedStandardDeviation = StandardDeviation(k).*(1 - StandardDeviationDecayRate); StandardDeviation(k+1) = max(decayedStandardDeviation,StandardDeviationMin);
You can calculate how many samples it will take for the standard deviation to be halved using this simple formula.
halflife = log(0.5)/log(1-StandardDeviationDecayRate);
Note that StandardDeviation(k)
is conserved between the end of an
episode and the start of the next one. Therefore, it keeps on uniformly decreasing
over multiple episodes until it reaches
StandardDeviationMin
.
For continuous action signals, it is important to set the noise standard deviation
appropriately to encourage exploration. It is common to set
StandardDeviation*sqrt(Ts)
to a value between 1% and 10% of
your action range.
If your agent converges on local optima too quickly, promote agent exploration by increasing
the amount of noise; that is, by increasing the standard deviation. Also, to increase
exploration, you can reduce the StandardDeviationDecayRate
.
Note
The StandardDeviation
property of an
OrnsteinUhlenbeckActionNoise
object represents
the initial standard deviation
StandardDeviation(1)
at the beginning of the
first episode.
References
[1] Sutton, Richard S., and Andrew G. Barto. Reinforcement Learning: An Introduction. Second edition. Adaptive Computation and Machine Learning. Cambridge, Mass: The MIT Press, 2018.
Version History
Introduced in R2020aThe PolicyUpdateFrequency
property has been redefined. Previously,
it was defined as the number of steps between policy updates. Now, it is defined as the
period of the policy (actor) update with respect to the critic update. For example, while a
PolicyUpdateFrequency
of 3
previously meant that
the actor was updated every three steps, it now means that is updated every three critic
updates.
The default value of the ResetExperienceBufferBeforeTraining
has
changed from true
to false
.
When creating a new TD3 agent, if you want to clear the experience buffer before
training, you must specify ResetExperienceBufferBeforeTraining
as
true
. For example, before training, set the property using dot
notation.
agent.AgentOptions.ResetExperienceBufferBeforeTraining = true;
Alternatively, you can set the property to true
in an
rlTD3AgentOptions
object and use this object to create the TD3
agent.
The properties defining the probability distribution of the Gaussian action noise model have changed. This noise model is used by TD3 agents for exploration and target policy smoothing.
The
Variance
property has been replaced by theStandardDeviation
property.The
VarianceDecayRate
property has been replaced by theStandardDeviationDecayRate
property.The
VarianceMin
property has been replaced by theStandardDeviationMin
property.
When a GaussianActionNoise
noise object saved from a previous
MATLAB release is loaded, the value of VarianceDecayRate
is
copied to StandardDeviationDecayRate
, while the square root of the
values of Variance
and VarianceMin
are copied to
StandardDeviation
and StandardDeviationMin
,
respectively.
The Variance
, VarianceDecayRate
, and
VarianceMin
properties still work, but they are not recommended. To
define the probability distribution of the Gaussian action noise model, use the new property
names instead.
This table shows how to update your code to use the new property names for
rlTD3AgentOptions
object td3opt
.
Not Recommended | Recommended |
---|---|
td3opt.ExplorationModel.Variance = 0.5; | td3opt.ExplorationModel.StandardDeviation =
sqrt(0.5); |
td3opt.ExplorationModel.VarianceDecayRate = 0.1; | td3opt.ExplorationModel.StandardDeviationDecayRate =
0.1; |
td3opt.ExplorationModel.VarianceMin = 0.1; | td3opt.ExplorationModel.StandardDeviationMin =
sqrt(0.1); |
The properties defining the probability distribution of the Ornstein-Uhlenbeck (OU) noise model have been renamed. TD3 agents use OU noise for exploration.
The
Variance
property has been renamedStandardDeviation
.The
VarianceDecayRate
property has been renamedStandardDeviationDecayRate
.The
VarianceMin
property has been renamedStandardDeviationMin
.
The default values of these properties remain the same. When an
OrnsteinUhlenbeckActionNoise
noise object saved from a previous
MATLAB release is loaded, the values of Variance
,
VarianceDecayRate
, and VarianceMin
are copied in
the StandardDeviation
, StandardDeviationDecayRate
,
and StandardDeviationMin
, respectively.
The Variance
, VarianceDecayRate
, and
VarianceMin
properties still work, but they are not recommended. To
define the probability distribution of the OU noise model, use the new property names
instead.
This table shows how to update your code to use the new property names for
rlTD3AgentOptions
object td3opt
.
Not Recommended | Recommended |
---|---|
td3opt.ExplorationModel.Variance = 0.5; | td3opt.ExplorationModel.StandardDeviation =
sqrt(0.5); |
td3opt.ExplorationModel.VarianceDecayRate = 0.1; | td3opt.ExplorationModel.StandardDeviationDecayRate =
0.1; |
td3opt.ExplorationModel.VarianceMin = 0.1; | td3opt.ExplorationModel.StandardDeviationMin =
sqrt(0.1); |
td3opt.TargetPolicySmoothModel.Variance = 0.5; | td3opt.TargetPolicySmoothModel.StandardDeviation =
sqrt(0.5); |
td3opt.TargetPolicySmoothModel.VarianceDecayRate =
0.1; | td3opt.TargetPolicySmoothModel.StandardDeviationDecayRate =
0.1; |
td3opt.TargetPolicySmoothModel.VarianceMin = 0.1; | td3opt.TargetPolicySmoothModel.StandardDeviationMin =
sqrt(0.1); |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)