Clear Filters
Clear Filters

How to index Neural Network for loop

2 views (last 30 days)
I would like to run this loop 4 times for the InitialLearnRate values of 0.0001, 0.001, 0.01, and 0.1. I would like to index the loop as well so I can compare the fracCorrect for each loop. Thank you!
InitialLearnRate = [0.0001,0.001,0.01,0.1]
augmentedDS_test = zeros(1,length(InitialLearnRate))
predictions = zeros(1,length(InitialLearnRate))
fracCorrect = zeros(1,length(InitialLearnRate))
for i = InitialLearnRate
imageDS = imageDatastore('deeplearning_course_files','IncludeSubfolders',true,'LabelSource','foldernames');
[wormTrain,wormTest] = splitEachLabel(imageDS,0.2); % takes x images from
augmentedDS_train = augmentedImageDatastore([227 227],wormTrain,'ColorPreprocessing','gray2rgb')
augmentedDS_test = augmentedImageDatastore([227 227],wormTest,'ColorPreprocessing','gray2rgb')
net = alexnet;
layers = net.Layers
fc = fullyConnectedLayer(2);
layers(end-2) = fc;
layers(end) = classificationLayer;
options = trainingOptions('sgdm','InitialLearnRate',i,'Momentum',0.1,'MaxEpochs',15)
[wormnet,info] = trainNetwork(augmentedDS_train,layers,options);
predictions = classify(wormnet,augmentedDS_test);
wormActual = wormTest.Labels;
numCorrect = nnz(predictions == wormActual);
fracCorrect = numCorrect/numel(predictions)
end
confusionchart(wormTest.Labels,predictions)
plot(info.TrainingLoss)

Accepted Answer

Anshika Chaurasia
Anshika Chaurasia on 6 Aug 2020
You can consider trying indexing as given below:
for i = 1:length(InitialLearnRate)
....
options = trainingOptions('sgdm','InitialLearnRate',InitialLearnRate(i),'Momentum',0.1,'MaxEpochs',15)
....
fracCorrect(i) = numCorrect/numel(predictions)
...
end

More Answers (0)

Categories

Find more on Image Data Workflows 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!