main content

predict next observation, next reward, or episode termination given observation and action input data -凯发k8网页登录

predict next observation, next reward, or episode termination given observation and action input data

since r2022a

description

example

prednextobs = predict(tsnfcnappx,obs,act) evaluates the environment transition function approximator object tsnfcnappx and returns the predicted next observation nextobs, given the current observation obs and the action act.

example

predreward = predict(rwdfcnappx,obs,act,nextobs) evaluates the environment reward function approximator object rwdfcnappx and returns the predicted reward predreward, given the current observation obs, the action act, and the next observation nextobs.

example

predisdone = predict(idnfcnappx,obs,act) evaluates the environment is-done function approximator object idnfcnappx and returns the predicted is-done status predisdone, given the current observation obs, the action act, and the next observation nextobs.

examples

create observation and action specification objects (or alternatively use getobservationinfo and getactioninfo to extract the specification objects from an environment). for this example, two observation channels carry vectors in a four- and two-dimensional space, respectively. the action is a continuous three-dimensional vector.

obsinfo = [rlnumericspec([4 1],upperlimit=10*ones(4,1));
           rlnumericspec([1 2],upperlimit=20*ones(1,2)) ];
actinfo = rlnumericspec([3 1]);

create a deep neural network to use as approximation model for the transition function approximator. for a continuous gaussian transition function approximator, the network must have two output layers for each observation (one for the mean values the other for the standard deviation values).

define each network path as an array of layer objects. get the dimensions of the observation and action spaces from the environment specification objects, and specify a name for the input layers, so you can later explicitly associate them with the appropriate environment channel.

% input path layers from first observation channel
inpath1 = [ featureinputlayer( ...
                prod(obsinfo(1).dimension), ...
                name="netobsin1")
            fullyconnectedlayer(5,name="infc1") ];
% input path layers from second observation channel
inpath2 = [ featureinputlayer( ...
                prod(obsinfo(2).dimension), ...
                name="netobsin2")
            fullyconnectedlayer(5,name="infc2") ];
% input path layers from action channel
inpath3 = [ featureinputlayer( ...
                prod(actinfo(1).dimension), ...
                name="netactin")
            fullyconnectedlayer(5,name="infc3") ];
% joint path layers, concatenate 3 inputs along first dimension
jointpath = [ concatenationlayer(1,3,name="concat")
              tanhlayer(name="tanhjnt");
              fullyconnectedlayer(10,name="jntfc") ];
% path layers for mean values of first predicted obs
% using scalinglayer to scale range from (-1,1) to (-10,10)
% note that scale vector must be a column vector
meanpath1 = [ tanhlayer(name="tanhmean1");
              fullyconnectedlayer(prod(obsinfo(1).dimension));
              scalinglayer(name="scale1", ...
                scale=obsinfo(1).upperlimit) ];
% path layers for standard deviations first predicted obs
% using softplus layer to make them non negative
sdevpath1 = [ tanhlayer(name="tanhstdv1");
              fullyconnectedlayer(prod(obsinfo(1).dimension));
              softpluslayer(name="splus1") ];
% path layers for mean values of second predicted obs
% using scalinglayer to scale range from (-1,1) to (-20,20)
% note that scale vector must be a column vector
meanpath2 = [ tanhlayer(name="tanhmean2");
              fullyconnectedlayer(prod(obsinfo(2).dimension));
              scalinglayer(name="scale2", ...
                scale=obsinfo(2).upperlimit(:)) ];
% path layers for standard deviations second predicted obs
% using softplus layer to make them non negative
sdevpath2 = [ tanhlayer(name="tanhstdv2");
              fullyconnectedlayer(prod(obsinfo(2).dimension));
              softpluslayer(name="splus2") ];
% add layers to network object
net = layergraph;
net = addlayers(net,inpath1);
net = addlayers(net,inpath2);
net = addlayers(net,inpath3);
net = addlayers(net,jointpath);
net = addlayers(net,meanpath1);
net = addlayers(net,sdevpath1);
net = addlayers(net,meanpath2);
net = addlayers(net,sdevpath2);
% connect layers
net = connectlayers(net,"infc1","concat/in1");
net = connectlayers(net,"infc2","concat/in2");
net = connectlayers(net,"infc3","concat/in3");
net = connectlayers(net,"jntfc","tanhmean1/in");
net = connectlayers(net,"jntfc","tanhstdv1/in");
net = connectlayers(net,"jntfc","tanhmean2/in");
net = connectlayers(net,"jntfc","tanhstdv2/in");
% plot network
plot(net)

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

% convert to dlnetwork
net=dlnetwork(net);
% display the number of weights
summary(net)
   initialized: true
   number of learnables: 352
   inputs:
      1   'netobsin1'   4 features
      2   'netobsin2'   2 features
      3   'netactin'    3 features

create a continuous gaussian transition function approximator object, specifying the names of all the input and output layers.

tsnfcnappx = rlcontinuousgaussiantransitionfunction(...
    net,obsinfo,actinfo,...
    observationinputnames=["netobsin1","netobsin2"], ...
    actioninputnames="netactin", ...
    nextobservationmeanoutputnames=["scale1","scale2"], ...
    nextobservationstandarddeviationoutputnames=["splus1","splus2"] );

predict the next observation for a random observation and action.

predobs = predict(tsnfcnappx, ...
    {rand(obsinfo(1).dimension),rand(obsinfo(2).dimension)}, ...
    {rand(actinfo(1).dimension)})
predobs=1×2 cell array
    {4x1 single}    {[-24.9934 0.9501]}

each element of the resulting cell array represents the prediction for the corresponding observation channel.

to display the mean values and standard deviations of the gaussian probability distribution for the predicted observations, use evaluate.

preddst = evaluate(tsnfcnappx, ...
    {rand(obsinfo(1).dimension),rand(obsinfo(2).dimension), ...
     rand(actinfo(1).dimension)})
preddst=1×4 cell array
    {4x1 single}    {2x1 single}    {4x1 single}    {2x1 single}

the result is a cell array in which the first and second element represent the mean values for the predicted observations in the first and second channel, respectively. the third and fourth element represent the standard deviations for the predicted observations in the first and second channel, respectively.

create an environment interface and extract observation and action specifications. alternatively, you can create specifications using rlnumericspec and rlfinitesetspec.

env = rlpredefinedenv("cartpole-continuous");
obsinfo = getobservationinfo(env);
actinfo = getactioninfo(env);

to approximate the reward function, create a deep neural network. for this example, the network has two input layers, one for the current action and one for the next observations. the single output layer contains a scalar, which represents the value of the predicted reward.

define each network path as an array of layer objects. get the dimensions of the observation and action spaces from the environment specifications, and specify a name for the input layers, so you can later explicitly associate them with the appropriate environment channel.

actionpath = featureinputlayer( ...
    actinfo.dimension(1), ...
    name="action");
nextstatepath = featureinputlayer( ...
    obsinfo.dimension(1), ...
    name="nextstate");
commonpath = [concatenationlayer(1,2,name="concat")
    fullyconnectedlayer(64)
    relulayer
    fullyconnectedlayer(64)
    relulayer
    fullyconnectedlayer(64)
    relulayer
    fullyconnectedlayer(1)];
net = layergraph(nextstatepath);
net = addlayers(net,actionpath);
net = addlayers(net,commonpath);
net = connectlayers(net,"nextstate","concat/in1");
net = connectlayers(net,"action","concat/in2");
plot(net)

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

create a dlnetwork object and display the number of weights.

net = dlnetwork(net);
summary(net);
   initialized: true
   number of learnables: 8.7k
   inputs:
      1   'nextstate'   4 features
      2   'action'      1 features

create a deterministic transition function object.

rwdfcnappx = rlcontinuousdeterministicrewardfunction(...
    net,obsinfo,actinfo,...
    actioninputnames="action", ...
    nextobservationinputnames="nextstate");

using this reward function object, you can predict the next reward value based on the current action and next observation. for example, predict the reward for a random action and next observation. since, for this example, only the action and the next observation influence the reward, use an empty cell array for the current observation.

act = rand(actinfo.dimension);
nxtobs = rand(obsinfo.dimension);
reward = predict(rwdfcnappx, {}, {act}, {nxtobs})
reward = single
    0.1034

to predict the reward, you can also use evaluate.

reward_ev = evaluate(rwdfcnappx, {act,nxtobs} )
reward_ev = 1x1 cell array
    {[0.1034]}

create an environment interface and extract observation and action specifications. alternatively, you can create specifications using rlnumericspec and rlfinitesetspec.

env = rlpredefinedenv("cartpole-continuous");
obsinfo = getobservationinfo(env);
actinfo = getactioninfo(env);

to approximate the is-done function, use a deep neural network. the network has one input channel for the next observations. the single output channel is for the predicted termination signal.

create the neural network as a vector of layer objects.

commonpath = [
    featureinputlayer( ...
                obsinfo.dimension(1), ...
                name="nextstate")
    fullyconnectedlayer(64)
    relulayer
    fullyconnectedlayer(64)
    relulayer
    fullyconnectedlayer(2)
    softmaxlayer(name="isdone")];
net = layergraph(commonpath);
plot(net)

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

covert the network to a dlnetwork object and display the number of weights.

net = dlnetwork(net);
summary(net);
   initialized: true
   number of learnables: 4.6k
   inputs:
      1   'nextstate'   4 features

create an is-done function approximator object.

isdonefcnappx = rlisdonefunction(...
    net,obsinfo,actinfo,...
    nextobservationinputnames="nextstate");

using this is-done function approximator object, you can predict the termination signal based on the next observation. for example, predict the termination signal for a random next observation. since for this example the termination signal only depends on the next observation, use empty cell arrays for the current action and observation inputs.

nxtobs = rand(obsinfo.dimension);
predisdone = predict(isdonefcnappx,{},{},{nxtobs})
predisdone = 0

you can obtain the termination probability using evaluate.

predisdoneprob = evaluate(isdonefcnappx,{nxtobs})
predisdoneprob = 1x1 cell array
    {2x1 single}
predisdoneprob{1}
ans = 2x1 single column vector
    0.5405
    0.4595

the first number is the probability of obtaining a 0 (no termination predicted), the second one is the probability of obtaining a 1 (termination predicted).

input arguments

environment transition function approximator object, specified as one of the following:

environment reward function approximator object, specified as one of the following:

environment is-done function approximator object, specified as an rlisdonefunction object.

observations, specified as a cell array with as many elements as there are observation input channels. each element of obs contains an array of observations for a single observation input channel.

the dimensions of each element in obs are mo-by-lb, where:

  • mo corresponds to the dimensions of the associated observation input channel.

  • lb is the batch size. to specify a single observation, set lb = 1. to specify a batch of observations, specify lb > 1. if valuerep or qvaluerep has multiple observation input channels, then lb must be the same for all elements of obs.

lb must be the same for both act and obs.

for more information on input and output formats for recurrent neural networks, see the algorithms section of .

action, specified as a single-element cell array that contains an array of action values.

the dimensions of this array are ma-by-lb, where:

  • ma corresponds to the dimensions of the associated action specification.

  • lb is the batch size. to specify a single observation, set lb = 1. to specify a batch of observations, specify lb > 1.

lb must be the same for both act and obs.

for more information on input and output formats for recurrent neural networks, see the algorithms section of .

next observations, that is the observation following the action act from the observation obs, specified as a cell array of the same dimension as obs.

output arguments

predicted next observation, that is the observation predicted by the transition function approximator tsnfcnappx given the current observation obs and the action act, retuned as a cell array of the same dimension as obs.

predicted reward, that is the reward predicted by the reward function approximator rwdfcnappx given the current observation obs, the action act, and the following observation nextobs, retuned as a single.

predicted is-done episode status, that is the episode termination status predicted by the is-done function approximator rwdfcnappx given the current observation obs, the action act, and the following observation nextobs, returned as a double.

note

if fcnappx is an rlcontinuousdeterministicrewardfunction object, then evaluate behaves identically to predict except that it returns results inside a single-cell array. if fcnappx is an rlcontinuousdeterministictransitionfunction object, then evaluate behaves identically to predict. if fcnappx is an rlcontinuousgaussiantransitionfunction object, then evaluate returns the mean value and standard deviation the observation probability distribution, while predict returns an observation sampled from this distribution. similarly, for an rlcontinuousgaussianrewardfunction object, evaluate returns the mean value and standard deviation the reward probability distribution, while predict returns a reward sampled from this distribution. finally, if fcnappx is an rlisdonefunction object, then evaluate returns the probabilities of the termination status being false or true, respectively, while predict returns a predicted termination status sampled with these probabilities.

version history

introduced in r2022a

网站地图