how to save and reuse a trained neural network

269 次查看(过去 30 天)
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

采纳的回答

Greg Heath
Greg Heath 2016-1-26
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 个评论
Pierre Pook
Pierre Pook 2017-8-7
编辑:Pierre Pook 2017-8-7
how do you specify a different directory to save the the network in ?
Abhijit Bhattacharjee
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.

请先登录,再进行评论。

更多回答(6 个)

Pranab Das
Pranab Das 2019-5-20
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

Saeed Bello
Saeed Bello 2020-2-24
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 2016-1-22
save net ...
...
load net
Hope this helps.
Thank you for formally accepting my answer
Greg
  1 个评论
Omotayo Asiru
Omotayo Asiru 2016-1-25
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.

请先登录,再进行评论。


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

请先登录,再进行评论。


navid salimpour
navid salimpour 2019-9-2
Hi omotayo-asiru, How are you?
did you solve this problem? i have same problem and i dont know how to solve that.
  3 个评论
Nazila Pourhajy
Nazila Pourhajy 2021-9-9
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.

请先登录,再进行评论。


The Anh Vuong
The Anh Vuong 2021-9-20
编辑:The Anh Vuong 2021-9-20
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

类别

Help CenterFile Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by