Update Network Parameters at Run Time
You can update deep learning network parameters at run time without regenerating code. This workflow is known as online update of learnables. When you enable online updates for learnable parameters, you can update the networks parameters incrementally.
You can enable online update of the learnable parameters for:
A
dlnetwork
(Deep Learning Toolbox) object that contains these layers with learnable parameters:Custom deep learning layers
fullyConnectedLayer
(Deep Learning Toolbox)lstmLayer
(Deep Learning Toolbox)bilstmLayer
(Deep Learning Toolbox)gruLayer
(Deep Learning Toolbox)lstmProjectedLayer
(Deep Learning Toolbox)gruProjectedLayer
(Deep Learning Toolbox)
Generating generic C/C++ or plain CUDA code for deep learning networks.
Generating generic C/C++ and plain CUDA code for Simulink® models.
Simulating Simulink models that contain MATLAB Function blocks, when the model configuration parameter Target library is set to
none
. For more information, see Deep learning library for simulation (Simulink).
Update the Network Parameters
This example shows how to create an entry-point function named
mPredict
that continuously updates the learnables of a deep
learning network.
The function:
Loads the trained network into a persistent
dlnetwork
object nameddlnet
by using thecoder.loadDeepLearningNetwork
function. A persistent variable allows you to preserve the updated learnables for subsequent function calls.Enables run-time update of learnables by using the
coder.ai.enableParameterUpdate
function. You must enable the learnables update of thedlnetwork
object before updating the learnable parameters.Updates the learnable parameters with the variable
newLearnables
.Calls the
predict
method to predict the responses by using the updated network.
function dlOut = mPredict(dlIn, newLearnables, matfile) %#codegen persistent dlnet if isempty(dlnet) dlnet = coder.loadDeepLearningNetwork(matfile); end % Enables learnables update dlnet = coder.ai.enableParameterUpdate(dlnet); % Update learnables dlnet.Learnables = newLearnables; dlOut = dlnet.predict(dlIn); end
Limitations
Online update of learnables is only supported for generating plain CUDA code that does not depend on third-party libraries. You cannot enable online updates of learnables for:
Generating code by using any third-party libraries in MATLAB or Simulink.
Simulating a model by using the deep learning target library other than
"none"
.A network that:
You pass to the entry-point function as an input.
Loaded by using the
coder.load
function.Loaded by calling a compile-time extrinsic function.
A nested network, which is a
dlnetwork
object with a custom layer that contains anotherdlnetwork
object as a learnable parameter object. For more information, see Define Nested Deep Learning Layer Using Network Composition (Deep Learning Toolbox).
See Also
coder.ai.enableParameterUpdate
| coder.loadDeepLearningNetwork
| dlnetwork
(Deep Learning Toolbox)
Topics
- Update the Network Learnables for a Battery State of Charge Estimation Model (Simulink Coder)
- Define Custom Deep Learning Layer with Learnable Parameters (Deep Learning Toolbox)
- Define Nested Deep Learning Layer Using Network Composition (Deep Learning Toolbox)
- Supported Networks, Layers, and Classes
- Load Pretrained Networks for Code Generation