How can I generate code for my deep neural network with ScalingLayer?

4 views (last 30 days)
I am using GPU Coder in R2019b to generate CUDA code for my deep neural network policies following the workflow outlined here:
I get the following error:
"Error generating code for network policy_0. Code generation for ScalingLayer ActorScaling is not supported."
Is there a workaround for this unsupported layer?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Mar 2020
The workaround is to substitute the 'ScalingLayer' with an 'nnet.onnx.layer.ElementwiseAffineLayer'. However, this would involve using a attached p-coded script ('getNetworkFromRepresentation.p') to extract the network from the 'rlLayerRepresentation' object. The steps are outlined in the script below:
%%load agent MAT file
load agent
%%get actor representation (contains scalingLayer) from saved agent
actor = getActor(saved_agent);
%%get the new network with codegen-supported layer
actorNetwork = getNetworkFromRepresentation(actor);
newActorNetwork = actorNetwork;
% replace any scalingLayer with nnet.onnx.layer.ElementwiseAffineLayer. The
% name, scale, bias values of old scalingLayer are transfered to the new
% layer
for ct = 1:numel(newActorNetwork.Layers)
if isa(newActorNetwork.Layers(ct),'rl.layer.ScalingLayer')
newLayer = nnet.onnx.layer.ElementwiseAffineLayer(...
newActorNetwork.Layers(ct).Name,...
newActorNetwork.Layers(ct).Scale,...
newActorNetwork.Layers(ct).Bias);
newActorNetwork = replaceLayer(newActorNetwork,newActorNetwork.Layers(ct).Name,newLayer);
end
end
%%recreate the representation with net network
actionLayerName = newActorNetwork.Layers(end).Name;
observationLayerName = newActorNetwork.Layers(1).Name;
actionInfo = actor.ActionInfo;
observationInfo = actor.ObservationInfo;
newActor = rlRepresentation(newActorNetwork,observationInfo,actionInfo,...
'Observation',observationLayerName,'Action',actionLayerName,actor.Options);
saved_agent = setActor(saved_agent,newActor);
%%codegen
generatePolicyFunction(saved_agent)
cfg = coder.gpuConfig('mex');
cfg.TargetLang = 'C++';
cfg.GpuConfig.Enabled = true;
cfg.DeepLearningConfig = coder.DeepLearningConfig('cudnn');
argstr = '{ones(4,1)}';
codegen('-config','cfg','evaluatePolicy','-args',argstr,'-report');
Notes:
  • You are free to use the 'getNetworkFromRepresentation.p' script as you wish.
  • You will need to install the 'Deep Learning Toolbox Converter for ONNX Model Format support package'.

More Answers (0)

Categories

Find more on Deep Learning with GPU Coder in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!