loading and training an existing network.
Show older comments
I am trying define a network, then train it in multiple sessions. The problem is that I can't get the load or read of the network to work in the second session. The code is:
layers = [ ...
sequenceInputLayer(270)
bilstmLayer(numHiddenUnits,OutputMode="last")
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer]
options = trainingOptions("adam", ...
InitialLearnRate=0.002,...
MaxEpochs=15, ...
Shuffle="never", ...
GradientThreshold=1, ...
Verbose=false, ...
ExecutionEnvironment="gpu", ...
Plots="training-progress");
clabels=categorical(labels);
numLables=numel(clabels)
load("savednet.mat","layers");
net = trainNetwork(data,clabels,layers,options);
save("savednet","net");
I have tried many variations of the load command and it always gives an error on the second argument:
Warning: Variable 'layers' not found.
Exactly what should that look like and then how should it be used as input to the trainNetwork routine?
7 Comments
Walter Roberson
on 7 Oct 2024
layers = [ ...
sequenceInputLayer(270)
bilstmLayer(numHiddenUnits,OutputMode="last")
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer]
You define a variable named layers
load("savednet.mat","layers");
You try to overwrite the variable named layers with the content of the variable layers stored in savednet.mat but that variable does not exist in that .mat
save("savednet","net");
Notice you do not save layers in savednet.mat
Mark Hubelbank
on 7 Oct 2024
Walter Roberson
on 7 Oct 2024
Remove
load("savednet.mat","layers");
Change
save("savednet","net");
to
save("savednet","net","layers");
Afterwards, to do additional work on the saved network,
load("savednet","net","layers");
Mark Hubelbank
on 7 Oct 2024
Walter Roberson
on 7 Oct 2024
filename = "savednet.mat";
if isfile(filename)
clear layers net
try
load(filename, "net", "layers");
catch ME
end
end
if ~exist("layers", "var") || ~exist("net", "var")
layers = [ ...
sequenceInputLayer(270)
bilstmLayer(numHiddenUnits,OutputMode="last")
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer]
options = trainingOptions("adam", ...
InitialLearnRate=0.002,...
MaxEpochs=15, ...
Shuffle="never", ...
GradientThreshold=1, ...
Verbose=false, ...
ExecutionEnvironment="gpu", ...
Plots="training-progress");
clabels=categorical(labels);
numLables=numel(clabels);
net = trainNetwork(data,clabels,layers,options);
save(filename, "net", "layers");
end
The above code loads layers and net from the file if possible, and if that fails then it creates and trains the network and saves it.
Mark Hubelbank
on 7 Oct 2024
Moved: Walter Roberson
on 7 Oct 2024
Walter Roberson
on 7 Oct 2024
Probably
net1 = trainnet(data,clabels,net1,"crossentropy",options);
Accepted Answer
More Answers (0)
Categories
Find more on Deep Learning Toolbox 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!