Help with Multilayer Perceptron algorithm code.
Afficher commentaires plus anciens
I'm trying to write MATLAB code for Multilayer Perceptron. My dataset is a bunch of images and I'm trying to use MLP for segmenting some objects. I'm giving the extracted features as input. Presently, the network has just one hidden layer and I have tried varying number of nodes in the hidden layer. I tried varying my learning rate from 0.1 to 0.9. The error is oscillating between 1000 and 3000. I'm not sure where the mistake is. Can someone verify my code?
function model = MLP_train(fea,OV,rate,NoIter,hidden1,visualize)
%
% INPUTS:
% fea -> Input feature vector
% OV -> Target Vector
% rate -> Learning rate
% NoIter -> No of iterations/epochs
% hidden1 -> No of hidden layers
% OUTPUT:
% model -> learned model
[m,n] = size(fea);
%Adding bias
bias = ones(m,1);
fea = [fea bias];
NoOfInputNodes = n + 1;
NoOfHiddenLayerNodes = NoOfInputNodes;
%Generate Random Weights
weightsInputHidden = (randn(NoOfInputNodes,NoOfHiddenLayerNodes) - 0.5)/10;
weightsHiddenOutput = (randn(1,NoOfHiddenLayerNodes) - 0.5)/10;
for iter = 1:NoIter
randInput = randperm(m,m);
for i = 1:m
input = fea(randInput(i),:);
target = OV(randInput(i),1);
inputHidden = tanh(input*weightsInputHidden);
HiddenOutput = inputHidden*weightsHiddenOutput';
error = HiddenOutput - target;
delta = error.*rate.*inputHidden;
weightsHiddenOutput = weightsHiddenOutput - delta;
delta= (rate/10).*error.*weightsHiddenOutput'.*(1-(inputHidden'.^2))*input;
weightsInputHidden = weightsInputHidden - delta';
end
pred = weightsHiddenOutput*tanh(fea*weightsInputHidden)';
err(iter) = (sum((pred' - OV).^2))^0.5;
fprintf('Epoch no -> %d; Error = %d\n',iter,err(iter));
if err(iter) < 0.001
fprintf('Converged at epoch: %d\n',iter);
break;
end
end
if visualize == 1
plot(1:iter,err);
title('Error plot');
end
model.weightsInputHidden = weightsInputHidden;
model.weightsHiddenOutpu = weightsHiddenOutput;
end
Thank you.
1 commentaire
Angel Esqueda
le 18 Mai 2017
Could you solve it? Thanks in advance
Réponses (0)
Catégories
En savoir plus sur Define Shallow Neural Network Architectures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!