how to save and reuse a trained neural network

612 Ansichten (letzte 30 Tage)
Omotayo Asiru
Omotayo Asiru am 18 Jan. 2016
Kommentiert: Hargun Kaur am 18 Mai 2023
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

Akzeptierte Antwort

Greg Heath
Greg Heath am 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 Kommentare
Pierre Pook
Pierre Pook am 7 Aug. 2017
Bearbeitet: Pierre Pook am 7 Aug. 2017
how do you specify a different directory to save the the network in ?
Abhijit Bhattacharjee
Abhijit Bhattacharjee am 17 Mai 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (6)

Pranab Das
Pranab Das am 20 Mai 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 Kommentar
Saeed Bello
Saeed Bello am 24 Feb. 2020
Simpply type
save(filename)
Then
load('filename.mat')
Remeber to include the file extension.

Melden Sie sich an, um zu kommentieren.


Saeed Bello
Saeed Bello am 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 am 22 Jan. 2016
save net ...
...
load net
Hope this helps.
Thank you for formally accepting my answer
Greg
  1 Kommentar
Omotayo Asiru
Omotayo Asiru am 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.

Melden Sie sich an, um zu kommentieren.


Ayesha Zafar
Ayesha Zafar am 17 Mai 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 Kommentar
Greg Heath
Greg Heath am 18 Mai 2019
  1. After you train net, what are the training,validation and test subset error rates ???
  2. tr = tr % = ?
Greg

Melden Sie sich an, um zu kommentieren.


navid salimpour
navid salimpour am 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 Kommentare
Nazila Pourhajy
Nazila Pourhajy am 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.
Hargun Kaur
Hargun Kaur am 18 Mai 2023
Thanks, this works very nicely

Melden Sie sich an, um zu kommentieren.


The Anh Vuong
The Anh Vuong am 20 Sep. 2021
Bearbeitet: The Anh Vuong am 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

Kategorien

Mehr zu Sequence and Numeric Feature Data Workflows finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by