自创建神经网络

xiaoxiao2021-03-01  3

%自创建神经网络 P=[-3,2]; T=[0.4,0.8]; Wrange=-4:0.4:4; Brange=-4:0.4:4; %W值的行向量、B值的行向量 ES=errsurf(P,T,Wrange,Brange,'logsig'); %求单神经元的误差平面（只用在单神经元中） %Error surface of single input neuron % errsurf(P,T,WV,BV,F) takes these arguments, % P -- 1 x Q matrix of input vectors % T -- 1 x Q matrix of target vectors % WV -- Row vector of values of W % BV -- Row vector of values of B % F -- Transfer function (string) % and returns a matrix of error values over WV and BV. mesh(ES,[60,30]); %作三维网状面，视角【60，30】 title('Error Surface Graph') %标题 xlabel('W') ylabel('B') zlabel('Sum Squared Error') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%华丽分割线 max_epoch=1500; %赋最大训练次数 err=[]; xx=[]; yy=[]; %定义矩阵 lr=0.5; W1=-4*rand(1); %赋权值和偏差的初值 b1=-4*rand(1); x1=W1; y1=b1; for i=1:max_epoch A1 = logsig(W1*P+b1*ones(1,2)); %计算输出 % Log sigmoid transfer function % % logsig is a transfer function. Transfer functions calculate a layer's output from its net input. % logsig(N) takes one input, % N -- S x Q matrix of net input (column) vectors % and returns each element of N squashed between 0 and 1. % logsig(code) returns useful information for each code string: % 'deriv' -- Name of derivative function % 'name' -- Full name % 'output' -- Output range % 'active' -- Active input range E = T-A1; %求误差 D1 = A1.*(1-A1).*E; %矩阵对应元素相乘 dW1 = D1*P'*lr; %求权值增量 db1 = D1*ones(2,1)*lr; %求偏差增量 newx = W1(1,1) + dW1(1,1); %新的权值 W1(1,1) = newx; xx =[xx newx]; newy = b1(1) + db1(1); %新的偏差 b1(1) = newy; yy =[yy newy]; SSE = sumsqr(E); %求误差平方和 err=[err SSE]; if (SSE<1e-8) break; end end figure(2) [C,h] =contour(Wrange,Brange,ES); %作等高线图，ES为高 %返回等高线矩阵C，列向量h是线或对象的句柄，一条线一个句柄，这些被用作CLABEL的输入， %每个对象包含每个等高线的高度 %A contour graph displays isolines of matrix Z. Label the contour lines using clabel % contour(X,Y,Z,n), and contour(X,Y,Z,v) draw contour plots of Z. % X and Y specify the x- and y-axis limits. When X and Y are matrices, %they must be the same size as Z, in which case they specify a surface, %as defined by the surf function. % If X or Y is irregularly spaced, % contour calculates contours using a regularly spaced contour grid, % then transforms the data to X or Y. clabel(C,h) %标上高度值 colormap cool %背景的颜色cool axis('equal') hold on plot(x1,y1,'r+') plot(xx,yy,'b*') %作矩阵变化曲线 hold off figure(3) plot(err) %作误差曲线 err