how to save and reuse a trained neural network

585 visualizaciones (últimos 30 días)
Omotayo Asiru
Omotayo Asiru el 18 de En. de 2016
Comentada: Hargun Kaur el 18 de Mayo de 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

Respuesta aceptada

Greg Heath
Greg Heath el 26 de En. de 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 comentarios
Pierre Pook
Pierre Pook el 7 de Ag. de 2017
Editada: Pierre Pook el 7 de Ag. de 2017
how do you specify a different directory to save the the network in ?
Abhijit Bhattacharjee
Abhijit Bhattacharjee el 17 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (6)

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

Iniciar sesión para comentar.


Saeed Bello
Saeed Bello el 24 de Feb. de 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 el 22 de En. de 2016
save net ...
...
load net
Hope this helps.
Thank you for formally accepting my answer
Greg
  1 comentario
Omotayo Asiru
Omotayo Asiru el 25 de En. de 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.

Iniciar sesión para comentar.


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

Iniciar sesión para comentar.


navid salimpour
navid salimpour el 2 de Sept. de 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 comentarios
Nazila Pourhajy
Nazila Pourhajy el 9 de Sept. de 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 el 18 de Mayo de 2023
Thanks, this works very nicely

Iniciar sesión para comentar.


The Anh Vuong
The Anh Vuong el 20 de Sept. de 2021
Editada: The Anh Vuong el 20 de Sept. de 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

Categorías

Más información sobre Sequence and Numeric Feature Data Workflows en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by