Why do custom MATLAB functions and Deep Learning Toolbox 'Predict' block in MATLAB R2025a give different predictions with identical inputs, weights, and layers?

When using identical inputs, weights, biases, and layer structure, I observe that predictions from my custom MATLAB function differ from those of the Deep Learning Toolbox 'Predict' block. I am using a FeatureInputLayer with 'rescale-zero-one' normalization, and I manually apply normalization in my MATLAB code using the min and max values from the network. Why are the results different, and is there a way to ensure consistency? Also, is there a Simulink block for 'rescale-zero-one' normalization that supports FeatureInputLayer, and how can I manually modify weights and biases in exported Simulink models?

 Accepted Answer

Prediction discrepancies between a custom MATLAB function and the Deep Learning Toolbox Predict block or Simulink model, even with the same inputs, weights, biases, and layers, are most often due to differences in how input normalization is implemented. The Predict block and exported Simulink models automatically apply normalization as defined in the network's FeatureInputLayer, ensuring consistency with the network's training configuration. In contrast, custom MATLAB code requires manual implementation of normalization, which can introduce subtle differences if not matched exactly to the network's parameters and data formatting.
Explanation
  • The Predict block and Simulink models imported using exportNetworkToSimulink internally handle all preprocessing, including normalization, exactly as specified in the imported model.
  • Manual normalization in MATLAB code must use the exact same logic, order of operations, and data orientation as the FeatureInputLayer. Any deviation can result in different normalized values and thus different predictions.
  • Data formatting (such as row vs. column orientation) and implementation of activation functions (e.g., softmax) can also introduce differences if not carefully matched.
Simulink Support for 'rescale-zero-one' Normalization
  • The "Rescale-Zero-One 1D" block in Simulink supports both sequence and feature inputs.
  • If you encounter errors when using this block with a FeatureInputLayer, ensure the block's data format is set to "C" (channels only) in the block mask, rather than the default "CT" (channels and time) used for sequences.
  • Assign normalization parameters (Min, Max) from your FeatureInputLayer to the block to match the model's configuration.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!