I just trained a neural network, and I would like to test it with a new data set that were not included in the training so as to check its performance on new data. This is my code:
net = patternnet(30);
net = train(net,x,t);
save (net);
y = net(x);
perf = perform(net,t,y)
classes = vec2ind(y);
where x and t are my input and target, respectively. I understand that save net
and load net;
can be used, but my questions are as follows:
At what point in my code should I use save net
?
Using save net;
, which location on the system is the trained network saved?
When I exit and open MATLAB again, how can I load the trained network and supply new data that I want to test it with?
Please Note: I have discovered that each time I run my code, it gives a different output which I do not want once I have an acceptable result. I want to be able to save the trained neural network such that when I run the code over and over again with the training data set, it gives the same output.
If you just call save net
, all your current variables from the workspace will be saved as net.mat
. You want to save only your trained network, so you need to use save('path_to_file', 'variable')
. For example:
save('C:\Temp\trained_net.mat','net');
In this case the network will be saved under the given file name.
The next time you want to use the saved pre-trained network you just need to call load('path_to_file')
. If you don't reinitialize or train this network again, the performance will be the same as before, because all weights and bias values will be the same.
You can see used weights and bias values by checking variables like net.IW{i,j}
(input weights), net.LW{i,j}
(layer weights) and net.b{i}
(bias). As long as they stay the same, the network's performance stay the same.
Train and save
[x,t] = iris_dataset;
net = patternnet;
net = configure(net,x,t);
net = train(net,x,t);
save('C:\Temp\trained_net.mat','net');
y = net(x);
perf = perform(net,t,y);
display(['performance: ', num2str(perf)]);
It returns performance: 0.11748
in my case. The values will be different after each new training.
Load and use
clear;
[x,t] = iris_dataset;
load('C:\Temp\trained_net.mat');
y = net(x);
perf = perform(net,t,y);
display(['performance: ', num2str(perf)]);
It returns performance: 0.11748
. The values will be the same when using the network on the same data set. Here we used the training set again.
If you get an absolutely new data set, the performance will be different, but it will always be the same for this particular data set.
clear;
[x,t] = iris_dataset;
%simulate a new data set of size 50
data_set = [x; t];
data_set = data_set(:,randperm(size(data_set,2)));
x = data_set(1:4, 1:50);
t = data_set(5:7, 1:50);
load('C:\Temp\trained_net.mat');
y = net(x);
perf = perform(net,t,y);
display(['performance: ', num2str(perf)]);
It returns performance: 0.12666
in my case.