Main Content

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:

  • 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:

  1. Loads the trained network into a persistent dlnetwork object named dlnet by using the coder.loadDeepLearningNetwork function. A persistent variable allows you to preserve the updated learnables for subsequent function calls.

  2. Enables run-time update of learnables by using the coder.ai.enableParameterUpdate function. You must enable the learnables update of the dlnetwork object before updating the learnable parameters.

  3. Updates the learnable parameters with the variable newLearnables.

  4. 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 generic C/C++ 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 another dlnetwork object as a learnable parameter object. For more information, see Define Nested Deep Learning Layer Using Network Composition (Deep Learning Toolbox).

See Also

| | (Deep Learning Toolbox)

Topics