How to utilize mse or l1loss when there are NaN values?

3 views (last 30 days)
Hi, I am trying to make a neural network with a custom loop and I have a dataset with 3 outputs and 4 "experiments". In most of the "experiments" I have numeric datapoints. How can I utilize MSE or l1loss if some of the data is incomplete?
Example:
T = dlarray(rand(3,4)); % "Target values"
Y_c = 1.03*T; % Assume this is a matrix calculated by a neural network
T(1,4) = NaN; % This value is artificially made as a "NaN"
Loss = l2loss(Y_c,T,NormalizationFactor="all-elements",DataFormat="CB")
The idea of this code is just to learn how to handle NaN values because I do not want to discard the "experiment of the last column.
In the shallow neural toolbox I just write NaN instead of the numeric values and the toolbox automatically removes the value from the performance funciton. This does not seem to be the case for the l2loss. How can I make l1loss automatically remove "NaN" so that it gives me a numeric performance?
P.S. I tried using "mse" as well but it does not support dlarrays.

Accepted Answer

Meet
Meet on 23 Sep 2024
Hi Marko,
Currently, the built-in functions for calculating loss do not automatically exclude ‘NaN’ values from the input. As a result, we need to explicitly handle these ‘NaN’ values by either masking them or replacing them with the mean of the dataset, among other techniques.
Below is an example where ‘NaN’ values from the input are masked to zero, and the “l2loss” is calculated on the inputs.
T = dlarray(rand(3,4)); % "Target values"
Y_c = 1.03*T; % Assume this is a matrix calculated by a neural network
T(1,4) = NaN; % This value is artificially made as a "NaN"
% Create a mask to identify non-NaN values
mask = ~isnan(T);
% Use logical indexing to exclude NaN values
T_masked = T(mask);
Y_c_masked = Y_c(mask);
Loss = l2loss(Y_c_masked, T_masked, NormalizationFactor="all-elements",DataFormat="CB")
Loss =
1x1 dlarray 2.9810e-04
You can refer to the documentation link below for more information on “l2loss”: https://www.mathworks.com/help/releases/R2021b/deeplearning/ref/dlarray.l2loss.html

More Answers (0)

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!