main content

q-凯发k8网页登录

q-value function approximator object for reinforcement learning agents

since r2022a

description

this object implements a q-value function approximator that you can use as a critic for a reinforcement learning agent. a q-value function (also known as action-value function) is a mapping from an environment observation-action pair to the value of a policy. specifically, its output is a scalar that represents the expected discounted cumulative long-term reward when an agent starts from the state corresponding to the given observation, executes the given action, and keeps on taking actions according to the given policy afterwards. a q-value function critic therefore needs both the environment state and an action as inputs. after you create an rlqvaluefunction critic, use it to create an agent such as , , , , or rltd3agent. for more information on creating representations, see create policies and value functions.

creation

description

example

critic = rlqvaluefunction(net,observationinfo,actioninfo) creates the q-value function object critic. here, net is the deep neural network used as an approximation model, and it must have both observation and action as inputs and a single scalar output. the network input layers are automatically associated with the environment observation and action channels according to the dimension specifications in observationinfo and actioninfo. this function sets the observationinfo and actioninfo properties of critic to the observationinfo and actioninfo input arguments, respectively.

example

critic = rlqvaluefunction(tab,observationinfo,actioninfo) creates the q-value function object critic with discrete action and observation spaces from the q-value table tab. tab is a object containing a table with as many rows as the number of possible observations and as many columns as the number of possible actions. the function sets the and properties of critic respectively to the observationinfo and actioninfo input arguments, which in this case must be scalar rlfinitesetspec objects.

example

critic = rlqvaluefunction({basisfcn,w0},observationinfo,actioninfo) creates a q-value function object critic using a custom basis function as underlying approximator. 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. here the basis function must have both observation and action as inputs and w0 must be a column vector. the function sets the observationinfo and actioninfo properties of critic to the observationinfo and actioninfo input arguments, respectively.

example

critic = rlqvaluefunction(___,name=value) specifies one or more name-value arguments. you can specify the input and output layer names (to mandate their association with the environment observation and action channels) for deep neural network approximators. for all types of approximators, you can specify the computation device, for example usedevice="gpu".

input arguments

deep neural network used as the underlying approximator within the critic, specified as one of the following:

  • array of layer objects

  • object

  • object

  • object

  • object

the network must have as many input layers as the number of environment observation channels plus one. specifically, there must be one input layer for each observation channel, and one additional input layer for the action channel. the network must have a single output layer returning a scalar value

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.

rlqvaluefunction objects support recurrent deep neural networks.

the learnable parameters of the critic 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.

q-value table, specified as an object containing an array with as many rows as the possible observations and as many columns as the possible actions. the element (s,a) is the expected cumulative long-term reward for taking action a from observed state s. the elements of this array are the learnable parameters of the critic.

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 output of the critic is the scalar c = w'*b, where w is a weight vector 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,act)

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 and act is an input with the same data type and dimension as the environment action channel defined in actioninfo.

for an example on how to use a basis function to create a q-value function critic with a mixed continuous and discrete observation space, see create mixed observation space q-value function critic from custom basis function.

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

initial value of the basis function weights w, specified as a column vector having the same length as the vector returned by the basis function.

name-value arguments

specify optional pairs of arguments as name1=value1,...,namen=valuen, where name is the argument name and value is the corresponding value. name-value arguments must appear after other arguments, but the order of the pairs does not matter.

example: usedevice="gpu"

network input layers name corresponding to the environment action channel, specified as a string array or a cell array of character vectors. the function assigns the environment action channel specified in actioninfo to the specified network input layer. therefore, the specified network input layer must have the same data type and dimensions of the action channel, as defined in actioninfo.

note

the function does not use the name or the description (if any) of the action channel specified in actioninfo.

this name-value argument is supported only when the approximation model is a deep neural network.

example: actioninputnames="mynetoutput_force"

network input layers names corresponding to the environment observation channels, specified as a string array or a cell array of character vectors. the function assigns, in sequential order, each environment observation channel specified in observationinfo to each specified network input layer. therefore, the specified network input layers, ordered as indicated in this argument, must have the same data type and dimensions as the observation channels, as ordered in observationinfo.

this name-value argument is supported only when the approximation model is a deep neural network.

example: observationinputnames={"netinput1_airspeed","netinput2_altitude"}

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.

rlqvaluefucntion sets the observationinfo property of critic to the input argument observationinfo.

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

action specifications, specified either as an rlfinitesetspec (for discrete action spaces) or rlnumericspec (for continuous action spaces) 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.

rlqvaluerepresentation sets the actioninfo property of critic to the input actioninfo.

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

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
deep q-network (dqn) reinforcement learning agent
q-learning reinforcement learning agent
sarsa reinforcement learning agent
soft actor-critic (sac) reinforcement learning agent
obtain estimated value from a critic given environment observations and actions
obtain maximum estimated value over all possible actions from a q-value function critic with discrete action space, 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 q-value function critic takes the current observation and an action as inputs and returns a single scalar as output (the estimated discounted cumulative long-term reward for taking the action from the state corresponding to the current observation, and following the policy thereafter).

to model the parametrized q-value function within the critic, use a neural network with two input layers (one receiving the content of the observation channel, as specified by obsinfo, and the other receiving the content of the action channel, as specified by actinfo) and one output layer (returning the scalar value).

define each network path as an array of layer objects. assign names to the input and output layers of each path, so you can properly connect the paths. get the dimensions of the observation and action spaces from the environment specification (regardless of whether the observation space is a column vector, row vector, or matrix, prod(obsinfo.dimension) is its total number of dimensions, for this example four, similarly, prod(actinfo.dimension) is the number of dimension of the action space, for this example two).

% observation path layers
obspath = [featureinputlayer(prod(obsinfo.dimension))
           fullyconnectedlayer(5)
           relulayer
           fullyconnectedlayer(5,name="obsout")];
% action path layers
actpath = [featureinputlayer(prod(actinfo.dimension))
           fullyconnectedlayer(5)
           relulayer
           fullyconnectedlayer(5,name="actout")];
% common path to output layers
% concatenate two layers along dimension one
compath = [concatenationlayer(1,2,name="cct")
           fullyconnectedlayer(5)
           relulayer    
           fullyconnectedlayer(1, name="output")];
% add layers to network object
net = addlayers(layergraph(obspath),actpath); 
net = addlayers(net,compath);
% connect layers
net = connectlayers(net,"obsout","cct/in1");
net = connectlayers(net,"actout","cct/in2");
% plot network
plot(net)

figure contains an axes object. the axes object contains an object of type graphplot.

convert to dlnetwork and display the number of weights.

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

create the critic with rlqvaluefunction, using the network as well as the observations and action specification objects. when using this syntax, the network input layers are automatically associated with the components of the observation and action signals according to the dimension specifications in obsinfo and actinfo.

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

to check your critic, use the getvalue function to return the value of a random observation and action, given the current network weights.

v = getvalue(critic, ...
    {rand(obsinfo.dimension)}, ...
    {rand(actinfo.dimension)})
v = single
    -1.1006

you can now use the critic (along with an actor) 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 q-value function critic, are , rltd3agent, and .

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 q-value function critic takes the current observation and an action as inputs and returns a single scalar as output (the estimated discounted cumulative long-term reward for taking the action from the state corresponding to the current observation, and following the policy thereafter).

to model the parametrized q-value function within the critic, use a neural network with two input layers (one receiving the content of the observation channel, as specified by obsinfo, and the other receiving the content of the action channel, as specified by actinfo) and one output layer (returning the scalar value).

define each network path as an array of layer objects. assign names to the input and output layers of each path. this assignment allows you to connect the paths and explicitly associate the network input and output layers with the appropriate environment channel.

get the dimensions of the observation and action spaces from the environment specification (regardless of whether the observation space is a column vector, row vector, or matrix, prod(obsinfo.dimension) is its total number of dimensions, for this example four, similarly, prod(actinfo.dimension) is the number of dimension of the action space, for this example two).

% observation path layers
obspath = [featureinputlayer( ...
                prod(obsinfo.dimension), ...
                name="netobsinput") 
           fullyconnectedlayer(16)
           relulayer
           fullyconnectedlayer(5,name="obsout")];
% action path layers
actpath = [featureinputlayer( ...
                prod(actinfo.dimension), ...
                name="netactinput") 
           fullyconnectedlayer(16)
           relulayer
           fullyconnectedlayer(5,name="actout")];
% common path to output layers 
% concatenate two layers along dimension one
compath = [concatenationlayer(1,2,name="cct")
           fullyconnectedlayer(8)
           relulayer
           fullyconnectedlayer(1, name="output")];
% add layers to network object
net = addlayers(layergraph(obspath),actpath); 
net = addlayers(net,compath);
% connect layers
net = connectlayers(net,"obsout","cct/in1");
net = connectlayers(net,"actout","cct/in2");
% plot network
plot(net)

figure contains an axes object. the axes object contains an object of type graphplot.

convert to dlnetwork and display the number of weights.

net = dlnetwork(net);
summary(net)
   initialized: true
   number of learnables: 395
   inputs:
      1   'netobsinput'   4 features
      2   'netactinput'   2 features

create the critic with rlqvaluefunction, using the network, the observations and action specification objects, and the names of the network input layers to be associated with the observation and action from the environment.

critic = rlqvaluefunction(net,...
             obsinfo,actinfo, ...
             observationinputnames="netobsinput",...
             actioninputnames="netactinput")
critic = 
  rlqvaluefunction with properties:
    observationinfo: [1x1 rl.util.rlnumericspec]
         actioninfo: [1x1 rl.util.rlnumericspec]
          usedevice: "cpu"

to check your critic, use the getvalue function to return the value of a random observation and action, given the current network weights.

v = getvalue(critic, ...
    {rand(obsinfo.dimension)}, ...
    {rand(actinfo.dimension)})
v = single
    0.2703

you can now use the critic (along with an actor) 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 q-value function critic, are , rltd3agent, and .

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

create a finite set observation specification object (or alternatively use getobservationinfo to extract the specification object from an environment with a discrete observation space). for this example define the observation space as a finite set with of four possible values.

obsinfo = rlfinitesetspec([7 5 3 1]);

create a finite set action specification object (or alternatively use getactioninfo to extract the specification object from an environment with a discrete action space). for this example define the action space as a finite set with two possible values.

actinfo = rlfinitesetspec([4 8]);

a q-value function critic takes the current observation and an action as inputs and returns a single scalar as output (the estimated discounted cumulative long-term reward for taking the action from the state corresponding to the current observation, and following the policy thereafter).

since both observation and action spaces are discrete and low-dimensional, use a table to model the q-value function within the critic. rltable creates a value table object from the observation and action specifications objects.

qtable = rltable(obsinfo,actinfo);

the table stores a value (representing the expected cumulative long term reward) for each possible observation-action pair. each row corresponds to an observation and each column corresponds to an action. you can access the table using the table property of the vtable object. the initial value of each element is zero.

qtable.table
ans = 4×2
     0     0
     0     0
     0     0
     0     0

you can initialize the table to any value, in this case an array containing the integer from 1 through 8.

qtable.table=reshape(1:8,4,2)
qtable = 
  rltable with properties:
    table: [4x2 double]

create the critic using the table as well as the observations and action specification objects.

critic = rlqvaluefunction(qtable,obsinfo,actinfo)
critic = 
  rlqvaluefunction with properties:
    observationinfo: [1x1 rl.util.rlfinitesetspec]
         actioninfo: [1x1 rl.util.rlfinitesetspec]
          usedevice: "cpu"

to check your critic, use the getvalue function to return the value of a given observation and action, using the current table entries.

v = getvalue(critic,{5},{8})
v = 6

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

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 three-dimensional space, so that there is a single observation channel that carries a column vector containing three doubles.

obsinfo = rlnumericspec([3 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 q-value function critic takes the current observation and an action as inputs and returns a single scalar as output (the estimated discounted cumulative long-term reward for taking the action from the state corresponding to the current observation, and following the policy thereafter).

to model the parametrized q-value function within the critic, use a custom basis function with two inputs, the first receiving the content of the observation channel (as specified by obsinfo) and the other receiving the content of the action channel, (as specified by actinfo).

create a custom function that returns a vector of four elements, given an observation and an action as inputs.

mybasisfcn = @(myobs,myact) [ ...
    myobs(2)^2; ...
    myobs(1) exp(myact(1)); ...
    abs(myact(2)); ...
    myobs(3) ]
mybasisfcn = function_handle with value:
    @(myobs,myact)[myobs(2)^2;myobs(1) exp(myact(1));abs(myact(2));myobs(3)]

the output of the critic is the scalar w'*mybasisfcn(myobs,myact), which represents the estimated value of the observation-action pair, under the given policy. here w is a weight column vector which must have the same size of the custom function output. the elements of w are the learnable parameters.

define an initial parameter vector.

w0 = [1;4;4;2];

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

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

to check your critic, use getvalue to return the value of a given observation-action pair, using the current parameter vector.

v = getvalue(critic,{[1 2 3]'},{[4 5]'})
v = 252.3926

you can now use the critic (along with an actor) 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 q-value function critic, are , rltd3agent, and .

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 channels, the first carrying a vector over a continuous two-dimensional space and the second carrying a vector over a three-dimensional space that can assume only four values.

obsinfo = [rlnumericspec([1 2]) 
           rlfinitesetspec({[1 0 -1], ...
                            [-1 2 1], ...
                            [0.1 0.2 0.3], ...
                            [0 0 0]})];

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 discrete set consisting of three possible actions, labeled 1, 2, and 3.

actinfo = rlfinitesetspec({1,2,3});

a q-value function critic takes the current observation and an action as inputs and returns a single scalar as output (the estimated discounted cumulative long-term reward for taking the action from the state corresponding to the current observation, and following the policy thereafter).

to model the parametrized q-value function within the critic, use a custom basis function with three inputs, the first two receiving the content of the two observation channel (as specified by obsinfo) and the third receiving the content of the action channel, (as specified by actinfo).

create a custom function that returns a vector of four elements, given an observation and an action as inputs.

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

the output of the critic is the scalar w'*mybasisfcn(myobs,myact), which represents the estimated value of the observation-action pair, under the given policy. here w is a weight column vector which must have the same size of the custom function output. the elements of w are the learnable parameters.

define an initial parameter vector.

w0 = ones(4,1);

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

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

to check your critic, use the getvalue function to return the value of a given observation-action pair, using the current parameter vector.

v = getvalue(critic,{[-0.5 0.6],[1 0 -1]},{3})
v = -0.9000

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

v = getvalue(critic,{[-0.5 0.6],[10 -10 -0.05]},{33})
v = -21.0000

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

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

version history

introduced in r2022a

网站地图