main content

deterministic actor with a continuous action space for reinforcement learning agents -凯发k8网页登录

deterministic actor with a continuous action space for reinforcement learning agents

since r2022a

description

this object implements a function approximator to be used as a deterministic actor within a reinforcement learning agent with a continuous action space. a continuous deterministic actor takes an environment observation as input and returns as output an action that is a parametrized deterministic function of the observation, thereby implementing a parametrized deterministic policy. after you create an rlcontinuousdeterministicactor object, use it to create a suitable agent, such as . for more information on creating representations, see create policies and value functions.

creation

description

example

actor = rlcontinuousdeterministicactor(net,observationinfo,actioninfo) creates a continuous deterministic actor object using the deep neural network net as underlying approximation model. for this actor, actioninfo must specify a continuous action space. the network input layers are automatically associated with the environment observation channels according to the dimension specifications in observationinfo. the network must have a single output layer with the same data type and dimensions as the action specified in actioninfo. this function sets the observationinfo and actioninfo properties of actor to the observationinfo and actioninfo input arguments, respectively.

example

actor = rlcontinuousdeterministicactor(net,observationinfo,actioninfo,observationinputnames=netobsnames) specifies the names of the network input layers to be associated with the environment observation channels. the function assigns, in sequential order, each environment observation channel specified in observationinfo to the layer specified by the corresponding name in the string array netobsnames. therefore, the network input layers, ordered as the names in netobsnames, must have the same data type and dimensions as the observation channels, as ordered in observationinfo.

example

actor = rlcontinuousdeterministicactor({basisfcn,w0},observationinfo,actioninfo) creates a continuous deterministic actor object using a custom basis function as underlying approximation model. the first input argument is a two-element cell array whose first element is the handle basisfcn to a custom basis function and whose second element is the initial weight vector w0. this function sets the observationinfo and actioninfo properties of actor to the observationinfo and actioninfo input arguments, respectively.

actor = rlcontinuousdeterministicactor(___,usedevice=usedevice) specifies the device used to perform computational operations on the actor object, and sets the usedevice property of actor to the usedevice input argument. you can use this syntax with any of the previous input-argument combinations.

input arguments

deep neural network used as the underlying approximation model within the actor, specified as one of the following:

  • array of layer objects

  • object

  • object

  • object

  • object

note

among the different network representation options, is preferred, since it has built-in validation checks and supports automatic differentiation. if you pass another network object as an input argument, it is internally converted to a dlnetwork object. however, best practice is to convert other representations to dlnetwork explicitly before using it to create a critic or an actor for a reinforcement learning agent. you can do so using dlnet=dlnetwork(net), where net is any deep learning toolbox™ neural network object. the resulting dlnet is the dlnetwork object that you use for your critic or actor. this practice allows a greater level of insight and control for cases in which the conversion is not straightforward and might require additional specifications.

the network must have as many input layers as the number of environment observation channels (with each input layer receiving input from an observation channel), and a single output layer returning the action.

rlcontinuousdeterministicactor objects support recurrent deep neural networks. for an example, see create deterministic actor from recurrent neural network.

the learnable parameters of the actor are the weights of the deep neural network. for a list of deep neural network layers, see . for more information on creating deep neural networks for reinforcement learning, see create policies and value functions.

network input layers names corresponding to the environment observation channels. when you use the pair value arguments 'observationinputnames' with netobsnames, the function assigns, in sequential order, each environment observation channel specified in observationinfo to each network input layer specified by the corresponding name in the string array netobsnames. therefore, the network input layers, ordered as the names in netobsnames, must have the same data type and dimensions as the observation channels, as ordered in observationinfo.

example: {"netinput1_airspeed","netinput2_altitude"}

custom basis function, specified as a function handle to a user-defined matlab function. the user defined function can either be an anonymous function or a function on the matlab path. the action to be taken based on the current observation, which is the output of the actor, is the vector a = w'*b, where w is a weight matrix containing the learnable parameters and b is the column vector returned by the custom basis function.

your basis function must have the following signature.

b = mybasisfunction(obs1,obs2,...,obsn)

here, obs1 to obsn are inputs in the same order and with the same data type and dimensions as the environment observation channels defined in observationinfo.

example: @(obs1,obs2,obs3) [obs3(2)*obs1(1)^2; abs(obs2(5) obs3(1))]

initial value of the basis function weights w, specified as a matrix having as many rows as the length of the vector returned by the basis function and as many columns as the dimension of the action space.

properties

observation specifications, specified as an rlfinitesetspec or rlnumericspec object or an array containing a mix of such objects. each element in the array defines the properties of an environment observation channel, such as its dimensions, data type, and name.

rlcontinuousdeterministicactor sets the observationinfo property of actor to the input observationinfo.

you can extract observationinfo from an existing environment or agent using getobservationinfo. you can also construct the specifications manually.

action specifications, specified as an rlnumericspec object. this object defines the properties of the environment action channel, such as its dimensions, data type, and name.

note

only one action channel is allowed.

rlcontinuousdeterministicactor sets the actioninfo property of actor to the input observationinfo.

you can extract actioninfo from an existing environment or agent using getactioninfo. you can also construct the specification manually.

for custom basis function representations, the action signal must be a scalar, a column vector, or a discrete action.

computation device used to perform operations such as gradient computation, parameter update and prediction during training and simulation, specified as either "cpu" or "gpu".

the "gpu" option requires both parallel computing toolbox™ software and a cuda® enabled nvidia® gpu. for more information on supported gpus see gpu computing requirements (parallel computing toolbox).

you can use gpudevice (parallel computing toolbox) to query or select a local gpu device to be used with matlab®.

note

training or simulating an agent on a gpu involves device-specific numerical round-off errors. these errors can produce different results compared to performing the same operations using a cpu.

to speed up training by using parallel processing over multiple cores, you do not need to use this argument. instead, when training your agent, use an rltrainingoptions object in which the useparallel option is set to true. for more information about training using multicore processors and gpus for training, see train agents using parallel computing and gpus.

example: "gpu"

object functions

deep deterministic policy gradient (ddpg) reinforcement learning agent
rltd3agenttwin-delayed deep deterministic (td3) policy gradient reinforcement learning agent
obtain action from agent, actor, or policy object given environment observations
evaluateevaluate function approximator object given observation (or observation-action) input data
evaluate gradient of function approximator object given observation and action input data
accelerateoption to accelerate computation of gradient for approximator object based on neural network
obtain learnable parameter values from agent, function approximator, or policy object
set learnable parameter values of agent, function approximator, or policy object
set approximation model in function approximator object
get approximation model from function approximator object

examples

create an observation specification object (or alternatively use getobservationinfo to extract the specification object from an environment). for this example, define the observation space as a continuous four-dimensional space, so that there is a single observation channel that carries a column vector containing four doubles.

obsinfo = rlnumericspec([4 1]);

create an action specification object (or alternatively use getactioninfo to extract the specification object from an environment). for this example, define the action space as a continuous two-dimensional space, so that the action channel carries a column vector containing two doubles.

actinfo = rlnumericspec([2 1]);

a continuous deterministic actor implements a parametrized deterministic policy for a continuous action space. this actor takes the current observation as input and returns as output an action that is a deterministic function of the observation.

to model the parametrized policy within the actor, use a neural network with one input layer (which receives the content of the environment observation channel, as specified by obsinfo) and one output layer (which returns the action to the environment action channel, as specified by actinfo).

define the network as an array of layer objects, and get the dimension of the observation and action spaces from the environment specification objects.

net = [ 
    featureinputlayer(obsinfo.dimension(1))
    fullyconnectedlayer(32)
    relulayer
    fullyconnectedlayer(actinfo.dimension(1))
    ];

convert the network to a dlnetwork object and display the number of learnable parameters.

net = dlnetwork(net);
summary(net)
   initialized: true
   number of learnables: 226
   inputs:
      1   'input'   4 features

create the actor object with rlcontinuousdeterministicactor, using the network and the observation and action specification objects as input arguments. the network input layer is automatically associated with the environment observation channel according to the dimension specifications in obsinfo.

actor = rlcontinuousdeterministicactor( ...
    net, ...
    obsinfo, ...
    actinfo)
actor = 
  rlcontinuousdeterministicactor with properties:
    observationinfo: [1x1 rl.util.rlnumericspec]
         actioninfo: [1x1 rl.util.rlnumericspec]
          usedevice: "cpu"

to check your actor, use getaction to return the action from a random observation, using the current network weights.

act = getaction(actor, ...
    {rand(obsinfo.dimension)}); 
act{1}
ans = 2x1 single column vector
   -0.0684
   -0.2538

you can now use the actor (along with a critic) to create an agent for the environment described by the given specification objects. examples of agents that can work with continuous action and observation spaces, and use a continuous deterministic actor, are and rltd3agent.

for more information on creating approximator objects such as actors and critics, see create policies and value functions.

create an observation specification object (or alternatively use getobservationinfo to extract the specification object from an environment). for this example, define the observation space as a continuous four-dimensional space, so that there is a single observation channel that carries a column vector containing four doubles.

obsinfo = rlnumericspec([4 1]);

create an action specification object (or alternatively use getactioninfo to extract the specification object from an environment). for this example, define the action space as a continuous two-dimensional space, so that the action channel carries a column vector containing two doubles.

actinfo = rlnumericspec([2 1]);

a continuous deterministic actor implements a parametrized deterministic policy for a continuous action space. this actor takes the current observation as input and returns an action as output.

to model the parametrized policy within the actor, use a neural network with one input layer (which receives the content of the environment observation channel, as specified by obsinfo) and one output layer (which returns the action to the environment action channel, as specified by actinfo).

define the network as an array of layer objects, and get the dimension of the observation and action spaces from the environment specification objects. name the network input layer netobsin so you can later explicitly associate it to the observation input channel.

net = [
    featureinputlayer(obsinfo.dimension(1),name="netobsin")
    fullyconnectedlayer(16)
    relulayer
    fullyconnectedlayer(actinfo.dimension(1))];

convert the network to a dlnetwork object, and display the number of learnable parameters.

net = dlnetwork(net);
summary(net)
   initialized: true
   number of learnables: 114
   inputs:
      1   'netobsin'   4 features

create the actor object with rlcontinuousdeterministicactor, using the network, the observation and action specification objects, and the name of the network input layer to be associated with the environment observation channel.

actor = rlcontinuousdeterministicactor(net, ...
            obsinfo,actinfo, ...
            observation="netobsin")
actor = 
  rlcontinuousdeterministicactor with properties:
    observationinfo: [1x1 rl.util.rlnumericspec]
         actioninfo: [1x1 rl.util.rlnumericspec]
          usedevice: "cpu"

to check your actor, use getaction to return the action from a random observation, using the current network weights.

act = getaction(actor,{rand(obsinfo.dimension)}); 
act{1}
ans = 2x1 single column vector
    0.4013
    0.0578

you can now use the actor (along with a critic) to create an agent for the environment described by the given specification objects. examples of agents that can work with continuous action and observation spaces, and use a continuous deterministic actor, are and rltd3agent.

for more information on creating approximator objects such as actors and critics, see create policies and value functions.

create an observation specification object (or alternatively use getobservationinfo to extract the specification object from an environment). for this example, define the observation space as consisting of two environment channels, the first containing a two-by-two continuous matrix and the second containing a scalar that can be only 0 or 1.

obsinfo = [rlnumericspec([2 2]) 
           rlfinitesetspec([0 1])];

create a continuous action space specification object (or alternatively use getactioninfo to extract the specification object from an environment). for this example, define the action space as a continuous three-dimensional space, so that the environment action channel carries a column vector containing three doubles.

actinfo = rlnumericspec([3 1]);

a continuous deterministic actor implements a parametrized deterministic policy for a continuous action space. this actor takes the current observation as input and returns as output an action that is a deterministic function of the observation.

to model the parametrized policy within the actor, use a custom basis function with two input arguments (one for each observation channel).

mybasisfcn = @(obsa,obsb) [obsa(1,1) obsb(1)^2;
                           obsa(2,1)-obsb(1)^2;
                           obsa(1,2)^2 obsb(1);
                           obsa(2,2)^2-obsb(1)];

the output of the actor is the vector w'*mybasisfcn(obsa,obsb), which is the action taken as a result of the given observation. the weight matrix w contains the learnable parameters and must have as many rows as the length of the basis function output and as many columns as the dimension of the action space.

define an initial parameter matrix.

w0 = rand(4,3);

create the actor. the first argument is a two-element cell containing both the handle to the custom function and the initial weight matrix. the second and third arguments are, respectively, the observation and action specification objects.

actor = rlcontinuousdeterministicactor({mybasisfcn,w0},obsinfo,actinfo)
actor = 
  rlcontinuousdeterministicactor with properties:
    observationinfo: [2x1 rl.util.rldataspec]
         actioninfo: [1x1 rl.util.rlnumericspec]
          usedevice: "cpu"

to check your actor, use the getaction function to return the action from a given observation, using the current parameter matrix.

a = getaction(actor,{rand(2,2),0})
a = 1x1 cell array
    {3x1 double}
a{1}
ans = 3×1
    1.3192
    0.8420
    1.5053

note that the actor does not enforce the set constraint for the discrete set elements.

a = getaction(actor,{rand(2,2),-1});
a{1}
ans = 3×1
    2.7890
    1.8375
    3.0855

you can now use the actor (along with a critic) to create an agent for the environment described by the given specification objects. examples of agents that can work with a mixed observation space, a continuous action space, and use a continuous deterministic actor, are and rltd3agent.

for more information on creating approximator objects such as actors and critics, see create policies and value functions.

create observation and action information. you can also obtain these specifications from an environment. for this example, define the observation space as a continuous four-dimensional space, so that a single observation channel carries a column vector containing four doubles, and the action space as a continuous two-dimensional space, so that the action channel carries a column vector containing two doubles.

obsinfo = rlnumericspec([4 1]);
actinfo = rlnumericspec([2 1]);

a continuous deterministic actor implements a parametrized deterministic policy for a continuous action space. this actor takes the current observation as input and returns as output an action that is a deterministic function of the observation.

to model the parametrized policy within the actor, use a neural network with one input layer (which receives the content of the environment observation channel, as specified by obsinfo) and one output layer (which returns the action to the environment action channel, as specified by actinfo).

define the network as an array of layer objects, and get the dimension of the observation and action spaces from the environment specification objects. since this network is recurrent, use a sequenceinputlayer as the input layer and include at least one lstmlayer.

net = [
    sequenceinputlayer(obsinfo.dimension(1))
    fullyconnectedlayer(10)
    relulayer
    lstmlayer(8,outputmode="sequence")
    fullyconnectedlayer(20)
    fullyconnectedlayer(actinfo.dimension(1))
    tanhlayer
    ];

convert the network to a dlnetwork object and display the number of learnable parameters.

net = dlnetwork(net);
summary(net)
   initialized: true
   number of learnables: 880
   inputs:
      1   'sequenceinput'   sequence input with 4 dimensions

create a deterministic actor representation for the network.

actor = rlcontinuousdeterministicactor( ...
    net, ...
    obsinfo, ...
    actinfo);

to check your actor, use getaction to return the action from a random observation, given the current network weights.

a = getaction(actor, ...
    {rand(obsinfo.dimension)}); 
a{1}
ans = 2x1 single column vector
   -0.0742
    0.0158

you can use getstate and setstate to extract and set the current state of the recurrent neural network in the actor.

getstate(actor)
ans=2×1 cell array
    {8x1 single}
    {8x1 single}
actor = setstate(actor, ...
    {-0.01*single(rand(8,1)), ...
      0.01*single(rand(8,1))});

to evaluate the actor using sequential observations, use the sequence length (time) dimension. for example, obtain actions for 5 independent sequences each one consisting of 9 sequential observations.

[action,state] = getaction(actor, ...
    {rand([obsinfo.dimension 5 9])});

display the action corresponding to the seventh element of the observation sequence in the fourth sequence.

action = action{1};
action(1,1,4,7)
ans = single
    -0.2426

display the updated state of the recurrent neural network.

state
state=2×1 cell array
    {8x5 single}
    {8x5 single}

you can now use the actor (along with a critic) to create an agent for the environment described by the given specification objects. examples of agents that can work with continuous action and observation spaces, and use a continuous deterministic actor, are and rltd3agent.

for more information on input and output format for recurrent neural networks, see the algorithms section of . for more information on creating approximator objects such as actors and critics, see create policies and value functions.

version history

introduced in r2022a

网站地图