deploy trained reinforcement learning policies -凯发k8网页登录

main content

deploy trained reinforcement learning policies

once you train a reinforcement learning agent, you can generate code to deploy the optimal policy. you can generate:

  • cuda® code for policies relying on deep neural networks, using gpu coder™

  • c/c code for policies relying on tables, deep neural networks, or linear basis functions, using matlab® coder™

code generation is supported for policies using feedforward neural networks in any of the input paths, provided that all the used layers are supported. code generation is not supported for policies containing a recurrent neural network (rnn).

to generate a policy evaluation function that selects an action based on a given observation, use . you can generate code to deploy this policy function using gpu coder or matlab coder. this function also creates a data file which stores policy information (such as for example the trained neural network for the actor). the evaluation function loads this data file to properly initialize itself the first time it is called.

to generate a simulink® policy evaluation block that selects an action based on a given observation, use . you can generate code to deploy this policy block using simulink coder. this function also creates a data file which stores policy information (such as for example the trained neural network for the actor). the generated policy block loads this data file to properly initialize itself prior to simulation. you can use the block to simulate the policy and generate code for deployment purposes..

note

the deployed policy might have an internal state. in this case, the internal state must be consistent when comparing the simulated and deployed policy.

generate code using gpu coder

if your trained optimal policy uses a deep neural network, you can generate cuda code for the policy using gpu coder. for more information on supported gpus see gpu computing requirements (parallel computing toolbox). there are several required and recommended prerequisite products for generating cuda code for deep neural networks. for more information, see installing prerequisite products (gpu coder) and (gpu coder).

not all deep neural network layers support gpu code generation. for a list of supported layers, see supported networks, layers, and classes (gpu coder). for more information and examples on gpu code generation, see deep learning with gpu coder (gpu coder).

generate cuda code for deep neural network policy

as an example, generate gpu code for the policy gradient agent trained in .

load the trained agent.

load('matlabcartpolepg.mat','agent')

create a policy evaluation function for this agent.

generatepolicyfunction(agent)

this command creates the evaluatepolicy.m file, which contains the policy function, and the agentdata.mat file, which contains the trained deep neural network actor. in general, for a given observation, the policy function evaluates a probability for each potential action using the actor network. then, the policy function randomly selects an action based on these probabilities (for for sac agents the policy function samples an unbounded action using the mean and the standard deviation obtained from the network, and then applies bounds, scale and bias to get the correct action).

you can generate code for this network using gpu coder. for example, you can generate a cuda compatible mex function.

configure the codegen function to create a cuda compatible c mex function.

cfg = coder.gpuconfig('mex');
cfg.targetlang = 'c  ';
cfg.deeplearningconfig = coder.deeplearningconfig('cudnn');

set an example input value for the policy evaluation function. to find the observation dimension, use the getobservationinfo function. in this case, the observations are in a four-element vector.

argstr = '{ones(4,1)}';

generate code using the codegen function.

codegen('-config','cfg','evaluatepolicy','-args',argstr,'-report');

this command generates the mex function evaluatepolicy_mex.

generate code using matlab coder

you can generate c/c code for table, deep neural network, or linear basis function policies using matlab coder.

using matlab coder, you can generate:

  • c/c code for policies that use q tables, value tables, or linear basis functions. for more information on general c/c code generation, see (matlab coder).

  • c code for policies that use deep neural networks. note that code generation is not supported for continuous actions pg, ac, ppo, and sac agents using a recurrent neural network (rnn). for a list of supported layers, see networks and layers supported for code generation (matlab coder). for more information, see (matlab coder) and deep learning with matlab coder (matlab coder).

generate c code for deep neural network policy without using any third-party library

as an example, generate c code without dependencies on third-party libraries for the policy gradient agent trained in .

load the trained agent.

load('matlabcartpolepg.mat','agent')

create a policy evaluation function for this agent.

generatepolicyfunction(agent)

this command creates the evaluatepolicy.m file, which contains the policy function, and the agentdata.mat file, which contains the trained deep neural network actor. for a given observation, the policy function evaluates a probability for each potential action using the actor network. then, the policy function randomly selects an action based on these probabilities (for for sac agents the policy function samples an unbounded action using the mean and the standard deviation obtained from the network, and then applies bounds, scale and bias to get the correct action).

configure the codegen function to generate code suitable for building a mex file.

cfg = coder.config('mex');

on the configuration object, set the target language to c , and set deeplearningconfig to 'none'. this option generates code without using any third-party library.

cfg.targetlang = 'c';
cfg.deeplearningconfig = coder.deeplearningconfig('none'); 

set an example input value for the policy evaluation function. to find the observation dimension, use the getobservationinfo function. in this case, the observations are in a four-element vector.

argstr = '{ones(4,1)}';

generate code using the codegen function.

codegen('-config','cfg','evaluatepolicy','-args',argstr,'-report');

this command generates the c code for the policy gradient agent containing the deep neural network actor.

generate c code for deep neural network policy using third-party libraries

as an example, generate c code for the policy gradient agent trained in using the intel math kernel library for deep neural networks (mkl-dnn).

load the trained agent.

load('matlabcartpolepg.mat','agent')

create a policy evaluation function for this agent.

generatepolicyfunction(agent)

this command creates the evaluatepolicy.m file, which contains the policy function, and the agentdata.mat file, which contains the trained deep neural network actor. for a given observation, the policy function evaluates a probability for each potential action using the actor network. then, the policy function randomly selects an action based on these probabilities.

configure the codegen function to generate code suitable for building a mex file.

cfg = coder.config('mex');

on the configuration object, set the target language to c , and set deeplearningconfig to the target library 'mkldnn'. this option generates code using the intel math kernel library for deep neural networks (intel mkl-dnn).

cfg.targetlang = 'c  ';
cfg.deeplearningconfig = coder.deeplearningconfig('mkldnn'); 

set an example input value for the policy evaluation function. to find the observation dimension, use the getobservationinfo function. in this case, the observations are in a four-element vector.

argstr = '{ones(4,1)}';

generate code using the codegen function.

codegen('-config','cfg','evaluatepolicy','-args',argstr,'-report');

this command generates the c code for the policy gradient agent containing the deep neural network actor.

generate c code for q table policy

as an example, generate c code for the q-learning agent trained in train reinforcement learning agent in basic grid world.

load the trained agent.

load('basicgwqagent.mat','qagent')

create a policy evaluation function for this agent.

generatepolicyfunction(qagent)

this command creates the evaluatepolicy.m file, which contains the policy function, and the agentdata.mat file, which contains the trained q table value function. for a given observation, the policy function looks up the value function for each potential action using the q table. then, the policy function selects the action for which the value function is greatest.

set an example input value for the policy evaluation function. to find the observation dimension, use the getobservationinfo function. in this case, there is a single one dimensional observation (belonging to a discrete set of possible values).

argstr = '{[1]}';

configure the codegen function to generate embeddable c code suitable for targeting a static library, and set the output folder to buildfolder.

cfg = coder.config('lib');
outfolder = 'buildfolder';

generate c code using the codegen function.

codegen('-c','-d',outfolder,'-config','cfg',...
    'evaluatepolicy','-args',argstr,'-report');

see also

functions

  • |

related examples

more about

网站地图