Creating unchangeable properties of a MATLAB machine learning model object

15 visualizzazioni (ultimi 30 giorni)
Once I generate a machine learning model in MATLAB, the model is often represented as an object. How do I set the object's property for user data or meta data properties to not be changeable or resilient to change after that data has been assigned to it? I would like to do this for models created with your Statistics and Machine Learning Toolbox and Deep Learning Toolbox (for deep learning model objects and shallow neural network objectts).
Is there a way I could embed the object into another object that carries around immutable unique identifers and creation history in a way that is not changeable by the user after the model creator has set that info? I would like to add this as part of my source control and quality control for the models

Risposte (1)

Parag
Parag il 22 Gen 2025 alle 11:50
Hi, to make properties of an object in MATLAB immutable after they have been set, you can utilize MATLAB's object-oriented programming features. Below is an implementation that incorporates the concept of immutable properties using object-oriented programming. We will create a custom class ‘ImmutableModel that encapsulates the neural network model and includes immutable properties for a unique identifier and creation history.
Include Class in .m file
classdef ImmutableModel
properties(SetAccess = private)
Model % The machine learning model object
UniqueID % Unique identifier for the model
CreationHistory % Metadata about the model's creation
end
methods
function obj = ImmutableModel(model, uniqueID, creationHistory)
% Constructor to initialize the model and metadata
obj.Model = model;
obj.UniqueID = uniqueID;
obj.CreationHistory = creationHistory;
end
end
end
Code for implementation
% Main script to create and use the ImmutableModel class
% 1. Input data (X)
X = [0 0;
0 1;
1 0;
1 1]; % Input data, 4 samples with 2 features
% 2. Target data (Y)
Y = [0; 1; 1; 0]; % Target data (desired output)
% 3. Create a simple feedforward neural network
hiddenLayerSize = 2; % Number of neurons in the hidden layer
net = feedforwardnet(hiddenLayerSize); % Create a feedforward network
% 4. Set the inputs and targets for training
net = configure(net, X', Y'); % Configure the network with the input and target data
% 5. Create an ImmutableModel object
uniqueID = 'Model_001'; % Example unique identifier
creationHistory = 'Created for binary classification task on XOR data'; % Example creation history
immutableModel = ImmutableModel(net, uniqueID, creationHistory);
% Display the immutable properties
fprintf('Unique ID: %s\n', immutableModel.UniqueID);
fprintf('Creation History: %s\n', immutableModel.CreationHistory);
% Verify that the neural network is encapsulated in an object
isObject = isobject(immutableModel.Model);
fprintf('Neural Network is an object: %d\n', isObject);
  • ImmutableModel Class: This class encapsulates the neural network (Model), along with UniqueID and CreationHistory properties. These properties are set to be immutable by using SetAccess = private, meaning they can only be set within the class constructor.
  • Constructor: The constructor initializes the Model, UniqueID, and CreationHistory properties. Once set, these properties cannot be changed from outside the class.
  • Main Script: In the main script, we create a neural network and configure it with input and target data. We then create an instance of ImmutableModel, passing the network, a unique identifier, and creation history as arguments.
This approach ensures that critical metadata about the model remains consistent and unchangeable after initial assignment, thereby supporting quality control and traceability.

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by