Search code examples
matlabmatlab-deploymentmatlab-compiler

Preload variable in Matlab Docker Microservice


I am working on a project where I've developed a neural network model in MATLAB. I want to deploy this model as a microservice using Docker, but I'm facing a challenge preloading the neural network weights.

To give a bit more context:

My neural network model is saved with weights in a .mat file. I want microservice to preload these weights upon startup to avoid loading them every time a request is made. This is crucial for performance reasons.

The microservice will be receiving input data, processing it through the neural network, and then sending back the predictions.

I've successfully dockerized my MATLAB application (using docs), but I'm not sure about the best way to preload and maintain the weights in memory.

function predictions = my_app(input_data)
    % Load weights (I want to avoid doing this on every request)
    load('neural_network_weights.mat');

    % ... process data and make predictions ...
end

Solution

  • I think you simply need to stash the weights in a persistent variable. I.e.

    function predictions = my_app(input_data)
        persistent WEIGHTS
        
        if isempty(WEIGHTS)
            % Load weights first time only
            WEIGHTS = load('neural_network_weights.mat');
        end
        % ... process data and make predictions using WEIGHTS
    end