Plot performance of validation and test set (gives NaN)

4 views (last 30 days)
Hi, I want to train a feedforward neural net. I split my data in 3 equally large sets for training, validation and testing. After training I want to show performance on the 3 test sets. Problem is, plotperform() only shows my training MSE and when calling tr.best_tperf I get 'NaN'. Below is my code, does anyone know what is the problem?
RandomIndex = randperm(length(X1),3000);
X1_train = X1(RandomIndex);
X2_train = X2(RandomIndex);
Tnew_train = Tnew(RandomIndex);
net = feedforwardnet(10, 'trainlm');
net.divideParam.trainRatio = 1/3;
net.divideParam.valRatio = 1/3;
net.divideParam.testRatio = 1/3;
net.trainParam.epochs = 10;
net.layers{1}.transferFcn = 'logsig';
[net,tr] = train(net, p_train, t_train);
plotperform(tr);

Answers (1)

Jayanti
Jayanti on 3 Jul 2025
Hi Alexs,
As per the given code you are manually selecting a shuffled subset of 3000 samples using "randperm", but you later call the "train()" function with p_train" and "t_train", which are not defined in the code and may not include the full dataset required for MATLAB to automatically split it into training, validation, and testing subsets.
If the inputs passed to "train()" contain only training data, the validation and test subsets might become extremly small or empty, resulting in Nan values and incomplete performance plots.
To avoid this try providing your complete, undivided input and target datasets to the "train()" function. MATLAB will then handle the 1/3, 1/3, 1/3 split internally based on your "net.divideParam" settings.
You can update your code as below:
% Prepare input data
P_all = [X1; X2];
T_all = Tnew;
% Train with complete Dataset
[net,tr] = train(net, P_all, T_all);
Happy coding!

Community Treasure Hunt

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

Start Hunting!