Untitled
unknown
plain_text
a year ago
2.3 kB
7
Indexable
% Load data
load IRIS_IN.csv; % Input data
load IRIS_OUT.csv; % Target output
input = IRIS_IN;
target = IRIS_OUT;
% Initialize weights
WeightIn = 2 * rand(4, 10) - 1 ; % Weights from input to hidden layer
WeightHidden = 2 * rand(10, 1) - 1; % Weights from hidden to output layer
% Parameters
epoch = 100;
trainDataSize = 75;
testDataSize = 75;
% Preallocate RMSE array
RMSE = zeros(epoch, 1);
for currentEpoch = 1:epoch
Tot_Error = 0;
for currentTrainDataIndex = 1:trainDataSize
% Forward propagation
Hidden = input(currentTrainDataIndex, :) * WeightIn;
logsigHidden = logsig(Hidden);
Output = logsigHidden * WeightHidden;
linearOutput = purelin(Output);
% Error calculation
DeltaOutput = target(currentTrainDataIndex, :) - linearOutput;
Tot_Error = Tot_Error + DeltaOutput^2;
% Backpropagation
DeltaHidden = (DeltaOutput * dpurelin(linearOutput)) * WeightHidden';
WeightHidden = WeightHidden + 0.45 * logsigHidden' * DeltaOutput * dpurelin(linearOutput);
% Calculate DeltaInput correctly
DeltaHidden = DeltaHidden .* dlogsig(logsigHidden); % Apply derivative element-wise
WeightIn = WeightIn + 0.45 * input(currentTrainDataIndex, :)' * DeltaHidden; % Update input weights
end
% Calculate and store RMSE
RMSE(currentEpoch) = sqrt(Tot_Error / trainDataSize);
fprintf('Epoch: %d, RMSE: %f\n', currentEpoch, RMSE(currentEpoch));
end
% Plot RMSE
figure;
plot(1:epoch, RMSE);
legend('Training');
ylabel('RMSE');
xlabel('Epoch');
% Testing phase
Tot_Correct = 0;
for currentTestDataIndex = trainDataSize + 1:trainDataSize + testDataSize
Hidden = input(currentTestDataIndex, :) * WeightIn;
logsigHidden = logsig(Hidden);
Output = logsigHidden * WeightHidden;
linearOutput = purelin(Output);
fprintf('Test %d: %f\n',currentTestDataIndex, linearOutput);
if linearOutput > target(currentTestDataIndex, :) - 0.5 && linearOutput < target(currentTestDataIndex, :) + 0.5
Tot_Correct = Tot_Correct + 1;
end
end
Tot_Percent = Tot_Correct / testDataSize;
fprintf('Test Correct Percentage: %f\n', Tot_Percent);
Editor is loading...
Leave a Comment