using Neural Network without toolbox
6 次查看(过去 30 天)
显示 更早的评论
I have to write a code to model Neural Network. I write it with sigmoid function, back propogation, and gradient descent method.
My problem is that I can not insert input higher than 1.
This is my code:
X = (0:0.01:1.5);
X = X';
LX = length(X);
B_size = 1;
NO_B = (LX / B_size);
Y_d = X.^2;
Width = 20;
H = zeros (Width,1);
H_f = zeros (Width,1);
Y = zeros(LX,1);
Y_f = zeros(LX,1);
W1 = rand (Width,B_size);
W2 = rand (B_size,Width);
b1 = 1 ;
b2 = 1 ;
E_total = 1;
Eta = 0.1;
itt = 0;
epoch = 1500;
for e = 1 : epoch
for i = 1 : NO_B
itt = itt + 1;
XX = X( (B_size * (i-1)) +1 : (i*B_size) );
YY_d = Y_d( (B_size * (i-1)) +1 : (i*B_size) );
H = W1*XX + b1;
H_f = SIG(H);
Y = W2*H_f + b2;
Y_f = SIG(Y);
E_total = sum ( 0.5 * (( YY_d - Y_f ).^2)) ;
E(itt) = E_total;
ITT(itt) = itt;
delta = YY_d - Y_f ;
dY = Y_f.*(1-Y_f) ;
dH = H_f.*(1-H_f) ;
pd2 = (delta.*dY) * H_f' ;
pd1 = (XX *((delta.*dY)' * W2).* dH')' ;
W2 = W2 + Eta*pd2;
W1 = W1 + Eta*pd1;
YY_f ( (B_size * (i-1)) +1 : (i*B_size) )= Y_f;
end
end
plot(X,YY_f,'r*',X,Y_d,'b:','LineWidth',2);
function [alpha_f] = SIG(alpha)
%SIGMOID FUNCTION
alpha_f = 1 ./ (1 + ((exp(1)) .^ (-alpha)));
end
2 个评论
Walter Roberson
2022-12-2
It is not clear to me which is the input that you cannot make larger than 1. Also you did not indicate what happens when you try to do that.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!