how to save and reuse a trained neural network
조회 수: 264 (최근 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
댓글 수: 0
채택된 답변
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
2017년 8월 7일
편집: Pierre Pook
2017년 8월 7일
how do you specify a different directory to save the the network in ?
Abhijit Bhattacharjee
2022년 5월 17일
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
2019년 5월 20일
Hi ,
When im using save command i'm getting this error.
can you please give reference to debug this error
댓글 수: 1
Saeed Bello
2020년 2월 24일
Simpply type
save(filename)
Then
load('filename.mat')
Remeber to include the file extension.
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.
댓글 수: 0
Greg Heath
2016년 1월 22일
save net ...
...
load net
Hope this helps.
Thank you for formally accepting my answer
Greg
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
2019년 5월 18일
- After you train net, what are the training,validation and test subset error rates ???
- tr = tr % = ?
Greg
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
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!