how to save and reuse a trained neural network

238 views (last 30 days)
I just trained a neural network and i will like to test it with 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; can be used but my questions are as follows ; 1.At what point in my code will i put save net 2.Using save net;, which location on the system is the trained network saved? 3.How can i load the trained network and supply new data that i want to test it with? Please Note: 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 same output. 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. Please help

Accepted Answer

Greg Heath
Greg Heath on 26 Jan 2016
1.At what point in my code will i put save net
Any time after training it and before deleting it.
However, give it a unique name so that it is not overwritten
or used by mistake.
gregnet1 = net;
save gregnet1
2.Using save net;, which location on the system is the trained network saved?
What ever directory you are in when you save it UNLESS you
specify another directory.
3.How can i load the trained network and supply new data that i want to test it with?
load gregnet1
newoutput = gregnet1(newinput);
Please Note: 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 same output. 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.
Then initialize the RNG to the same state before training to
obtain reproducibility. See any of my training example posts.
Hope this helps.
Thank you for formally accepting my answer
Greg
  4 Comments
Pierre Pook
Pierre Pook on 7 Aug 2017
Edited: Pierre Pook on 7 Aug 2017
how do you specify a different directory to save the the network in ?
Abhijit Bhattacharjee
Abhijit Bhattacharjee on 17 May 2022
You can use a different syntax for the SAVE command:
outputDir = "path_to_dir";
outputFile = fullfile(outputDir, "net.mat");
save(outputFile, "net");
In the case above, net is the name of the network in the MATLAB workspace, and we are saving it to the location path_to_dir/net.mat.

Sign in to comment.

More Answers (6)

Pranab Das
Pranab Das on 20 May 2019
Hi ,
When im using save command i'm getting this error.
can you please give reference to debug this errorScreenshot from 2019-05-20 11-37-59.png
  1 Comment
Saeed Bello
Saeed Bello on 24 Feb 2020
Simpply type
save(filename)
Then
load('filename.mat')
Remeber to include the file extension.

Sign in to comment.


Saeed Bello
Saeed Bello on 24 Feb 2020
You get error while loading your saved net becuase your filename is not complete (undefined).
Don't forget that on your current folder, the filneame you save your net with has '.mat' file extension.
First save your net as:
save(gregnet1)
The correct way to load it again is
load('gregnet1.mat')
Hope this help.

Greg Heath
Greg Heath on 22 Jan 2016
save net ...
...
load net
Hope this helps.
Thank you for formally accepting my answer
Greg
  1 Comment
Omotayo Asiru
Omotayo Asiru on 25 Jan 2016
Thanks for your response but this has not answered my question.As i said in my question,i know you save net and load net can be used but my questions are: 1.At what point in my code will i put save net 2.Using save net;, which location on the system is the trained network saved? 3.How can i load the trained network and supply new data that i want to test it with? Please Note: 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 same output. 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.

Sign in to comment.


Ayesha Zafar
Ayesha Zafar on 17 May 2019
hello!
I am trying to save and load the net but when i test the net keeping train function in comment.It give error that is "Undefined function or variable 'gregnet1'. " Attached screenshot is explaining the problem
close all, clear all, clc, format compact,
img = imread('nn3.bmp');
% imshow(img);
% imgGray = rgb2gray(img);
% imgCrop = imcrop(imgGray);
% imshow(imgCrop);
% imgLGE=imresize(imgCrop,[7,5 ]);
% imshow(imgLGE);
% imgRTE = imrotate(imgLGE, 35);
% imshow(imgRTE);
% imgBW = im2bw(imgLGE, 0.90455);
% imshow(imgBW);
% imwrite(imgBW,'nine3.bmp');
input = imread('nine.bmp');
corresponding target output vector
output=[ -1 -1 -1 -1 -1 -1 -1 -1 -1 1];
net = network( 1,1,0,1,0,1);
net.layers{1}.size = 10;
net.layers{1}.transferFcn = 'tansig';
net = configure(net,input(:),output(:));
% net = init(net);
view(net);
initial_output = net(input(:))
net.trainFcn = 'traingd'; %the term “backpropagation” is sometimes used to refer specifically to the gradient descent algorithm,
[net,tr]= train(net,input(:),output(:)); %training record= tr
final_output = net(input(:))
gregnet1 = net;
save gregnet1
%test
% output=sim(net,input(:))
load gregnet1
newoutput = gregnet1(input(:))
  1 Comment
Greg Heath
Greg Heath on 18 May 2019
  1. After you train net, what are the training,validation and test subset error rates ???
  2. tr = tr % = ?
Greg

Sign in to comment.


navid salimpour
navid salimpour on 2 Sep 2019
Hi omotayo-asiru, How are you?
did you solve this problem? i have same problem and i dont know how to solve that.
  3 Comments
Nazila Pourhajy
Nazila Pourhajy on 9 Sep 2021
Hi everyone
It is better to use the following format to save the trained network:
save('filename.mat','net');
And for load trained network:
load('filename.mat','name of varaible for loading network in it');
for example : load('filename.mat','net1');
and predict with net1 in your program.

Sign in to comment.


The Anh Vuong
The Anh Vuong on 20 Sep 2021
Edited: The Anh Vuong on 20 Sep 2021
Hi , I would like to share with you this code, it is working in a different working environment.
First in your training program, after trainig save network
...
net1 = net
save ('your_reuse.mat', 'net1')
--.
Than create a new mlx : eg. reuse.mlx with a prediction for testimage.png and a Network with 4 precision-digits
load ('your_reuse.mat', 'net1')
filename = "testimage.png";
im = imread(filename);
I = imresize(im, [224 224]);
[label,scores] = classify(net1,I);
imshow(I)
title(string(filename)+ " typ: " + string(label) + ", " + num2str(100*max(scores),4) + "%");
l

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!