Validation Accuracy on Neural network
Afficher commentaires plus anciens
Hello..I wonder if any of you who have used deep learning on matlab can help me to troubleshoot my problem. I don't understand why I got a sudden drop of my validation accuracy at the end of the graph? It's a simple network with one convolution layer to classify cases with low or high risk of having breast cancer. After the final iteration it displays a validation accuracy of above 80% but then suddenly it dropped to 73% without an iteration. I don't understand that.

Here's my code
%set training dataset folder
digitDatasetPath = fullfile('C:\Users\UOS\Documents\Desiree Data\Run
2\dataBreast\training2');
%training set
imdsTrain = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
%set validation dataset folder
validationPath = fullfile('C:\Users\UOS\Documents\Desiree Data\Run
2\dataBreast\validation2');
%testing set
imdsValidation = imageDatastore(validationPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
%create a clipped ReLu layer
layer = clippedReluLayer(10,'Name','clip1');
% define network architecture
layers = [
imageInputLayer([256 256 1]);
% conv_1
convolution2dLayer(3,32,'Stride',1)
batchNormalizationLayer
clippedReluLayer(10);
maxPooling2dLayer(2,'Stride',2)
%fc
fullyConnectedLayer(100)
dropoutLayer(0.7,'Name','drop1');
%fc
fullyConnectedLayer(25)
dropoutLayer(0.8,'Name','drop2');
% fc layer
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
% specify training option
options = trainingOptions('adam', ...
'InitialLearnRate',0.001, ...
'MaxEpochs',15, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
% train network using training data
net = trainNetwork(imdsTrain,layers,options);
% classify validation images and compute accuracy
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
%calculate accuracy
accuracy = sum(YPred == YValidation)/numel(YValidation);
8 commentaires
Don Mathis
le 19 Fév 2019
Does it still happen if you use a much larger MiniBatchSize, say 1000 or 2000?
Andrik Rampun
le 19 Fév 2019
Modifié(e) : Andrik Rampun
le 19 Fév 2019
Jon Cherrie
le 19 Fév 2019
I think Don means the MiniBatchSize in the training options,
The drop in validation accuracy in the plot might be the same as this:
Andrik Rampun
le 19 Fév 2019
Andrik Rampun
le 20 Fév 2019
Modifié(e) : Andrik Rampun
le 20 Fév 2019
amir aslam
le 16 Avr 2019
I have done the same experiment but accuracy does not drop
code:
clc;
clear;
%set training dataset folder
digitDatasetPath = fullfile('C:\Program Files\MATLAB\R2018aaa\toolbox\nnet\nndemos\nndatasets\Experiments\Train');
%training set
imdsTrain = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
%set validation dataset folder
validationPath = fullfile('C:\Program Files\MATLAB\R2018aaa\toolbox\nnet\nndemos\nndatasets\Experiments\Test');
%testing set
imdsValidation = imageDatastore(validationPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
%create a clipped ReLu layer
%layer = clippedReluLayer(10,'Name','clip1');
% define network architecture
layers = [
imageInputLayer([48 48 3]);
% conv_1
convolution2dLayer(3,32,'Stride',1)
batchNormalizationLayer
reluLayer;
maxPooling2dLayer(2,'Stride',2)
%fc
fullyConnectedLayer(100)
dropoutLayer(0.7,'Name','drop1');
%fc
fullyConnectedLayer(25)
dropoutLayer(0.8,'Name','drop2');
% fc layer
fullyConnectedLayer(7)
softmaxLayer
classificationLayer];
% specify training option
options = trainingOptions('adam', ...
'InitialLearnRate',0.0001, ...
'MaxEpochs',15, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
% train network using training data
net = trainNetwork(imdsTrain,layers,options);
% classify validation images and compute accuracy
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
%calculate accuracy
accuracy = sum(YPred == YValidation)/numel(YValidation);

Sridharan K
le 10 Mar 2021
i got 100% accuracy. thanks for this program.
Santhosh Surya Kiran
le 1 Juil 2021
What u got is wrong..
Réponses (4)
Andrik Rampun
le 19 Fév 2019
18 commentaires
Don Mathis
le 19 Fév 2019
What is the size of your training set?
Andrik Rampun
le 20 Fév 2019
Andrik Rampun
le 20 Fév 2019
Don Mathis
le 20 Fév 2019
Modifié(e) : Don Mathis
le 20 Fév 2019
Your training set has 740 observations, which is a very small training set size, especially for such a large network. Now that you have specified MiniBatchSize=2048, each minibatch contains the entire training set, which we can verify by noticing that the number of iterations is equal to the number of epochs.
Looking at your final plots, I would say there's a good chance that this fixed the problem. The finalized loss is visually identical to the unfinalized loss, and the finalized error rate jumps about 5%, which is probably 5 of your 99 validation observations. I'm not sure why it jumps at all, but 5 observations switching their class seems like noise to me.
You need to run many more iterations. 15 is not enough. I suggest running for at least 100, or until validation error levels off.
Oh, and if you can, try to get your hands on 1 million observations :-)
Andrik Rampun
le 20 Fév 2019
Andrik Rampun
le 21 Fév 2019
Modifié(e) : Andrik Rampun
le 21 Fév 2019
Don Mathis
le 21 Fév 2019
What version of MATLAB are you using? (Which of R2017b, R2018a, R2018b?)
Andrik Rampun
le 21 Fév 2019
Don Mathis
le 21 Fév 2019
There are 2 ways I'm aware of that BatchNormalization layers can cause this problem:
(1) There is some layer or data transformation that occurs only during training (not during prediction), and which precedes a batchnorm layer (by any distance). For example, if a dropout layer precedes a batchnorm layer, or if your training images have noise added, and your validation images do not.
(2) The mean and variance of your minibatch activations don't match the mean and variance of the full dataset activations.
Unless you're adding noise to your training images in some way, it doesn't appear that either of these scenarios apply to you.
Andrik Rampun
le 21 Fév 2019
Modifié(e) : Andrik Rampun
le 21 Fév 2019
Don Mathis
le 21 Fév 2019
Modifié(e) : Don Mathis
le 21 Fév 2019
It doesn't look like you're doing anything wrong. I think this is just how these layers behave under these circumstances.
But it's difficult to draw precise conclusions from the experiments you've done because you have used 3 very different networks. The first had 1 conv, 3 fullyConnected, and very high dropout. The second had 1 conv, 1 fullyConnected, and no dropout. The third had 6 conv, 4 fullyConnected, and moderate dropout.
My next hypothesis would be that batchnorm may show this 'drop' effect when the network is too large for the dataset size.
My best guess for removing the 'drop' issue would be:
- Continue to use a large minibatch size (e.g., 2000 or the size of the training set).
- Start with small networks (like your first one) and work up to larger ones gradually.
- Use a dropout rate <= 0.5.
- Do not put dropout before batchnorm in the network.
Andrik Rampun
le 22 Fév 2019
Andrik Rampun
le 22 Fév 2019
Don Mathis
le 22 Fév 2019
Your networks were ok in that regard. You only put dropout after batchnorm. So that's not why you're getting the 'drop' effect.
Andrik Rampun
le 26 Fév 2019
Don Mathis
le 26 Fév 2019
Modifié(e) : Don Mathis
le 26 Fév 2019
3136 hidden units in each FC layer times batchsize of 2048 adds up to a lot of memory. It's trying to put the whole batch on the GPU at once and failing. This is a common problem. You just need to shrink either the batchsize or parts of the network until it fits.
Andrik Rampun
le 26 Fév 2019
Don Mathis
le 26 Fév 2019
Yes, MiniBatchSize. And I meant the outputSize of your fullyConnectedLayers could be reduced to something smaller than 3136:
fullyConnectedLayer(3136)
Saira
le 15 Juin 2020
0 votes
Hi,
I have 5600 training images. I have extracted features using Principal Component Analysis (PCA). Then I am applying CNN on extracted features. My training accuracy is 30%. How to increase training accuracy?
Feature column vector size: 640*1
My training code:
% Convolutional neural network architecture
layers = [
imageInputLayer([1 640 1]);
reluLayer
fullyConnectedLayer(7);
softmaxLayer();
classificationLayer()];
options = trainingOptions('sgdm', 'Momentum',0.95, 'InitialLearnRate',0.0001, 'L2Regularization', 1e-4, 'MaxEpochs',5000, 'MiniBatchSize',8192, 'Verbose', true);
Sevda Kemba
le 6 Juin 2022
0 votes
@Andrik Rampun Hello. In Matlab, we load the data set with code and limit it in deep learning. But when we train, validation accuracy stays between 40-50%. What can we do to increase it to 90%? We would be very happy if you could help.
Sevda Kemba
le 6 Juin 2022
0 votes
@Saira Hello. In Matlab, we load the data set with code and limit it in deep learning. But when we train, validation accuracy stays between 40-50%. What can we do to increase it to 90%? We would be very happy if you could help.
Catégories
En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



