rlSACAgentOptions
Description
Use an rlSACAgentOptions
object to specify options for soft
actor-critic (SAC) agents. To create a SAC agent, use rlSACAgent
.
For more information, see Soft Actor-Critic (SAC) 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 SAC agent using all default options. You can
modify the object properties using dot notation.opt
= rlSACAgentOptions
creates the options set opt
= rlSACAgentOptions(Name=Value
)opt
and sets its properties using one
or more name-value arguments. For example,
rlSACAgentOptions(DiscountFactor=0.95)
creates an option set with a
discount factor of 0.95
. You can specify multiple name-value
arguments.
Properties
SampleTime
— Sample time of agent
1
(default) | positive scalar | -1
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.
Within a Simulink® environment, the RL Agent block
that uses the agent object executes every SampleTime
seconds of
simulation time. If SampleTime
is -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.
Note
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.
Regardless of the type of environment, the time interval between consecutive elements
in the output experience returned by sim
or
train
is
always SampleTime
.
If SampleTime
is -1
, for 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, while for MATLAB environments, this time interval is considered equal to
1
.
This property is shared between the agent and the agent options object within the agent. Therefore, if you change it in the agent options object, it gets changed in the agent, and vice versa.
Example: SampleTime=-1
DiscountFactor
— Discount factor
0.99
(default) | positive scalar less than or equal to 1
Discount factor applied to future rewards during training, specified as a positive scalar less than or equal to 1.
Example: DiscountFactor=0.9
EntropyWeightOptions
— Entropy tuning options
EntropyWeightOptions
object | vector containing two EntropyWeightOptions
objects
Entropy tuning options, specified as:
One
EntropyWeightOptions
object, for discrete or continuous action spaces.A vector containing one
EntropyWeightOptions
object for the discrete action part and oneEntropyWeightOptions
object for the continuous action part, for hybrid action spaces.
An EntropyWeightOptions
object has the following
properties.
EntropyWeight
— Initial entropy component weight
1
(default) | positive scalar
Initial entropy component weight, specified as a positive scalar.
LearnRate
— Optimizer learning rate
3e-4
(default) | nonnegative scalar
Optimizer learning rate, specified as a nonnegative scalar. If
LearnRate
is zero, the EntropyWeight
value is fixed during training and the TargetEntropy
value is
ignored.
TargetEntropy
— Target entropy value
[]
(default) | scalar
Target entropy value for tuning entropy weight, specified as a scalar. A higher target entropy value encourages more exploration.
If you do not specify TargetEntropy
, the agent uses
–A as the target value, where A is the
number of actions.
Algorithm
— Algorithm to tune entropy
"adam"
(default) | "sgdm"
| "rmsprop"
Algorithm to tune entropy, specified as one of the following strings.
"adam"
— Use the Adam optimizer. You can specify the decay rates of the gradient and squared gradient moving averages using theGradientDecayFactor
andSquaredGradientDecayFactor
fields of theOptimizerParameters
option."sgdm"
— Use the stochastic gradient descent with momentum (SGDM) optimizer. You can specify the momentum value using theMomentum
field of theOptimizerParameters
option."rmsprop"
— Use the RMSProp optimizer. You can specify the decay rate of the squared gradient moving average using theSquaredGradientDecayFactor
fields of theOptimizerParameters
option.
For more information about these optimizers, see the Algorithms
section of trainingOptions
in Deep Learning Toolbox™.
GradientThreshold
— Threshold value for gradient
Inf
(default) | positive scalar
Threshold value for the entropy gradient, specified as Inf
or a positive scalar. If the gradient exceeds this value, the gradient is
clipped.
OptimizerParameters
— Applicable parameters for optimizer
OptimizerParameters
object
Applicable parameters for the optimizer, specified as an
OptimizerParameters
object with the following parameters.
The default parameter values work well for most problems.
Parameter | Description | Default |
---|---|---|
Momentum | Contribution of previous step, specified as a scalar from 0 to 1. A value of 0 means no contribution from the previous step. A value of 1 means maximal contribution. This parameter applies only
when | 0.9 |
Epsilon | Denominator offset, specified as a positive scalar. The optimizer adds this offset to the denominator in the network parameter updates to avoid division by zero. This parameter applies
only when | 1e-8 |
GradientDecayFactor | Decay rate of gradient moving average, specified as a positive scalar from 0 to 1. This parameter applies only when
| 0.9 |
SquaredGradientDecayFactor | Decay rate of squared gradient moving average, specified as a positive scalar from 0 to 1. This parameter applies only
when | 0.999 |
When a particular property of OptimizerParameters
is not
applicable to the optimizer type specified in the Algorithm
option, that property is set to "Not applicable"
.
To change the default values, access the properties of
OptimizerParameters
using dot notation.
opt = rlSACAgentOptions; opt.EntropyWeightOptions.OptimizerParameters.GradientDecayFactor = 0.95;
ExperienceBufferLength
— Experience buffer size
10000
(default) | positive integer
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
MiniBatchSize
— Size of random experience mini-batch
64
(default) | positive integer
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 actor and critics. Large mini-batches reduce the variance when computing gradients but increase the computational effort.
Example: MiniBatchSize=128
SequenceLength
— Maximum batch-training trajectory length when using RNN
1
(default) | positive integer
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
ActorOptimizerOptions
— Actor optimizer options
rlOptimizerOptions
object
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)
CriticOptimizerOptions
— Critic optimizer options
rlOptimizerOptions
object
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)
NumStepsToLookAhead
— Number of future rewards used to estimate the value of the policy
1
(default) | positive integer
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
NumWarmStartSteps
— Minimum number of samples to generate before learning starts
positive integer
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
NumEpoch
— Number of times agent learns over data set stored in the experience buffer
1
(default) | positive integer
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
MaxMiniBatchPerEpoch
— Maximum number of mini-batches used for learning during a single epoch
100
(default) | positive integer
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
LearningFrequency
— Minimum number of environment interactions between learning iterations
-1
(default) | positive integer
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 off-policy agents that support this property
(DQN, DDPG, TD3, and SAC), the default value of -1
means that
learning occurs after each episode is finished. Note that 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
PolicyUpdateFrequency
— Period of policy update with respect to critic update
1
(default) | positive integer
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=2
TargetUpdateFrequency
— Number of critic updates between periodic target critic updates
1
(default) | positive integer
Number of critic updates between periodic target critic updates, specified as a positive integer. For more information, see Target Update Methods.
Example: TargetUpdateFrequency=5
TargetSmoothFactor
— Smoothing factor for target critic updates
1e-3
(default) | positive scalar less than or equal to 1
Smoothing factor for target critic updates, specified as a positive scalar less than or equal to 1. For more information, see Target Update Methods.
Example: TargetSmoothFactor=1e-2
UseCriticTargetEntropy
— Option to use entropy in the critic targets
true
(default) | false
Option to use entropy in the critic targets, specified as either
true
(default, entropy is used) or false
(entropy is not used). Note that this option does not affect entropy usage in the
actor.
Example: UseCriticTargetEntropy=false
BatchDataRegularizerOptions
— Batch data regularizer options
[]
(default) | rlBehaviorCloningRegularizerOptions
object
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.
Note
SAC agents with noncontinuous action spaces do not support
BatchDataRegularizerOptions
.
For more information, see rlBehaviorCloningRegularizerOptions
.
Example: BatchDataRegularizerOptions =
rlBehaviorCloningRegularizerOptions(BehaviorCloningRegularizerWeight=10)
ResetExperienceBufferBeforeTraining
— Option for clearing the experience buffer
false
(default) | true
Option for clearing the experience buffer before training, specified as a logical value.
Example: ResetExperienceBufferBeforeTraining=true
InfoToSave
— Options to save additional agent data
structure (default)
Options to save additional agent data, specified as a structure containing the following fields.
Optimizer
PolicyState
Target
ExperienceBuffer
You can save an agent object in one of the following ways:
Using the
save
commandSpecifying
saveAgentCriteria
andsaveAgentValue
in anrlTrainingOptions
objectSpecifying 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 is saved with the agent. For example, if you set the
Optimizer
field to true
,
then the actor and critic optimizers are saved along with the agent.
You can modify the InfoToSave
property only after the
agent options object is created.
Example: options.InfoToSave.Optimizer=true
Optimizer
— Option to save actor and critic optimizers
false
(default) | 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
PolicyState
— Option to save state of explorative policy
false
(default) | 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
Target
— Option to save actor and critic targets
false
(default) | 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
ExperienceBuffer
— Option to save experience buffer
false
(default) | 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
rlSACAgent | Soft actor-critic (SAC) reinforcement learning agent |
Examples
Create SAC Agent Options Object
Create a SAC agent options object, specifying the discount factor.
opt = rlSACAgentOptions(DiscountFactor=0.95)
opt = rlSACAgentOptions with properties: SampleTime: 1 DiscountFactor: 0.9500 EntropyWeightOptions: [1x1 rl.option.EntropyWeightOptions] ExperienceBufferLength: 10000 MiniBatchSize: 64 SequenceLength: 1 ActorOptimizerOptions: [1x1 rl.option.rlOptimizerOptions] CriticOptimizerOptions: [1x2 rl.option.rlOptimizerOptions] NumStepsToLookAhead: 1 NumWarmStartSteps: 64 NumEpoch: 1 MaxMiniBatchPerEpoch: 100 LearningFrequency: -1 PolicyUpdateFrequency: 1 TargetUpdateFrequency: 1 TargetSmoothFactor: 1.0000e-03 UseCriticTargetEntropy: 1 BatchDataRegularizerOptions: [] ResetExperienceBufferBeforeTraining: 0 InfoToSave: [1x1 struct]
You can modify options using dot notation. For example, set the agent sample time to 0.5
.
opt.SampleTime = 0.5;
For SAC agents, configure the entropy weight optimizer using the options in EntropyWeightOptions
. For example, set the target entropy value to –5
.
opt.EntropyWeightOptions.TargetEntropy = -5;
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 R2020bR2024a: The CriticUpdateFrequency
and NumGradientStepsPerUpdate
properties are no longer effective
The CriticUpdateFrequency
and
NumGradientStepsPerUpdate
properties of the
rlSACAgentOptions
object are no longer effective. To specify the minimum
number of environment interactions after which the critic is updated, use the
LearningFrequency
property. To specify the number of passes over the
data in the replay buffer, use the NumEpoch
property.
R2024a: The PolicyUpdateFrequency
property has been redefined
The 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.
R2022a: Simulation and deployment: UseDeterministicExploitation
will be removed
The property UseDeterministicExploitation
of the
rlSACAgentOptions
object will be removed in a future release. Use the
UseExplorationPolicy
property of rlSACAgent
instead.
Previously, you set UseDeterministicExploitation
as follows.
Force the agent to always select the action with maximum likelihood, thereby using a greedy deterministic policy for simulation and deployment.
agent.AgentOptions.UseDeterministicExploitation = true;
Allow the agent to select its action by sampling its probability distribution for simulation and policy deployment, thereby using a stochastic policy that explores the observation space.
agent.AgentOptions.UseDeterministicExploitation = false;
Starting in R2022a, set UseExplorationPolicy
as follows.
Force the agent to always select the action with maximum likelihood, thereby using a greedy deterministic policy for simulation and deployment.
agent.UseExplorationPolicy = false;
Allow the agent to select its action by sampling its probability distribution for simulation and policy deployment, thereby using a stochastic policy that explores the observation space.
agent.UseExplorationPolicy = true;
Similarly to UseDeterministicExploitation
,
UseExplorationPolicy
affects only simulation and deployment; it does
not affect training.
R2022a: The default value of the ResetExperienceBufferBeforeTraining
property has changed
The default value of the ResetExperienceBufferBeforeTraining
has
changed from true
to false
.
When creating a new SAC 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
rlSACAgentOptions
object and use this object to create the SAC
agent.
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
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)